Python Notes
Python Notes
Python Notes
What is a String?
Strings were used in "Hello World!" print function examples. Strings are sets of
characters. In Python strings are in double or single quotes like "Hello
World!" or 'after edit, save!'
String examples: "ABC", 'Joana Dias', 'I have 2 pet cats.', "item #3 cost $3.29 USD"
"Hello' is incorrectly formatted starting with double " and ending with single '
python needs a matching quote to know where the string ends
Example
# examples of printing strings with single and double quotes
print('strings go in single')
Task 1
print messages with "double quotes" and 'single' quotes
# [ ] enter a string in the print() function using single
quotes
A String can contain any character, this includes letters, spaces, punctuation,
number digits and formatting. In a string with digits ("123") the digits are text images
rather than representations of numeric values.
remember a line of python code starting with the pound or hash symbol (#) indicates
a comment
comments are for humans to read and are not run by computers
Example
# printing an Integer with python
print(299)
print("2017")
Task 2
print Integers
# [ ] print an Integer
Student Tip: look for [ ] , which indicates a task in the notebook needs to be
completed.
Edit and Run to complete the task
Concepts: Variables
Variables & Storing Strings in Variables
Variables are named containers
Computer programs often create storage for things that are used in repeated
processing like item_price, student_name, stock_symbol. In python a variable is a
type of object that can be addressed by name to access the assigned contents.
item_price
student_name
stock_symbol
descriptive names reduce the need for comments in the code and make it easier to
share code or read code written long ago
Variable reassignment
We can reassign a variable, changing the contents. current_msg = "new
current message"
Example
Initializing variables
Variable reassignment
print("I am a string")
print(current_msg)
print(current_msg)
Task 3
Program: assigning values to a string
run the code cell above and then run the cell below
# { ] run cell above then run this cell after completing the
code as directed
Q?: In the cell above, why did the variable have a value to print before it was
assigned?
Jupyter notebooks run cells individually. Any cell in the notebook can access a
variable assigned from a cell that has been run in the current notebook session.
x = "I am a string"
Examples
name = "Joana Dias"
print(name)
test_value = 22
print(test_value)
test_value = "Joana"
print(test_value)
Task 4: multi-part
Assign a variable and print the value
Tasks 4 cont...
modified the value of a variable
Tasks 4 cont...
change variable type with reassignment
a string of digits can be assigned to the variable id --> id = "001023" the quotations,
mean the number symbols should be stored in the variable as type string, meaning the
digits "001023" are treated as text
Tasks 4 cont...
run the above code the next cell:
# [ ] print integer 123 number
str: when type() returns str that means it has evaluated a string characters (numbers,
letters, punctuation...) in quotes
int: when type() returns int that means it has evaluated an Integer (+/- whole numbers)
float: when type() returns float that means it has evaluated decimal numbers (e.g.
3.33, 0.01, 9.9999 and 3.0), ...more later in the course
Example
Read and run the code for each sample
type("Hello World!")
type(501)
type(8.33333)
type(student_name)
Task 1: multi-part
Using type()
Complete the "identify data types" tasks by assigning values to the variable bucket and
using type()
# [ ] show the type after assigning bucket = a whole number
value such as 16
3 + 5
String addition
string addition single line equations, run in a code cell, will output a single
concatenated string
We can also add variables as long as we add strings to strings and numbers to
numbers
Example
String and Number Addition
# [ ] Review and run code for adding a pair of 2 digit
Integers
23 + 18
shoe_color = "brown"
Task 1: multi-part
String and Number Addition
# [ ] add 3 integer numbers
Example
addition in variable assignments and in print()
add_two = 34 + 16
first_name = "Alton"
print(add_two)
print(greeting)
int_sum = 6 + 7
print(int_sum)
print(11 + 15)
print()
print(hat_msg)
Task 2: multi-part
create Integer addition and string addition output
# [ ] perform string addition in the variable named new_msg
(add a string to "my favorite food is ")
print(new_msg)
new_sum = 0
print(new_sum)
Concept: Errors
Errors!
Encountering Errors and troubleshooting errors are fundamental parts of computer
programming
Example
# [ ] review & run code
TypeError
The line print("my number is " + 123) causes the TypeError message to
appear
When adding to the string "my number is " the compiler is experiencing another
string, but finds a number *123
Python cannot convert the Integer 123 to a string without explicit instruction (in
code)
str + str
int + int
Task 3
Fix TypeError
Review the code in the cells below and then run the code
Fix any errors and run until the code no longer shows errors
total_cost = 3 + "45"
print(total_cost)
school_num = 123
# [ ] Read and run the code - write a hypothesis for what you
observe adding float + int
# [ ] HYPOTHESIS:
print(type(3.3))
print(type(3))
print(3.3 + 3)
Concept: Errors
More Errors!
SyntaxError & NameError
Example
SyntaxError
print("Hi!")
prin('hi')
Python went to the end of the file looking for, but not finding, a closing parenthesis
Task 4
Fix Errors
Tip: explaining errors to a partner often reveals a solution (works even if explaining
error to a pencil)
student_name = "Alton"
print(STUDENT_NAME)
total = 3
print(" *")
print(" * *")
print(" *****")
print(" * *")
print("* *")
print()
Extra Task
create the flying bird in character art in the Code cell below
_ _
\ /
\ . . /
Example
# review and run code - enter a small integer in the text box
small_int = input()
print(small_int)
Task 1
storing input in a variable
Task 1 continued...
note: input() returns a string (type = str) regardless of entry
test_input = input()
Example
student_name = input("enter the student name: ")
Task 2
prompting the user for input
[ ] create a variable named city to store input, add a prompt for the name of a city
[ ] print "the city name is " followed by the value stored in city
Task 2 cont...
multiple prompts for user input
often programs need information on multiple items
[ ] create prompts for name, age and yes/no to being on an email list
tip: with multiple input statements, after each prompt, click 'in' the input box to
continue entering input
The print() function supports using commas to combine strings for output
by comma separating strings print() will output each string separated with a space
by default
[ ] print 3 strings on the same line using commas inside the print() function
Example
# review and run code
name = "Colette"
# string addition
Task 1
print 3 strings on the same line using commas inside the print() function