Variables
About 12 minutes
Why this matters
Programs work with information: prices, names, scores. A variable is a name you give to a piece of information so you can use it again later.
The idea
You create a variable with =. The name goes on the left, the value on the right:
age = 30
name = "Sam"
This is called assignment. Read it as “let age be 30”, not as the equals sign from maths class.
Once a variable exists you can use its name anywhere you could use the value:
age = 30
print(age) # shows 30
print(age + 1) # shows 31
You can do arithmetic with + (add), - (subtract), * (multiply) and / (divide). A variable can also hold the result of a calculation:
apples = 3
pears = 4
fruit = apples + pears
print(fruit) # shows 7
Lines starting with # are comments: Python ignores them, so you can leave notes for humans.
Try it
Use the editor to create a few variables of your own and print them. Try changing a value and running the program again. Everything that uses that variable changes with it.
Your turn
A coffee shop sells coffee for 3.75 dollars a cup, and you are buying 4 cups. The starter code already has the variables price and quantity.
- Create a variable called
totalthat holdspricemultiplied byquantity. - Print
total.
Use the variables rather than typing the answer yourself. That way the program still works if the price changes.
Recap
name = valuecreates (or updates) a variable.- A variable can hold the result of a calculation.
- Use
*to multiply and+,-,/for the other basic operations.
Common mistakes
- Using a variable before creating it:
print(total)beforetotal = ...fails withNameError. - Spelling or capitalisation differences:
Totalandtotalare different names. - Putting the value on the left:
5 = xis aSyntaxError. The name always goes on the left.