Lesson 12
Changing a variable
After this lesson: You'll change a variable's value: increase it, decrease it and swap two variables.
Video
The video for this lesson is coming soon.
Explanation
In a game you collect points - the score keeps growing. See how to change what's in the box!
A new value throws out the old one
You can put a new value into a box at any time. Then the old value disappears - a box holds only one thing at a time:
Replacing the contents
Watch the box x: every new number drops in and the old one falls out. A box holds only one thing.
Click the arrow and watch the values move between the boxes.
1x = 52print(x)3x = 84print(x)5x = 1006print(x)
โThe weather during the day โ๏ธ๐ง๏ธ
One box, three different values. Watch the variables panel - the old value disappears.
Click the arrow and watch the values move between the boxes.
1weather = "sun"2print("Morning:", weather)3weather = "rain"4print("Noon:", weather)5weather = "snow"6print("Evening:", weather)
โA backpack for a trip ๐
Two boxes: food and drink. We change only one of them - watch whether the other one changes too.
Click the arrow and watch the values move between the boxes.
1food = "sandwich"2drink = "water"3print("Food:", food)4print("Drink:", drink)5food = "apple"6print("Food:", food)7print("Drink:", drink)
โDid you notice? When the apple went into the box food, the box drink still had the water. Every variable is a separate box - changing one doesn't touch the other.
A match: two teams' goals โฝ
Two boxes for the match score. After every goal a new number goes into the team's box - and the old one falls out.
Click the arrow and watch the values move between the boxes.
1red = 02blue = 03print("Red:", red, "Blue:", blue)4red = 15print("Red:", red, "Blue:", blue)6blue = 17print("Red:", red, "Blue:", blue)8blue = 29print("Red:", red, "Blue:", blue)
โx = x + 1
In maths x = x + 1 would be impossible - no number equals itself plus 1! ๐ค But in Python the = sign doesn't mean "is equal to". It means "put into the box".
See how Python reads x = x + 1 when the box x holds 5:
- First it looks at the right side:
x + 1. - It looks into the box x - there's 5 in it.
- It calculates: 5 + 1 = 6.
- It puts 6 into the box x. The old five falls out - a box holds only one thing.
Read it like this: "the new x is the old x plus 1". It's like a points counter in a game: after every point the score goes up by one.
coins = coins + 1 step by step ๐ช
A coin hangs above Bajtek. When coins = coins + 1 runs, Bajtek jumps, grabs it - and the coin disappears! The box coins held 5, and now it holds 6: the new coins are the old coins plus 1.
1coins = 52print("Before the jump:", coins)3coins = coins + 14print("After the jump:", coins)
โGame points
You collect coins worth 10 points and a star worth 5. After every change we print the score - watch it grow: 0, 10, 20, 25.
1points = 02print("Start:", points)3points = points + 104print("After the first coin:", points)5points = points + 106print("After the second coin:", points)7points = points + 58print("After the star:", points)
โLives and coins ๐ฎ
1lives = 32lives = lives - 13print("Lives after a mistake:", lives)4coins = 105coins = coins + 56print("Coins after a treasure:", coins)7coins = coins * 28print("Coins after a x2 bonus:", coins)
โPiggy bank
A real story: you deposit every week and sometimes spend.
1piggy_bank = 202print("At the start:", piggy_bank)3# deposit pocket money4piggy_bank = piggy_bank + 155print("After pocket money:", piggy_bank)6# buy ice cream7piggy_bank = piggy_bank - 88print("After ice cream:", piggy_bank)9# grandma doubles the savings10piggy_bank = piggy_bank * 211print("After grandma's gift:", piggy_bank)
โA birthday every year ๐
The age grows by 1 - each time we take the old value and add 1.
1age = 92age = age + 13print(age)4age = age + 15print(age)6age = age + 17print(age)
โBaubles for the tree ๐
Everyone in the family hangs their baubles: mum 5, dad 3, me 7. After each turn we count the baubles on the tree: 5, 8, 15.
1baubles = 02baubles = baubles + 53print("Mum hung 5, on the tree:", baubles)4baubles = baubles + 35print("Dad hung 3, on the tree:", baubles)6baubles = baubles + 77print("I hung 7, on the tree:", baubles)
โA snail on a wall ๐
During the day it climbs 3 cm up, at night it slides 1 cm down. Where will it be after 3 days?
1height = 02height = height + 33print("Day 1:", height)4height = height - 15print("Night 1:", height)6height = height + 37print("Day 2:", height)8height = height - 19print("Night 2:", height)10height = height + 311print("Day 3:", height)
โA magic bean ๐ฑ
The bean doubles its height every night: bean = bean * 2. Watch how fast it grows: 2, 4, 8, 16.
1bean = 12bean = bean * 23print("Night 1:", bean)4bean = bean * 25print("Night 2:", bean)6bean = bean * 27print("Night 3:", bean)8bean = bean * 29print("Night 4:", bean)
โSwapping two variables
Want to swap what's in the boxes a and b? If you wrote a = b, the old contents of a would be gone and you'd have nothing to put into b. You need a third box:
- put a aside in a helper box
temp, - put b into a,
- put what you set aside in
tempinto b.
It's like swapping drinks between two cups - you need a third, helper cup.
Two cups ๐ฅค
To swap juice and water, you need a third cup - a helper. It holds the juice for a moment.
Click the arrow and watch the values move between the boxes.
1cup1 = "juice"2cup2 = "water"3helper = cup14cup1 = cup25cup2 = helper6print(cup1, cup2)
โWrong - without a third cup
After cup1 = cup2 the juice is lost forever!
Click the arrow and watch the values move between the boxes.
1cup1 = "juice"2cup2 = "water"3cup1 = cup24cup2 = cup15print(cup1, cup2)
โSwapping with a helper box
Now the same with numbers: the box temp is our helper cup. First we print a and b before the swap, and at the end after it - compare both lines.
Click the arrow and watch the values move between the boxes.
1a = 12b = 23print("Before the swap:", a, b)4temp = a5a = b6b = temp7print("After the swap:", a, b)
โSwapping without a helper - doesn't work!
After a = b both boxes hold 2 - the one is lost forever.
Click the arrow and watch the values move between the boxes.
1a = 12b = 23a = b4b = a5print(a, b)
โWhat will this program print?
x = 2x = x + 3x = x * 2print(x)
What will this program print?
x = 10x = 3print(x)
Common mistakes
x + 1withoutx =- Python calculates the result and throws it away, x doesn't change.- Swapping with
a = b,b = awithout a helper variable - both variables end up the same.
A score counter in a game
The player collects coins: +10, +10, +50 for a treasure, then loses 20 to a trap. Watch the points box change. ๐ฎ
1points = 02points = points + 103print("After the first coin:", points)4points = points + 105print("After the second coin:", points)6points = points + 507print("After the treasure:", points)8points = points - 209print("After the trap:", points)
โA growing plant
The plant is 3 cm tall and grows 2 cm every day. After the third day we double it with fertiliser. ๐ฑ
1height = 32height = height + 23print("Day 1:", height)4height = height + 25print("Day 2:", height)6height = height + 27print("Day 3:", height)8height = height * 29print("With fertiliser:", height)
โWhy does // 10 cut off the last digit? Imagine you're packing sweets into packs of 10. ๐ฌ
You have 47 sweets. That makes 4 full packs (40 sweets) and 7 loose sweets. Look: 4 and 7 are exactly the digits of 47!
47 // 10says how many full packs there are: 4 - the number without its last digit,47 % 10says how many are left loose: 7 - the last digit.
The last digit is always left loose, because it's smaller than 10 - not enough for a new pack. And // counts only full packs, so it skips it. โ๏ธ
Packs of 10 ๐ฌ
The same number of sweets: how many full packs and how many loose.
1print("Full packs:", 47 // 10)2print("Loose:", 47 % 10)
โ2026 // 10 step by step
Cutting off digits
x = x // 10 cuts off the last digit. Three times - and 2026 becomes 2.
Click the arrow - every x = x // 10 cuts one digit off the end.
1x = 20262x = x // 103print(x)4x = x // 105print(x)6x = x // 107print(x)
โWhat does the program print?
a = 4a = a * 3a = a - 2print(a)
What does the program print?
a = 5b = aa = 9print(b)
Remember
- A new value in the box throws out the old one.
x = x + 1- the new x is the old x plus 1.- Swapping two variables needs a third, helper variable.
Now you change what's in the boxes. Let's go!
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.