AlgoBazaAlgoBaza
Python for kids ยท Module 1

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.

not yet
๐Ÿ“ฆ x

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

Code
1x = 5
2print(x)
3x = 8
4print(x)
5x = 100
6print(x)
Screen
โ–
1/7 ยท line 1 runs next

The weather during the day โ˜€๏ธ๐ŸŒง๏ธ

One box, three different values. Watch the variables panel - the old value disappears.

not yet
๐Ÿ“ฆ weather

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

Code
1weather = "sun"
2print("Morning:", weather)
3weather = "rain"
4print("Noon:", weather)
5weather = "snow"
6print("Evening:", weather)
Screen
โ–
1/7 ยท line 1 runs next

A backpack for a trip ๐ŸŽ’

Two boxes: food and drink. We change only one of them - watch whether the other one changes too.

not yet
๐Ÿ“ฆ food
not yet
๐Ÿ“ฆ drink

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

Code
1food = "sandwich"
2drink = "water"
3print("Food:", food)
4print("Drink:", drink)
5food = "apple"
6print("Food:", food)
7print("Drink:", drink)
Screen
โ–
1/8 ยท line 1 runs next

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.

not yet
๐Ÿ“ฆ red
not yet
๐Ÿ“ฆ blue

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

Code
1red = 0
2blue = 0
3print("Red:", red, "Blue:", blue)
4red = 1
5print("Red:", red, "Blue:", blue)
6blue = 1
7print("Red:", red, "Blue:", blue)
8blue = 2
9print("Red:", red, "Blue:", blue)
Screen
โ–
1/10 ยท line 1 runs next

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:

  1. First it looks at the right side: x + 1.
  2. It looks into the box x - there's 5 in it.
  3. It calculates: 5 + 1 = 6.
  4. 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.

โ˜๏ธ
๐Ÿช™
๐Ÿ“ฆ coins
๐Ÿช™โ€ฆ
Code
1coins = 5
2print("Before the jump:", coins)
3coins = coins + 1
4print("After the jump:", coins)
Screen
โ–
1/5 ยท line 1 runs next

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.

๐Ÿ† points: โ€ฆ
โ˜๏ธโ˜๏ธ
๐Ÿ๐Ÿช™๐Ÿช™โญ๐Ÿš—
Code
1points = 0
2print("Start:", points)
3points = points + 10
4print("After the first coin:", points)
5points = points + 10
6print("After the second coin:", points)
7points = points + 5
8print("After the star:", points)
Screen
โ–
1/9 ยท line 1 runs next

Lives and coins ๐ŸŽฎ

๐Ÿ“ฆ lives
โค๏ธโ€ฆ
๐Ÿ“ฆ coins
๐Ÿช™โ€ฆ
Code
1lives = 3
2lives = lives - 1
3print("Lives after a mistake:", lives)
4coins = 10
5coins = coins + 5
6print("Coins after a treasure:", coins)
7coins = coins * 2
8print("Coins after a x2 bonus:", coins)
Screen
โ–
1/9 ยท line 1 runs next

Piggy bank

A real story: you deposit every week and sometimes spend.

๐Ÿ“ฆ piggy_bank
๐Ÿช™โ€ฆ
Code
1piggy_bank = 20
2print("At the start:", piggy_bank)
3# deposit pocket money
4piggy_bank = piggy_bank + 15
5print("After pocket money:", piggy_bank)
6# buy ice cream
7piggy_bank = piggy_bank - 8
8print("After ice cream:", piggy_bank)
9# grandma doubles the savings
10piggy_bank = piggy_bank * 2
11print("After grandma's gift:", piggy_bank)
Screen
โ–
1/9 ยท line 1 runs next

A birthday every year ๐ŸŽ‚

The age grows by 1 - each time we take the old value and add 1.

๐Ÿ“ฆ ?age ยท 0 candles
Code
1age = 9
2age = age + 1
3print(age)
4age = age + 1
5print(age)
6age = age + 1
7print(age)
Screen
โ–
1/8 ยท line 1 runs next

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.

โญ
๐Ÿ“ฆ ?baubles ยท 0 baubles
Code
1baubles = 0
2baubles = baubles + 5
3print("Mum hung 5, on the tree:", baubles)
4baubles = baubles + 3
5print("Dad hung 3, on the tree:", baubles)
6baubles = baubles + 7
7print("I hung 7, on the tree:", baubles)
Screen
โ–
1/8 ยท line 1 runs next

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?

