Division and Remainders
Division splits an amount into equal parts, or asks how many times one number fits into another. When it does not fit exactly, something is left over: the remainder. Programmers use remainders far more than most people expect, from paging through search results to deciding whose turn it is in a game.
Words and symbols
In , the number being divided (20) is the dividend, the number you divide by (4) is the divisor, and the result (5) is the quotient. Division is also written as a fraction, , or with a slash in code: 20 / 4.
Two ways to picture :
- Sharing: 20 sweets shared fairly between 4 children gives each child 5.
- Grouping: how many groups of 4 can you make from 20? Five groups.
Division undoes multiplication: because . That is how you check a division.
When it doesn't divide evenly
23 sweets shared between 4 children: each gets 5 (that uses 20) and 3 are left. We write
This division equation is the key fact of the whole lesson:
The remainder is always smaller than the divisor. If it were 4 or more, every child could get another sweet.
Python gives the quotient with //, the remainder with %, and both at once with divmod:
a, b = 23, 4
q, r = a // b, a % b
print("quotient:", q, " remainder:", r)
print("divmod:", divmod(a, b))
print("check:", b * q + r == a)
print("exact answer:", a / b)quotient: 5 remainder: 3
divmod: (5, 3)
check: True
exact answer: 5.75
The exact answer 5.75 is the quotient plus the remainder shared out too: .
Long division
For bigger numbers, long division works through the dividend one digit at a time, from the left: divide, multiply, subtract, bring down the next digit, repeat.
Divide 857 by 6:
8 ÷ 6 = 1, 1 × 6 = 6, 8 − 6 = 2, bring down 5 → 25
25 ÷ 6 = 4, 4 × 6 = 24, 25 − 24 = 1, bring down 7 → 17
17 ÷ 6 = 2, 2 × 6 = 12, 17 − 12 = 5, no digits left
quotient 142, remainder 5 check: 6 × 142 + 5 = 852 + 5 = 857 ✓The same steps in code. Each step keeps a running remainder, multiplies it by 10 and adds the next digit, which is exactly "bring down":
def long_division(dividend, divisor):
quotient_digits, rem = [], 0
for ch in str(dividend):
current = rem * 10 + int(ch) # bring down the next digit
digit = current // divisor
rem = current - digit * divisor
quotient_digits.append(str(digit))
print(f"{current:3d} / {divisor} = {digit}, remainder {rem}")
return int("".join(quotient_digits)), rem
print("result:", long_division(857, 6)) 8 / 6 = 1, remainder 2
25 / 6 = 4, remainder 1
17 / 6 = 2, remainder 5
result: (142, 5)
Dividing by zero
You can never divide by zero. would have to be a number that gives 12 when multiplied by 0, and no such number exists, because . Python refuses with an error, and so does every language for integers:
try:
print(12 / 0)
except ZeroDivisionError as e:
print("error:", e)
print(0 / 12) # zero divided by something is fineerror: division by zero
0.0
Zero divided by a number is fine: sharing 0 sweets among 12 children gives each child 0.
Rounding the quotient up
Sometimes the leftover still needs a whole unit. 95 search results at 10 per page need 10 pages, not 9: the last 5 results need a page of their own. This is ceiling division. In Python, -(-a // b) or math.ceil(a / b) both work; the first stays exact for huge integers.
import math
results, per_page = 95, 10
print("full pages:", results // per_page, " leftover:", results % per_page)
print("pages needed:", -(-results // per_page), math.ceil(results / per_page))
print("buses for 130 people, 48 seats each:", -(-130 // 48))full pages: 9 leftover: 5
pages needed: 10 10
buses for 130 people, 48 seats each: 3
Note: With negative numbers, Python's
//rounds down toward :-7 // 2is-4, and-7 % 2is1, so that the division equation still holds (). C and Java round toward zero instead (−3 remainder −1). The Modular Arithmetic lesson returns to this.
In software
- Pagination and batching: number of pages = ceiling of items page size.
- Even and odd:
n % 2 == 0tests evenness;i % 3 == 0does something every third time round a loop. - Turn order and wrap-around: in a 4-player game, the player after player
pis(p + 1) % 4. An animation with 8 frames shows frametick % 8. - Hash tables place a key in bucket
hash % number_of_buckets(see Hash Tables). - Unit conversion: seconds to minutes and seconds is a
divmodby 60.
Exercises
Exercise 1: Quotient and remainder
Find as a quotient and remainder, and write the division equation.
Answer
14 remainder 2, because .
Solution
is the largest multiple of 7 not above 100, and . The remainder 2 is less than 7, as it must be.
Exercise 2: Long division
Divide 4 738 by 9 with long division.
Answer
526 remainder 4
Solution
4 ÷ 9 = 0, remainder 4, bring down 7 → 47
47 ÷ 9 = 5, 45, remainder 2, bring down 3 → 23
23 ÷ 9 = 2, 18, remainder 5, bring down 8 → 58
58 ÷ 9 = 6, 54, remainder 4
quotient 0526 = 526, remainder 4; check 9 × 526 + 4 = 4 734 + 4 = 4 738 ✓print(divmod(4738, 9))(526, 4)
Exercise 3: Boxes
Eggs are packed in boxes of 12. How many boxes are needed for 250 eggs, and how many eggs are in the last box?
Answer
21 boxes; the last box holds 10 eggs.
Solution
. Twenty boxes are full and the 10 leftover eggs need a 21st box.
Exercise 4: Whose turn?
Five players sit in a circle numbered 0 to 4, and player 0 starts. Write code that prints who plays turns 0 to 11 using %. Who plays turn 11?
Answer
Player 1 plays turn 11 ().
Solution
Turn belongs to player : after player 4 it wraps back to 0.
players = 5
print([turn % players for turn in range(12)])[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1]
Exercise 5: Time conversion
Convert 10 000 seconds into hours, minutes and seconds using divmod twice.
Answer
2 h 46 min 40 s
Solution
minutes, seconds = divmod(10000, 60)
hours, minutes = divmod(minutes, 60)
print(hours, "h", minutes, "min", seconds, "s")2 h 46 min 40 s
Test yourself
What is 47 % 6 in Python?
- 7
- 5
- 1
- 7.83
A ferry carries 35 cars per trip. How many trips are needed for 140 cars? For 141 cars?
- 4 and 4
- 4 and 5
- 5 and 5
- 3 and 4
Which statement about division by zero is correct?
- is undefined, but
- Both and are undefined