AlgoBazaAlgoBaza
Python for kids ยท Module 1

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.

Bajtek
๐Ÿ“ฆlabel: ageinside: empty
1

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.

Step 1 / 5

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.

not yet
๐Ÿ“ฆ number

Click the arrow and watch the values move between the boxes.

Code
1number = 5
2print(number)
Screen
โ–
1/3 ยท line 1 runs next

Boxes in the computer's memory

Step through the program and watch the boxes with variables appear.

๐Ÿง  The computer's memory
๐Ÿท๏ธ nameยท
๐Ÿท๏ธ ageยท

The memory is still empty. Click the arrow - every line with = puts down a new box.

Code
1name = "Bajtek"
2age = 3
3print(name)
4print(age + 1)
5print(name, age)
Screen
โ–
1/6 ยท line 1 runs next

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 computer's memory
๐Ÿท๏ธ dogsยท
๐Ÿท๏ธ catsยท
๐Ÿท๏ธ fishยท

The memory is still empty. Click the arrow - every line with = puts down a new box.

Code
1dogs = 3
2cats = 5
3fish = 12
4print("Dogs:", dogs)
5print("Cats:", cats)
6print("Fish:", fish)
7print("Pets in total:", dogs + cats + fish)
Screen
โ–
1/8 ยท line 1 runs next

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 computer's memory
๐Ÿท๏ธ lengthยท
๐Ÿท๏ธ widthยท
๐Ÿท๏ธ areaยท
๐Ÿท๏ธ perimeterยท

The memory is still empty. Click the arrow - every line with = puts down a new box.

Code
1length = 4
2width = 3
3area = length * width
4perimeter = 2 * length + 2 * width
5print("The area is:", area)
6print("The perimeter is:", perimeter)
Screen
โ–
1/7 ยท line 1 runs next

A game character card ๐Ÿง™

Text and two numbers - each in its own box. Every box fills one field of the character card.

๐Ÿง™
???
โญ Level?
โค๏ธ Health?

Click the arrow - every variable fills one field of the character card.

Code
1name = "Merlin"
2level = 7
3health = 85
4print("Character:", name)
5print("Level:", level)
6print("Health:", health)
Screen
โ–
1/7 ยท line 1 runs next

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.

๐Ÿ“ฆ mark = ?
โ€ฆ

Click the arrow - first the character goes into the box, then every line of the tree takes it from there.

Code
1mark = "*"
2print(" " + mark)
3print(" " + mark * 3)
4print(" " + mark * 5)
5print(" |")
Screen
โ–
1/6 ยท line 1 runs next

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 same
โ€ฆ

Click the arrow - first the character goes into the box, then every line of the tree takes it from there.

Code
1mark = "o"
2print(" " + mark)
3print(" " + mark * 3)
4print(" " + mark * 5)
5print(" |")
Screen
โ–
1/6 ยท line 1 runs next

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.

๐Ÿ“ฆ people = ?
at the table
๐ŸŒพ Floura portion for 1 person: 50 g
๐Ÿฅ› Milka portion for 1 person: 100 ml
๐Ÿฅš Eggsa portion for 1 person: 1

Click the arrow - first the number of people goes into the box.

Code
1people = 1
2print("Flour (g):", 50 * people)
3print("Milk (ml):", 100 * people)
4print("Eggs:", 1 * people)
Screen
โ–
1/5 ยท line 1 runs next

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 = ?
at the table
๐ŸŒพ Floura portion for 1 person: 50 g
๐Ÿฅ› Milka portion for 1 person: 100 ml
๐Ÿฅš Eggsa portion for 1 person: 1

Click the arrow - first the number of people goes into the box.

Code
1people = 4
2print("Flour (g):", 50 * people)
3print("Milk (ml):", 100 * people)
4print("Eggs:", 1 * people)
Screen
โ–
1/5 ยท line 1 runs next

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 = ?
at the table
๐ŸŒพ Floura portion for 1 person: 50 g
๐Ÿฅ› Milka portion for 1 person: 100 ml
๐Ÿฅš Eggsa portion for 1 person: 1

Click the arrow - first the number of people goes into the box.

Code
1people = 10
2print("Flour (g):", 50 * people)
3print("Milk (ml):", 100 * people)
4print("Eggs:", 1 * people)
Screen
โ–
1/5 ยท line 1 runs next

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: price is better than x.

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.

