Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 10+ · Coding · Python
Variables in Python
1coins = 32coins = coins + 13print(coins)
The box coins holds 3, then 4, and print shows 4.
The big ideaA variable is a named box that holds a value. The value can change.

How it works

A variable is a named box that holds a value. The value can change while the name stays the same.

One equals sign puts a value in the box. It does not ask a question.

Python works out the right side first, then stores the answer in the box on the left.

A box can hold a number or some text in quotes. A name has no spaces, so use an underscore.

Look in the box: 3Add one: 4Put the 4 back in thesame box
What coins = coins + 1 does.
The line• coins = coins + 1• coins = coins - 1• coins = 1The box then holds• 4• 2• 1
What each line does to coins when it holds 3.

Example 1: Add one

  1. The box coins holds 3, and Zara types coins = coins + 1.
  2. Python works out the right side first: 3 + 1 = 4.
  3. It stores the 4 back in the same box. Print shows 4.

Example 2: Take one away

  1. The box coins holds 3, and the line is coins = coins - 1.
  2. The right side first: 3 - 1 = 2.
  3. The box now holds 2.

Example 3: Put a new value in

  1. The box coins holds 3, and the line is coins = 1.
  2. The new value goes in, and the old three is gone.
  3. The box now holds 1.

Example 4: Four lines that print six

  1. The first line makes the box with coins = 2.
  2. Then coins = coins + 1: 2 + 1 = 3.
  3. Then coins = coins * 2: 3 * 2 = 6.
  4. Last, print(coins) shows what is in the box, which is 6.
1coins = 22coins = coins + 13coins = coins * 24print(coins)
Make the box, add one, double it, print it.

Example 5: A name with a space

  1. Leo names his box my coins, with a space, and Python refuses to read it.
  2. A name has no spaces.
  3. The mix-up to fix: write my_coins, with an underscore.

Words to know

variable
A named box that holds a value.
value
What is inside the box. It can change.
name
The label on the box, with no spaces.
equals sign
One of them puts a value in the box.

Watch out

Try it

  1. The box coins holds 3. What does it hold after coins = coins + 1?
  2. The box coins holds 3. What does it hold after coins = coins - 1?
  3. The box coins holds 3. What does it hold after the line coins + 1 on its own?
  4. Is my coins a good name for a box? Why?
  5. True or false: a variable can only hold a number.
Answers: 1. 4 · 2. 2 · 3. 3 · 4. No, it has a space · 5. False
Try at homeLabel three boxes or jars with names like pencils, socks and coins. Put a value in each, like seven pencils. Then ask a grown-up to change one value, and say the new line out loud, like: pencils equals pencils plus one.
For grown-upsPlay the Mission together: label three jars or boxes with names like pencils, socks and coins, put a value in each, and ask your child to say the new line out loud when you change one.
Open these notes on screenScan the code, or go to tutorpip.com.