AlgoBazaAlgoBaza
Python for kids ยท Module 1

Lesson 5

Division: / // %

After this lesson: You'll divide numbers in three ways: exactly, without remainder, and get just the remainder.

Video

?

The video for this lesson is coming soon.

Explanation

?

I have 17 candies and 5 friends. How many does each get? And how many are left for me? Python has three kinds of division for this!

Three kinds of division

SignWhat it calculates17 ... 5
/exact division (with a decimal point)17 / 5 = 3.4
//how many each gets (no remainder)17 // 5 = 3
%how many are left (the remainder)17 % 5 = 2

In a moment we'll share 17 candies among 5 friends and you'll see all three: each will get 3 - that's 17 // 5. 2 will be left - that's 17 % 5. And if we could cut candies into pieces, each would get 3.4 - that's 17 / 5.

Bajtek shares his candies ๐ŸŽฌ

Click Next to go step by step. Watch the candies fly to the plates - this story shows all three divisions: //, % and /.

?Bajtek
Fair shares!
๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ
17 ๐Ÿฌ
๐Ÿง’
๐Ÿ‘ง
๐Ÿ‘ฆ
๐Ÿ‘ฑ
๐Ÿง‘
1

17 candies, 5 friends

Bajtek has 17 candies in a bowl and 5 friends came to visit. He wants to give everyone the same amount. How?

Step 1 / 10

Now you share the candies

Click the arrow: in every round each friend gets one candy. When there are fewer candies than friends left - we stop.

To share: 17

๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
click the arrow - everyone gets one more

The candies in code

The same as in the animation: // says how many each gets, and % how many are left. The third line shows how many it would be if we cut the candies.