๐Ÿท๏ธ age๐Ÿ“ฆยท
โ€ฆ
?

Click the arrow - first a number goes into the box age.

Code
1age = 11
2print(age)
3print("age")
4print("age:", age)
Screen
โ–
1/5 ยท line 1 runs next

A variable that doesn't exist

A typo in the name: there's no box called agee, so Python reports a NameError.

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputint

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1age = 11
2print(agee)
Screen
โ–
1/3 ยท line 1 runs next

Ola has as many as Kuba ๐Ÿฌ

Click Next and see what ola = kuba does.

Bajtek
Thank you, Grandma!๐Ÿ‘ฆ
๐Ÿ‘ต๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌโžก๏ธ๐Ÿ“ฆkuba: 5

Code

kuba = 5

๐Ÿ–ฅ๏ธ Screen

๐Ÿ“ฆ kuba = 5
1

Grandma 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.

Step 1 / 6

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.

Code
1ola_goals = 3
2kuba_goals = ola_goals
3print("Ola:", ola_goals)
4print("Kuba:", kuba_goals)
5print("Draw", ola_goals, ":", kuba_goals)
Screen
โ–
1/6 ยท line 1 runs next

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.

Code
1ola_colour = "green"
2kuba_colour = ola_colour
3print("Ola picks the colour:", ola_colour)
4print("Kuba picks the colour:", kuba_colour)
Screen
โ–
1/5 ยท line 1 runs next
?

What will this program print?

a = 4
b = a
print(a + b)
?

Which variable name is correct?

?

What's in the area box after this program?

side = 6
area = 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 computer's memory
๐Ÿท๏ธ sandwichesยท
๐Ÿท๏ธ bottlesยท
๐Ÿท๏ธ nameยท

The memory is still empty. Click the arrow - every line with = puts down a new box.

Code
1sandwiches = 3
2bottles = 2
3name = "Ola"
4print(name, "has", sandwiches, "sandwiches")
5print("Items in total:", sandwiches + bottles)
Screen
โ–
1/6 ยท line 1 runs next

Guess what this calculates

Both programs calculate the same thing. Which one is easier to understand?

๐Ÿท๏ธ a?๐Ÿท๏ธ b?
๐Ÿท๏ธ notebook_price?๐Ÿท๏ธ notebook_count?

Click the arrow - first the first program, then the second.

Code
1a = 12
2b = 3
3print(a * b)
4notebook_price = 12
5notebook_count = 3
6print(notebook_price * notebook_count)
Screen
โ–
1/7 ยท line 1 runs next

A variable or just text?

This is a very important difference:

CodeScreenWhy
print(name)Olano quotes - the contents of the box
print("name")namein 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 computer's memory
๐Ÿท๏ธ lengthยท
๐Ÿท๏ธ widthยท
๐Ÿท๏ธ areaยท
๐Ÿท๏ธ perimeterยท

The memory is still empty. Click the arrow - every line with = puts down a new box.

Code
1length = 5
2width = 3
3area = length * width
4perimeter = 2 * length + 2 * width
5print("The area is:", area)
6print("The perimeter is:", perimeter)
Screen
โ–
1/7 ยท line 1 runs next

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?

  1. I put the prices in boxes: ticket = 18, popcorn = 12.
  2. The number of people: people = 4.
  3. One person pays: per_person = ticket + popcorn - that is 30.
  4. Everyone together: total = per_person * people - that is 30 * 4 = 120. Only the ready results are printed with print. ๐Ÿฟ

The cinema in code

๐Ÿท๏ธ ticket๐ŸŽŸ๏ธ ? zl๐Ÿท๏ธ popcorn๐Ÿฟ ? zl๐Ÿท๏ธ people๐Ÿง‘ ?๐Ÿท๏ธ per_person? zl๐Ÿท๏ธ total? zl

Click the arrow - first the prices and the number of people go into boxes.

Code
1ticket = 18
2popcorn = 12
3people = 4
4per_person = ticket + popcorn
5total = per_person * people
6print("One person pays:", per_person)
7print("Everyone together:", total)
Screen
โ–
1/8 ยท line 1 runs next
?

What does the program print?

cat = "Tom"
print("cat")
?

Which variable name is valid?

Remember

  • name = value puts 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.