Four friends and one bill
A variable in Python is a name that holds a value, such as a price or an age. Programs use variables to remember things, so you can change one number and let the program redo every sum.
Four friends share a restaurant bill and add a tip. Doing it once is easy. A program does it for any bill, with no new work.
By the end of this lesson
Note
I can store values in variables and print a result.
Key words
| Word | What it means | Example |
|---|---|---|
| Variable | a name that holds a value | people = 4 |
| Integer | a whole number, called int in Python | 19 |
| Float | a number with a decimal point | 2.5 |
| String | text inside quotes, called str in Python | "Ana" |
| Boolean | either True or False, called bool in Python | |
| f-string | a string that fills in values from variables | f"Hello, {name}" |
The cells students fill in are left blank in this preview.
A variable is a name for a value
Write people = 4 and Python stores the number 4 under the name people. The sign = is called assignment. It means: work out the right side, then store it under the name on the left.
Later lines use the name instead of the number. Give a new value and the old one is gone. Choose names that say what the value is, such as total_price.
A bill split between friends
# A bill for four people, with a tip.
bill = 45.0
tip = 5.0
people = 4
each = (bill + tip) / people
print("Each person pays", each)
# Change people to 5 and run again.
A common mistake
Warning
Many people read total = total + 5 as a false sum, because a number cannot equal itself plus 5. In Python, = does not mean "is equal to". It means "store". The right side is worked out first, then stored under the name on the left.
Every value has a type
Python keeps track of what kind of value each variable holds. This is its type. Whole numbers are int, numbers with a decimal point are float, text is str, and True or False is bool.
The type decides what you can do. You can add two numbers. Adding two pieces of text joins them.
Worked example: four values, four types
| Value | Type | Good for |
|---|---|---|
| 19 | int | an age, a count of people |
| 2.5 | float | a price, a length |
| "Ana" | str | a name, a message |
| True | bool | a yes or no answer |
| "19" | str | text that looks like a number |
The cells students fill in are left blank in this preview.
Ask Python for three types
python3 -c "print(type(3), type(3.0), type('3'))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Four types in one program
age = 19 # int: a whole number
price = 2.5 # float: has a decimal point
name = "Ana" # str: text in quotes
is_student = True # bool: True or False
print(type(age), type(price))
print(type(name), type(is_student))
Python does arithmetic with operators
An operator is a symbol that works on values. +, - and * add, take away and multiply. / divides and always gives a float.
// divides and keeps only the whole part. % gives the remainder, what is left over. ** raises a number to a power.
Worked example: seven operators on 7 and 2
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | add | 7 + 2 | 9 |
| - | take away | 7 - 2 | 5 |
| * | multiply | 7 * 2 | 14 |
| / | divide | 7 / 2 | 3.5 |
| // | whole part of a division | 7 // 2 | 3 |
| % | remainder | 7 % 2 | 1 |
| ** | power | 7 ** 2 | 49 |
The cells students fill in are left blank in this preview.
Three divisions of 7 by 2
python3 -c "print(7 / 2, 7 // 2, 7 % 2)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Two to the power of ten
python3 -c "print(2 ** 10)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
An f-string puts values into text
Put the letter f before the opening quote and Python fills in anything inside curly brackets. print(f"Hello, {name}") prints the value held by name.
This is neater than joining pieces of text with +, and it works with numbers too.
Fill in a name and a total
name = "Ana"
total = 12.5
print(f"{name} pays {total} pounds")
# Change name to your own name and run again.
The same idea on one line
python3 -c "name = 'Ana'; print(f'Hello, {name}')"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
In the terminal, type python3 -c "print(0.1 + 0.2)" and read the answer. Floats are stored as close approximations, so a sum can be a tiny bit out.
Wrap the sum in round(..., 2) and run it again. Money is the usual reason to round.