🐍 Python for kids · Class 6 · Lesson 9 of 18 · Free
The Modulo Operator – Remainders That Do Magic
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Find the quotient with // and the remainder with %.
- Check if a number is even, odd or divisible by another number.
- Use % to pull out digits and to wrap numbers around like a clock.
You have 23 laddoos to share equally among 5 friends. Each friend gets 4, and 3 laddoos are left over. That leftover 3 is the remainder, and Python finds it with one symbol: %.
What is the modulo operator in Python?
The modulo operator is the % sign. It gives the remainder after a division.
Remember long division from Class 4? The remainder is the small number left at the bottom.
It has nothing to do with percent here. In Python, % between two numbers always means remainder.
Its partner is //, called floor division. // tells you how many whole times one number fits into another.
Use them together whenever you share things out equally. // gives each share, and % gives what is left.
▶laddoos = 232friends = 53each = laddoos // friends4left = laddoos % friends5print("Each friend gets", each)6print("Laddoos left over:", left)7print("Check:", each * friends + left)
No variables yet.
▌✏️ Change this code and run it yourself
Each friend gets 4 Laddoos left over: 3 Check: 23
💡 The check line always gives back the original number. That is a good way to test your answer.
How do I check if a number is even or odd in Python?
An even number divides by 2 with nothing left over. So n % 2 is 0 for even numbers.
For odd numbers, n % 2 is always 1. There are only two possible remainders when you divide by 2.
Notice that 0 is even. 0 % 2 is 0, because nothing is left over.
This is the most common use of %. You will see it in games, puzzles and real apps.
▶for n in [14, 7, 0, 31, 100]:2 if n % 2 == 0:3 print(n, "is even")4 else:5 print(n, "is odd")
No variables yet.
▌✏️ Change this code and run it yourself
14 is even 7 is odd 0 is even 31 is odd 100 is even
How do I check if a number is divisible by another number?
The same idea works for any number. If a % b == 0, then a divides exactly by b.
You can join two checks with and. A number divisible by both 3 and 5 is divisible by 15.
Below is the famous Fizz Buzz game. Children say Fizz for multiples of 3 and Buzz for multiples of 5.
For multiples of both, they say FizzBuzz. Every other number is said normally.
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
💡 The FizzBuzz check must come first. If Fizz came first, 15 would stop there and never reach FizzBuzz.
How do I get the last digit of a number with %?
Any whole number % 10 gives its last digit. 4729 % 10 is 9.
And number // 10 removes the last digit. 4729 // 10 is 472.
Put both inside a while loop, and you can visit every digit one by one. Here we add up the digits.
▶n = 47292digit_sum = 03while n > 0:4 digit = n % 105 digit_sum = digit_sum + digit6 n = n // 107print("Sum of digits:", digit_sum)
No variables yet.
▌✏️ Change this code and run it yourself
Sum of digits: 22
💡 Teachers use digit sums to check if a number divides by 3. 4729 has digit sum 22, so it does not.
How does % help with clocks and repeating patterns?
A clock goes up to 12 and then starts again from 1. Days of the week also repeat after 7.
% makes a number wrap around. Any number % 7 is always between 0 and 6.
Say today is Monday. We can find the day 100 days later without a calendar.
100 % 7 is 2, because 98 days make 14 full weeks. So we move two days on from Monday, to Wednesday.
The train works the same way. 22 + 5 is 27, and 27 % 24 is 3. So it arrives at 3 o'clock at night.
How many days from Monday? 100 That day is a Wed Train arrives at 3 o'clock
How do I use % to do something every few rounds?
Sometimes you want an action every 3rd or every 5th time in a loop.
Check if the loop counter % 5 is 0. That is True on rounds 5, 10, 15 and so on.
Here a fitness app cheers you on after every 5 push-ups.
Games use the same trick. They give a bonus every 10th level, or a new row after every 4 items.
Change 5 to 3 and run it again. Now the cheer comes on rounds 3, 6, 9 and 12.
Push-up 1 Push-up 2 Push-up 3 Push-up 4 Push-up 5 Great job! Keep going! Push-up 6 Push-up 7 Push-up 8 Push-up 9 Push-up 10 Great job! Keep going! Push-up 11 Push-up 12
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
print(17 % 5, 17 // 5)
Question 2. What does this print?
print(6 % 8)
Question 3. What does this print?
count = 0
for n in range(1, 21):
if n % 4 == 0:
count = count + 1
print(count)🧩 Your challenge
A school bus has 40 seats. Ask how many students are going on the trip. Print how many buses are full, how many students are on the last bus, and the total number of buses needed.
How many students? 135 Full buses: 3 Students on the last bus: 15 Total buses needed: 4
⚠️ Common mistakes (and how to fix them)
❌ Wrong
n = 9
if n % 2 == 1:
print(n, "is even")No error message – but it calls 9 even, which is wrong.
✅ Right
n = 9
if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")Even numbers leave remainder 0 when divided by 2. Remainder 1 means odd.
❌ Wrong
apples = 10 baskets = 0 print(apples % baskets)
ZeroDivisionError: integer modulo by zero
✅ Right
apples = 10 baskets = 3 print(apples % baskets)
You cannot divide by zero, so % by zero is not allowed either. Check the number first.
❌ Wrong
num = input("Enter a number: ")
print(num % 2)TypeError: not all arguments converted during string formatting
✅ Right
num = int(input("Enter a number: "))
print(num % 2)input() always gives text. On text, % means something else, so convert with int() first.
📌 Remember
- % gives the remainder after division; // gives the whole-number answer.
- n % 2 == 0 means n is even; n % 2 == 1 means n is odd.
- a % b == 0 means a divides exactly by b.
- n % 10 gives the last digit, and n // 10 removes it.
- % wraps numbers around, like hours on a clock or days in a week.
- Never use % with 0 on the right side.
Finished the quiz and the challenge?
Questions students ask
What does % mean in Python?
Between two numbers, % is the modulo operator. It gives the remainder, so 10 % 3 is 1.
What is the difference between / , // and % in Python?
/ gives the exact answer with a decimal. // gives only the whole part. % gives only the remainder.
How do I check if a number is a multiple of 5 in Python?
Use if n % 5 == 0. If the remainder is zero, n is a multiple of 5.
Why is 3 % 10 equal to 3?
10 fits into 3 zero times. So nothing is taken away and the remainder is 3 itself.