Floor Division and Integer Division
The modulus operator (%) returns the remainder when one integer is divided by another.
From Groups to Leftovers
When you divide objects into equal-sized groups, division gives you two useful pieces of information: how many complete groups you can make and how many items remain. The floor division operator // gives the number of complete groups. The modulus operator % gives the leftover, called the remainder.
Suppose you have 7 apples and place them into groups of 3. You can make 2 complete groups, using 6 apples, and 1 apple remains ungrouped. The complete-group result is 2, while the remainder is 1.
Reading the Operators
Python uses a % b for modulus, where a is the dividend and b is the divisor. The expression asks what remains after dividing a by b. Floor division, written a // b, asks how many complete groups of size b fit into a. True division, written a / b, gives the decimal result. These operators examine the same division situation but return different information.
Tracing a Remainder
What do you think happens?
Predict the result of 17 % 5 before checking the explanation.
Reveal answer
Answer: 2
Three complete groups of 5 use 15 items. Two items remain, so the remainder is 2.
The expression 17 % 5 returns 2 because 5 fits completely into 17 three times, accounting for 15 items, and 2 items remain. The corresponding floor division expression 17 // 5 returns 3 because it counts the complete groups.
Repeating Remainder Patterns
With the same positive divisor, remainders stay within a fixed range. For a divisor b, the result is between 0 and b minus 1. If the dividend divides evenly by the divisor, the result is 0. If the dividend is smaller than the divisor, no complete group can be made, so the remainder is the dividend itself.
| Expression | Remainder | What it shows |
|---|---|---|
| 0 % 3 | 0 | The cycle starts at 0 |
| 1 % 3 | 1 | The remainder increases |
| 2 % 3 | 2 | The largest possible remainder for divisor 3 |
| 3 % 3 | 0 | The cycle returns to 0 |
| 4 % 3 | 1 | The cycle repeats |
Remainders repeat through 0, 1, and 2 when the divisor is 3.
This repeating behavior makes modulus useful for detecting patterns, checking divisibility, identifying even and odd numbers, distributing items into buckets, calculating positions in circular structures such as clocks or calendars, and working with recurring cycles.
Common Misunderstandings
Treating % as another way to ask for the number of complete groups
The modulus operator returns what remains after complete groups are made. It does not return the number of complete groups.
Fix:
Use // for the number of complete groups and % for the leftover.Expecting a remainder of 0 whenever the dividend is smaller than the divisor
When the dividend is smaller than the divisor, no complete group can be made, but the entire dividend remains.
Fix:
Recognize that 5 % 8 is 5.Forgetting that an even division produces a remainder of 0
If the dividend divides evenly by the divisor, nothing is left over.
Fix:
Check whether the divisor fits an exact number of times; an exact division has remainder 0.Confusing %, //, and /
True division gives the decimal result, while floor division gives complete groups and modulus gives the remainder.
Fix:
Choose the operator according to the information the program needs.
Practice Predictions
Predict the result of each modulus expression, then explain the complete groups and leftover items: 12 % 4, 10 % 3, 5 % 8, and 14 % 5.
Hints
- Find how many complete groups of the divisor fit into the dividend.
- Subtract the items in those complete groups from the original dividend.
- Remember that an exact division has remainder 0.
- When the dividend is smaller than the divisor, the dividend remains.
Checking 14 % 5
Determine the remainder when 14 is divided by 5.
Find complete groups: Five fits into fourteen two complete times.
Account for used items: Two groups of five account for ten items.
Find what remains: Four items remain after ten are grouped.
14 % 5 is 4.
Key Takeaways
- The modulus operator % returns the remainder after one integer is divided by another.
- The floor division operator // returns the number of complete groups.
- For a positive divisor b, a % b produces a value from 0 through b minus 1.
- An exact division has remainder 0, while a smaller dividend remains unchanged as the remainder.
- Repeating remainders make modulus useful for divisibility checks, even and odd patterns, buckets, positions, and recurring cycles.
Key Takeaways
- Modulus finds the leftover from division.
- Floor division finds the number of complete groups.
- Remainders follow predictable cycles for a fixed divisor.
- Use modulus when a program needs to detect divisibility, repeating positions, or recurring patterns.