Lesson 11
Variables - labelled boxes
After this lesson: You'll store numbers and text in variables and calculate with them.
Video
The video for this lesson is coming soon.
Explanation
Imagine a box with a label. You write a name on the label and put a number or text inside. In programming such a box is a variable.
Creating a variable
age = 11 means: take a box labelled "age" and put 11 in it.
- On the left of
=is the variable's name, on the right - the value. - When you later write
age(without quotes!), Python looks into the box and uses what's inside. - The
=sign in Python doesn't mean "equals" like in maths - it means "put into the box".
A box with a label ๐ฌ
Click Next and see how a variable works.
An empty box
Bajtek is 11 ๐ and wants the program to remember it. He takes an empty box ๐ฆ and sticks a label on it that says age.
One box ๐ฆ
Line 1: we put the number 5 into the box labelled number. Line 2: print looks into the box and prints what's in it - 5.
Click the arrow and watch the values move between the boxes.
1number = 52print(number)
โBoxes in the computer's memory
Step through the program and watch the boxes with variables appear.
The memory is still empty. Click the arrow - every line with = puts down a new box.
1name = "Bajtek"2age = 33print(name)4print(age + 1)5print(name, age)
โA pet shop ๐ถ
Three boxes: dogs, cats and fish. First we print how many of each pet there are, and at the end we count them all together: 3 + 5 + 12.
The memory is still empty. Click the arrow - every line with = puts down a new box.
1dogs = 32cats = 53fish = 124print("Dogs:", dogs)5print("Cats:", cats)6print("Fish:", fish)7print("Pets in total:", dogs + cats + fish)
โWhat can go into a box?
- a number:
points = 100, - text:
name = "Ola"(text in quotes!), - the result of a calculation:
area = 4 * 3- Python first calculates 12 and then puts 12 into the box, - the contents of another box:
copy = points.
Calculating with variables
Notice that the area box holds the already calculated number 12, not the calculation. Below is the same rectangle as a picture: the area is the squares inside, and the perimeter is the fence around.
The memory is still empty. Click the arrow - every line with = puts down a new box.
1length = 42width = 33area = length * width4perimeter = 2 * length + 2 * width5print("The area is:", area)6print("The perimeter is:", perimeter)
โA game character card ๐ง
Text and two numbers - each in its own box. Every box fills one field of the character card.
Click the arrow - every variable fills one field of the character card.
1name = "Merlin"2level = 73health = 854print("Character:", name)5print("Level:", level)6print("Health:", health)
โWhy do we need variables?
Imagine the rectangle now has length 5 instead of 4. Without variables you'd have to fix every calculation. With variables you change one line length = 5 - and everything is recalculated. Variables also have names that say what you're calculating - the code reads like a sentence.
A tree with a variable ๐
We keep the tree's character in the box mark. Every line of the tree takes it from the box.
Click the arrow - first the character goes into the box, then every line of the tree takes it from there.
1mark = "*"2print(" " + mark)3print(" " + mark * 3)4print(" " + mark * 5)5print(" |")
โWe change only the first line โจ
The same program - we changed only mark = "*" to mark = "o". We don't touch the rest of the code, and the whole tree is now made of baubles! That's what variables are for: change it in one place, and it changes everywhere.
mark = "*"โก๏ธ๐ฆ mark = ?the rest of the code is the sameClick the arrow - first the character goes into the box, then every line of the tree takes it from there.
1mark = "o"2print(" " + mark)3print(" " + mark * 3)4print(" " + mark * 5)5print(" |")
โPancakes for 1 person ๐ฅ
The recipe for one person: 50 g of flour, 100 ml of milk and 1 egg. We keep the number of people in the box people.
Click the arrow - first the number of people goes into the box.
1people = 12print("Flour (g):", 50 * people)3print("Milk (ml):", 100 * people)4print("Eggs:", 1 * people)
โPancakes for a family (4 people) ๐จโ๐ฉโ๐งโ๐ฆ
The same program - we changed only people = 1 to people = 4. All the ingredients recalculated themselves!
people = 1โก๏ธ๐ฆ people = ?Click the arrow - first the number of people goes into the box.
1people = 42print("Flour (g):", 50 * people)3print("Milk (ml):", 100 * people)4print("Eggs:", 1 * people)
โPancakes for grandparents and two families (10 people) ๐ต๐ด
Grandparents and two families of 4 make 2 + 4 + 4 = 10 people. Again we change only the first line - the rest of the code stays the same.
people = 4โก๏ธ๐ฆ people = ?Click the arrow - first the number of people goes into the box.
1people = 102print("Flour (g):", 50 * people)3print("Milk (ml):", 100 * people)4print("Eggs:", 1 * people)
โGood variable names
- small letters, no spaces:
age,number_of_cats, - digits are fine, but not at the start:
player1- good,1player- wrong, - instead of a space - an underscore
_, - the name should say what's inside:
priceis better thanx.
age or "age"?
print(age) prints what's in the box (e.g. 11), while print("age") prints the word age. Quotes make a huge difference!
With and without quotes
Line 1: we put 11 into the box labelled age. Line 2: age without quotes - Python looks into the box and prints what's inside: 11. Line 3: "age" in quotes - it's plain text, Python copies it letter by letter: age. Line 4: both at once - the text as a label, and next to it what's in the box. Hint: in the code, text in quotes is orange.
Click the arrow - first a number goes into the box age.
1age = 112print(age)3print("age")4print("age:", age)
โA variable that doesn't exist
A typo in the name: there's no box called agee, so Python reports a NameError.
Click the arrow. For every name Python checks its list to see if it knows it.
1age = 112print(agee)
โOla has as many as Kuba ๐ฌ
Click Next and see what ola = kuba does.
Code
๐ฅ๏ธ Screen
๐ฆ kuba = 5Grandma brings sweets
Grandma ๐ต came to visit and gave Kuba a box with 5 sweets ๐ฌ. That's the first line: kuba = 5 - we put 5 into the box labelled kuba.
A draw โฝ
Ola scored 3 goals, and Kuba the same. kuba_goals = ola_goals looks into the box ola_goals and puts the same 3 into a new box.
1ola_goals = 32kuba_goals = ola_goals3print("Ola:", ola_goals)4print("Kuba:", kuba_goals)5print("Draw", ola_goals, ":", kuba_goals)
โThe same colour ๐จ
A box can hold text too. kuba_colour = ola_colour copies the text "green" into the second box - both boxes now have the same colour.
1ola_colour = "green"2kuba_colour = ola_colour3print("Ola picks the colour:", ola_colour)4print("Kuba picks the colour:", kuba_colour)
โWhat will this program print?
a = 4b = aprint(a + b)
Which variable name is correct?
What's in the area box after this program?
side = 6area = side * side
Imagine a cabinet with drawers. Every drawer has a label with a name, and something lies inside. A variable is such a drawer! ๐๏ธ
A backpack for a trip
Watch the variables panel: every line with = creates a new box.
The memory is still empty. Click the arrow - every line with = puts down a new box.
1sandwiches = 32bottles = 23name = "Ola"4print(name, "has", sandwiches, "sandwiches")5print("Items in total:", sandwiches + bottles)
โGuess what this calculates
Both programs calculate the same thing. Which one is easier to understand?
Click the arrow - first the first program, then the second.
1a = 122b = 33print(a * b)4notebook_price = 125notebook_count = 36print(notebook_price * notebook_count)
โA variable or just text?
This is a very important difference:
| Code | Screen | Why |
|---|---|---|
print(name) | Ola | no quotes - the contents of the box |
print("name") | name | in quotes - plain text |
A name without quotes is a label on a box - Python looks inside. ๐ฆ
A rectangle
The same program as above - we changed only length = 4 to length = 5. Dimensions stored once are used in several calculations, so changing one number changes every result: the rectangle is longer, and the area and the perimeter are recalculated by themselves.
The memory is still empty. Click the arrow - every line with = puts down a new box.
1length = 52width = 33area = length * width4perimeter = 2 * length + 2 * width5print("The area is:", area)6print("The perimeter is:", perimeter)
โSolving a task with variables
Task: A cinema ticket costs 18 zl, popcorn 12 zl. 4 friends go and each buys a ticket and popcorn. How much do they pay in total?
- I put the prices in boxes:
ticket = 18,popcorn = 12. - The number of people:
people = 4. - One person pays:
per_person = ticket + popcorn- that is 30. - Everyone together:
total = per_person * people- that is 30 * 4 = 120. Only the ready results are printed with print. ๐ฟ
The cinema in code
Click the arrow - first the prices and the number of people go into boxes.
1ticket = 182popcorn = 123people = 44per_person = ticket + popcorn5total = per_person * people6print("One person pays:", per_person)7print("Everyone together:", total)
โWhat does the program print?
cat = "Tom"print("cat")
Which variable name is valid?
Remember
name = valueputs the value into the box labelled name.=means "put into the box", not "equals".- A variable name is written without quotes, text - in quotes.
- Names: small letters, no spaces, don't start with a digit.
Now you create the boxes! In the tasks, first store the numbers in variables, then calculate with them.
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.