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
| Sign | What it calculates | 17 ... 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 /.
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?
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
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.
1print("Each gets:", 17 // 5)2print("Left for me:", 17 % 5)3print("If we cut them:", 17 / 5)
โThree divisions in a program
The same numbers, three different results.
1print(17 / 5)2print(17 // 5)3print(17 % 5)
โPizza for 6 people ๐
We share 20 slices of pizza among 6 people.
To share: 20
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.
1print("Each gets:", 20 // 6)2print("Left over:", 20 % 6)3print("If we cut them:", 20 / 6)
โChocolate for 5 people ๐ซ
We share 24 pieces of chocolate among 5 people.
To share: 24
A chocolate bar ๐ซ
24 pieces for 5 people: how many each, how many are left and how many if we could cut pieces.
1print("Each gets:", 24 // 5)2print("Left over:", 24 % 5)3print("With cutting:", 24 / 5)
โ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
1print(20 / 4)2print(20 // 4)3print(7 / 2)4print(1 / 4)
โWhen nothing is left
12 candies for 4 friends - they divide evenly, the remainder is 0.
To share: 12
12 candies in code
It divides evenly, but / still gives a point: 3.0.
1print("Each gets:", 12 // 4)2print("Left over:", 12 % 4)3print("If we cut them:", 12 / 4)
โ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
3 // 5 = 0 (what each one gets)
3 % 5 = 3 (what's left over - the remainder)
3 candies in code
Too few to give everyone even one: each gets 0, and all 3 are left.
1print("Each gets:", 3 // 5)2print("Left over:", 3 % 5)3print("If we cut them:", 3 / 5)
โSplitting the bill ๐ธ
Money can be divided exactly - then we use /.
Click the arrow - we'll split the bill equally.
1print(10 / 4)2print(30 / 4)3print(9 / 3)
โWhat is the remainder % good for?
The remainder is very useful:
- Even or odd? A number is even when
number % 2gives 0 (it divides by 2 with no remainder). - A clock: 135 minutes is
135 // 60= 2 hours and135 % 60= 15 minutes. - The last digit:
2026 % 10is 6, and2026 // 10is 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.
1print(24, "->", 24 % 2)2print(37, "->", 37 % 2)3print(100, "->", 100 % 2)4print(1, "->", 1 % 2)
โ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'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?
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.
1print(20, "->", 20 % 5)2print(21, "->", 21 % 5)3print(22, "->", 22 % 5)4print(23, "->", 23 % 5)5print(24, "->", 24 % 5)6print(25, "->", 25 % 5)
โ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.
1print(10 / 2)2print(10 / 0)3print(10 / 5)
โApples in baskets
We put 23 apples equally into 4 baskets. ๐
To share: 23
The apples in code
1print("In each basket:", 23 // 4)2print("Left over:", 23 % 4)3print("If we cut them:", 23 / 4)
โHow many in each basket, how many left?
Shoes in pairs ๐
We pair up 9 shoes - that is, divide by 2. One will be left without a pair!
To share: 9
The shoes in code ๐
Click the arrow - we have 9 shoes to sort into pairs.
1print("Pairs:", 9 // 2)2print("Without a pair:", 9 % 2)3print("Exactly:", 9 / 2)
โ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.
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.
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)
โ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.
1print("472 without its last digit:", 472 // 10)2print("2026 without its last digit:", 2026 // 10)3print("7 without its last digit:", 7 // 10)
โ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
We have 135 minutes. Click the arrow - let's see how many full hours fit in.
1print(135 // 60, "h")2print(135 % 60, "min")
โWhat is 7 % 2?
Which operation gives the last digit of 359?
Seconds to minutes โฑ๏ธ
200 seconds: a minute has 60 seconds.
We have 200 seconds. Click the arrow - let's see how many full minutes fit in.
1print(200 // 60, "min")2print(200 % 60, "s")
โHow many weeks to Christmas? ๐
There are 78 days left to Christmas Eve. A week has 7 days.
We have 78 days. Click the arrow - let's see how many full weeks fit in.
1print("Weeks:", 78 // 7)2print("Plus days:", 78 % 7)
โ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).% 2checks even/odd,% 10gives 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.