Code
1print("Each gets:", 17 // 5)
2print("Left for me:", 17 % 5)
3print("If we cut them:", 17 / 5)
Screen
โ–
1/4 ยท line 1 runs next

Three divisions in a program

The same numbers, three different results.

Code
1print(17 / 5)
2print(17 // 5)
3print(17 % 5)
Screen
โ–
1/4 ยท line 1 runs next

Pizza for 6 people ๐Ÿ•

We share 20 slices of pizza among 6 people.

To share: 20

๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•๐Ÿ•
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
click the arrow - everyone gets one more

The pizza in code ๐Ÿ•

The same numbers as in the animation: each gets 3 slices, and 2 are left. With / Python prints lots of digits after the point, because the pieces don't divide evenly - that's normal. The last digit can look a bit odd: that's how computers store fractions.

Code
1print("Each gets:", 20 // 6)
2print("Left over:", 20 % 6)
3print("If we cut them:", 20 / 6)
Screen
โ–
1/4 ยท line 1 runs next

Chocolate for 5 people ๐Ÿซ

We share 24 pieces of chocolate among 5 people.

To share: 24

๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ๐Ÿซ
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
๐Ÿง‘
0
click the arrow - everyone gets one more

A chocolate bar ๐Ÿซ

24 pieces for 5 people: how many each, how many are left and how many if we could cut pieces.

Code
1print("Each gets:", 24 // 5)
2print("Left over:", 24 % 5)
3print("With cutting:", 24 / 5)
Screen
โ–
1/4 ยท line 1 runs next

Division with a decimal point

The result of / always has a decimal point - even when it divides evenly: 20 / 4 is 5.0, not 5. Such a number with a point is a different kind of number for Python (we call it a float). If a task wants a result without a point, use //.

The point is always there

Code
1print(20 / 4)
2print(20 // 4)
3print(7 / 2)
4print(1 / 4)
Screen
โ–
1/5 ยท line 1 runs next

When nothing is left

12 candies for 4 friends - they divide evenly, the remainder is 0.

To share: 12

๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
click the arrow - everyone gets one more

12 candies in code

It divides evenly, but / still gives a point: 3.0.

Code
1print("Each gets:", 12 // 4)
2print("Left over:", 12 % 4)
3print("If we cut them:", 12 / 4)
Screen
โ–
1/4 ยท line 1 runs next

Not enough candies

3 candies and 5 friends - you can't even give one each. Everyone gets 0, and all 3 are left.

To share: 3

๐Ÿฌ๐Ÿฌ๐Ÿฌ
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0
๐Ÿง’
0

3 // 5 = 0 (what each one gets)

3 % 5 = 3 (what's left over - the remainder)

3 left - not enough for each of the 5 to get one more

3 candies in code

Too few to give everyone even one: each gets 0, and all 3 are left.

Code
1print("Each gets:", 3 // 5)
2print("Left over:", 3 % 5)
3print("If we cut them:", 3 / 5)
Screen
โ–
1/4 ยท line 1 runs next

Splitting the bill ๐Ÿ’ธ

Money can be divided exactly - then we use /.

โ€ฆ
?

Click the arrow - we'll split the bill equally.

Code
1print(10 / 4)
2print(30 / 4)
3print(9 / 3)
Screen
โ–
1/4 ยท line 1 runs next

What is the remainder % good for?

The remainder is very useful:

  • Even or odd? A number is even when number % 2 gives 0 (it divides by 2 with no remainder).
  • A clock: 135 minutes is 135 // 60 = 2 hours and 135 % 60 = 15 minutes.
  • The last digit: 2026 % 10 is 6, and 2026 // 10 is 202 - the number without its last digit.

Even or odd?

Remainder 0 - even, remainder 1 - odd.

โ€ฆ
?

Click the arrow - we'll put every number into pairs.

Code
1print(24, "->", 24 % 2)
2print(37, "->", 37 % 2)
3print(100, "->", 100 % 2)
4print(1, "->", 1 % 2)
Screen
โ–
1/5 ยท line 1 runs next

Bajtek's birthday at school ๐ŸŽ‚

Click Next to go step by step. Watch the sweets fly from the bag to the friends' plates - and how many stay in the bag.

Bajtek ๐ŸŽ‚
It's my birthday! ๐ŸŽ‚
๐Ÿ‘œ Bajtek's bag
๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ
23 ๐Ÿฌ
๐Ÿง’
๐Ÿ‘ง
๐Ÿ‘ฆ
๐Ÿ‘ฑ
๐Ÿง‘
1

Bajtek's birthday

It's Bajtek's birthday! He brought a bag with 23 sweets to school and wants to share them with 5 friends. Everyone should get the same amount. Bajtek wants to know: how many sweets will be left in his bag?

Step 1 / 8

How many sweets should Mum give? ๐Ÿ›๏ธ

Bajtek wants as many sweets as possible left in his bag after sharing with 5 friends. He checks different numbers of sweets in Python: after the arrow is what stays in his bag.

๐Ÿ‘œ โ€ฆ
?

Click the arrow - Bajtek will check different numbers of sweets. Every group of 5 sweets is one round for 5 friends, and what doesn't fill a group stays in the bag.

Code
1print(20, "->", 20 % 5)
2print(21, "->", 21 % 5)
3print(22, "->", 22 % 5)
4print(23, "->", 23 % 5)
5print(24, "->", 24 % 5)
6print(25, "->", 25 % 5)
Screen
โ–
1/7 ยท line 1 runs next

The most are left with 24 sweets - 4 for me! ๐ŸŽ‰ And with 25 nothing is left, because 25 divides by 5 with no remainder. The remainder of dividing by 5 is always less than 5: 0, 1, 2, 3 or 4. Mum, give me 24! ๐Ÿ›๏ธ

?

What does print(10 // 3) print?

?

Which number is odd?

print(8 % 2)
print(9 % 2)
print(10 % 2)
?

What does print(2026 // 10) print?

Don't divide by zero!

print(5 / 0) stops the program with a ZeroDivisionError - you can't divide by zero, just like in maths. The same goes for // and %.

Dividing by zero

The program prints the first line and stops on the second.

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

Apples in baskets

We put 23 apples equally into 4 baskets. ๐ŸŽ

To share: 23

๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ
๐Ÿงบ
0
๐Ÿงบ
0
๐Ÿงบ
0
๐Ÿงบ
0
click the arrow - everyone gets one more

The apples in code

Code
1print("In each basket:", 23 // 4)
2print("Left over:", 23 % 4)
3print("If we cut them:", 23 / 4)
Screen
โ–
1/4 ยท line 1 runs next

How many in each basket, how many left?

23 // 4 * 4 + 23 % 4
1/4 ยท click the arrow - Python works out the next part

Shoes in pairs ๐Ÿ‘Ÿ

We pair up 9 shoes - that is, divide by 2. One will be left without a pair!

To share: 9

๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ
๐Ÿ‘ฃ
0
๐Ÿ‘ฃ
0
click the arrow - everyone gets one more

The shoes in code ๐Ÿ‘Ÿ

๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ๐Ÿ‘Ÿ
?

Click the arrow - we have 9 shoes to sort into pairs.

Code
1print("Pairs:", 9 // 2)
2print("Without a pair:", 9 % 2)
3print("Exactly:", 9 / 2)
Screen
โ–
1/4 ยท line 1 runs next

Digits of a number: % 10 and // 10

This is a very common contest task! Let's start with the simplest part: how do we get the last digit of a number?

Just take the remainder of dividing by 10: 472 % 10 is 2.

Why? 472 is 47 full tens and 2 more. Those 2 don't make another ten - they are left over as the remainder. And that's exactly the last digit!

The last digit of 472

% 10 is the remainder of dividing by 10.

472 % 10
1/2 ยท click the arrow - Python works out the next part

The last digit in code

For any number % 10 gives its last digit - even for a 0 at the end and for a one-digit number.

โ€ฆ
?

Click the arrow - we'll take the last digit out of every number.

Code
1print("Last digit of 472:", 472 % 10)
2print("Last digit of 2026:", 2026 % 10)
3print("Last digit of 150:", 150 % 10)
4print("Last digit of 7:", 7 % 10)
Screen
โ–
1/5 ยท line 1 runs next

And how do we cut the last digit off? That's // 10: 472 // 10 is 47 - the number without its last digit (47 full tens).

Cutting the last digit off

โ€ฆ
?

Click the arrow - we'll cut the last digit off every number.

Code
1print("472 without its last digit:", 472 // 10)
2print("2026 without its last digit:", 2026 // 10)
3print("7 without its last digit:", 7 // 10)
Screen
โ–
1/4 ยท line 1 runs next

A clock: minutes to hours

A film lasts 135 minutes. How many hours and minutes is that? An hour has 60 minutes, so:

  • full hours: 135 // 60 = 2,
  • minutes left over: 135 % 60 = 15.

The film lasts 2 hours and 15 minutes. ๐ŸŽฌ

A film in hours and minutes

minutes: 135
135 // 60
?
135 % 60
?
?

We have 135 minutes. Click the arrow - let's see how many full hours fit in.

Code
1print(135 // 60, "h")
2print(135 % 60, "min")
Screen
โ–
1/3 ยท line 1 runs next
?

What is 7 % 2?

?

Which operation gives the last digit of 359?

Seconds to minutes โฑ๏ธ

200 seconds: a minute has 60 seconds.

seconds: 200
200 // 60
?
200 % 60
?
?

We have 200 seconds. Click the arrow - let's see how many full minutes fit in.

Code
1print(200 // 60, "min")
2print(200 % 60, "s")
Screen
โ–
1/3 ยท line 1 runs next

How many weeks to Christmas? ๐ŸŽ„

There are 78 days left to Christmas Eve. A week has 7 days.

days: 78
78 // 7
?
78 % 7
?
?

We have 78 days. Click the arrow - let's see how many full weeks fit in.

Code
1print("Weeks:", 78 // 7)
2print("Plus days:", 78 % 7)
Screen
โ–
1/3 ยท line 1 runs next

Remember

  • / divides exactly and always gives a number with a point.
  • // says how many each gets (no remainder).
  • % says how many are left (the remainder).
  • % 2 checks even/odd, % 10 gives the last digit.
  • You can't divide by zero.

Now you share the candies! In the tasks pick the right kind of division.

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.