โ˜€๏ธ
โ˜€๏ธ height: โ€ฆ cm
0 cm
1 cm
2 cm
3 cm
4 cm
5 cm
6 cm
7 cm
8 cm
๐ŸŒ
Code
1height = 0
2height = height + 3
3print("Day 1:", height)
4height = height - 1
5print("Night 1:", height)
6height = height + 3
7print("Day 2:", height)
8height = height - 1
9print("Night 2:", height)
10height = height + 3
11print("Day 3:", height)
Screen
โ–
1/12 ยท line 1 runs next

A magic bean ๐ŸŒฑ

The bean doubles its height every night: bean = bean * 2. Watch how fast it grows: 2, 4, 8, 16.

๐ŸŒ™โœฆโœฆโœฆ
๐ŸŒ™ bean: โ€ฆ cm
0 cm
2 cm
4 cm
6 cm
8 cm
10 cm
12 cm
14 cm
16 cm
Code
1bean = 1
2bean = bean * 2
3print("Night 1:", bean)
4bean = bean * 2
5print("Night 2:", bean)
6bean = bean * 2
7print("Night 3:", bean)
8bean = bean * 2
9print("Night 4:", bean)
Screen
โ–
1/10 ยท line 1 runs next

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:

  1. put a aside in a helper box temp,
  2. put b into a,
  3. put what you set aside in temp into 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.

not yet
๐Ÿฅค cup1
not yet
๐Ÿฅค cup2
not yet
๐Ÿฅค helper

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

Code
1cup1 = "juice"
2cup2 = "water"
3helper = cup1
4cup1 = cup2
5cup2 = helper
6print(cup1, cup2)
Screen
โ–
1/7 ยท line 1 runs next

Wrong - without a third cup

After cup1 = cup2 the juice is lost forever!

not yet
๐Ÿฅค cup1
not yet
๐Ÿฅค cup2

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

Code
1cup1 = "juice"
2cup2 = "water"
3cup1 = cup2
4cup2 = cup1
5print(cup1, cup2)
Screen
โ–
1/6 ยท line 1 runs next

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.

not yet
๐Ÿ“ฆ a
not yet
๐Ÿ“ฆ b
not yet
๐Ÿ“ฆ temp

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

Code
1a = 1
2b = 2
3print("Before the swap:", a, b)
4temp = a
5a = b
6b = temp
7print("After the swap:", a, b)
Screen
โ–
1/8 ยท line 1 runs next

Swapping without a helper - doesn't work!

After a = b both boxes hold 2 - the one is lost forever.

not yet
๐Ÿ“ฆ a
not yet
๐Ÿ“ฆ b

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

Code
1a = 1
2b = 2
3a = b
4b = a
5print(a, b)
Screen
โ–
1/6 ยท line 1 runs next
?

What will this program print?

x = 2
x = x + 3
x = x * 2
print(x)
?

What will this program print?

x = 10
x = 3
print(x)

Common mistakes

  • x + 1 without x = - Python calculates the result and throws it away, x doesn't change.
  • Swapping with a = b, b = a without 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. ๐ŸŽฎ

๐Ÿ† points: โ€ฆ
โ˜๏ธโ˜๏ธ
๐Ÿ๐Ÿช™๐Ÿช™๐Ÿ’Ž๐Ÿ•ณ๏ธ๐Ÿš—
Code
1points = 0
2points = points + 10
3print("After the first coin:", points)
4points = points + 10
5print("After the second coin:", points)
6points = points + 50
7print("After the treasure:", points)
8points = points - 20
9print("After the trap:", points)
Screen
โ–
1/10 ยท line 1 runs next

A growing plant

The plant is 3 cm tall and grows 2 cm every day. After the third day we double it with fertiliser. ๐ŸŒฑ

Code
1height = 3
2height = height + 2
3print("Day 1:", height)
4height = height + 2
5print("Day 2:", height)
6height = height + 2
7print("Day 3:", height)
8height = height * 2
9print("With fertiliser:", height)
Screen
โ–
1/10 ยท line 1 runs next
?

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 // 10 says how many full packs there are: 4 - the number without its last digit,
  • 47 % 10 says 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.

Code
1print("Full packs:", 47 // 10)
2print("Loose:", 47 % 10)
Screen
โ–
1/3 ยท line 1 runs next

2026 // 10 step by step

2026 // 10
1/3 ยท click the arrow - Python works out the next part

Cutting off digits

x = x // 10 cuts off the last digit. Three times - and 2026 becomes 2.

๐Ÿ“ฆ x
not yet

Click the arrow - every x = x // 10 cuts one digit off the end.

Code
1x = 2026
2x = x // 10
3print(x)
4x = x // 10
5print(x)
6x = x // 10
7print(x)
Screen
โ–
1/8 ยท line 1 runs next
?

What does the program print?

a = 4
a = a * 3
a = a - 2
print(a)
?

What does the program print?

a = 5
b = a
a = 9
print(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.