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.
What coins = coins + 1 does.What each line does to coins when it holds 3.
Example 1: Add one
The box coins holds 3, and Zara types coins = coins + 1.
Python works out the right side first: 3 + 1 = 4.
It stores the 4 back in the same box. Print shows 4.
Example 2: Take one away
The box coins holds 3, and the line is coins = coins - 1.
The right side first: 3 - 1 = 2.
The box now holds 2.
Example 3: Put a new value in
The box coins holds 3, and the line is coins = 1.
The new value goes in, and the old three is gone.
The box now holds 1.
Example 4: Four lines that print six
The first line makes the box with coins = 2.
Then coins = coins + 1: 2 + 1 = 3.
Then coins = coins * 2: 3 * 2 = 6.
Last, print(coins) shows what is in the box, which is 6.
Make the box, add one, double it, print it.
Example 5: A name with a space
Leo names his box my coins, with a space, and Python refuses to read it.
A name has no spaces.
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
One equals sign puts a value in. Two equals signs ask whether two things are equal.
A new value replaces the old one, so the old value is gone.
The line coins + 1 works out four but stores nothing, so the box still holds three.
A name has no spaces, and a number cannot be the name of a box.
Try it
The box coins holds 3. What does it hold after coins = coins + 1?
The box coins holds 3. What does it hold after coins = coins - 1?
The box coins holds 3. What does it hold after the line coins + 1 on its own?
Is my coins a good name for a box? Why?
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.