Loops and Conditional Logic
A common conditional pattern is to test whether a remainder equals 0.
The Leftover in Every Division
When objects are placed into equal-sized groups, division tells you how many complete groups can be made, but it may leave some objects ungrouped. The modulus operator, written as %, extracts that leftover value. In Python, an expression such as a % b finds the remainder when the integer a is divided by the integer b.
Imagine having 7 apples and placing them into groups of 3. Two complete groups use 6 apples, leaving 1 apple ungrouped. The modulus operation 7 % 3 represents that leftover, so its result is 1.
Reading a Modulus Expression
The syntax is a % b. The first value, a, is the dividend: the number being divided. The second value, b, is the divisor: the number that defines the group size. The result is the remainder after making as many complete groups of size b as possible.
| Operator | What it tells you |
|---|---|
| // | How many complete groups fit |
| % | What remains ungrouped |
| / | The decimal result of division |
These operators describe different results from the same division operation.
Tracing 17 % 5
What do you think happens?
What does 17 % 5 return?
Reveal answer
2
Five fits into 17 three complete times, using 15. The 2 that remain is the modulus result.
Finding the Remainder
Evaluate 17 % 5.
- 1Find complete groups
Groups of 5 fit into 17 three complete times.
- 2Count what was used
Three groups of 5 use 15 of the 17 items.
- 3Count what remains
Two items remain after the complete groups are formed.
17 % 5 is 2.
2
0
5The second result is 0 because 12 divides evenly into groups of 4. The third result is 5 because the dividend is smaller than the divisor: no complete group of 8 can be made, so all 5 items remain.
Patterns from Remainders
With the same divisor, modulus produces a repeating set of possible remainders. For a divisor of 3, the remainders cycle through 0, 1, and 2. The result never exceeds 2, which is one less than the divisor. This predictable cycle makes modulus useful for detecting patterns, checking divisibility, and working with positions that repeat.
0
1
2
0
1
2
0A common conditional pattern is to test whether a remainder equals 0. If a number % 2 equals 0, the number divides evenly by 2; this identifies an even-number pattern. A nonzero remainder indicates that the division is not even, which identifies the other side of that two-position pattern.
Useful Cycles in Programs
Because the remainders repeat, modulus can turn an increasing number into a repeating position. For example, applying modulus with a fixed divisor can help distribute items into buckets, calculate positions in circular structures such as clocks or calendars, and detect repeating patterns. The important idea is that the input can keep increasing while the remainder stays within a fixed range.
Mapping Positions into Three Buckets
Use the repeating results of division by 3 to assign successive positions to three repeating buckets.
- 1Start with position 0
0 % 3 produces bucket position 0.
- 2Advance the position
The next positions produce 1 and 2.
- 3Continue the cycle
After position 2, the remainder returns to 0, then repeats 1 and 2.
The repeating positions are 0, 1, 2, 0, 1, 2, and so on.
Mistakes with the Percent Sign
Common mistake Treating % as ordinary division
What it looks likeExpecting 17 % 5 to produce a decimal division result
Why it failsThe modulus operator returns the remainder, not the decimal result.
Do this insteadUse % when you want what remains after complete groups are formed.
Common mistake Confusing the remainder with the number of complete groups
What it looks likeReading 7 % 3 as 2 because two complete groups can be made
Why it failsTwo is the number of complete groups; the remainder is the one apple left over.
Do this insteadUse // for the number of complete groups and % for the leftover.
Common mistake Assuming a smaller dividend produces zero
What it looks likeExpecting 5 % 8 to be 0
Why it failsNo complete group of 8 fits into 5, so all 5 items remain.
Do this insteadWhen the dividend is smaller than the divisor, the dividend remains as the remainder.
Common mistake Forgetting that an evenly divisible pair has remainder 0
What it looks likeExpecting 12 % 4 to be 4
Why it failsFour divides into 12 evenly, so nothing is left over.
Do this insteadCheck whether the dividend divides evenly by the divisor; if it does, the modulus result is 0.
Practice the Remainder
Predict the result of each expression before checking it: 14 % 4, 20 % 5, 3 % 7, and 10 % 3. For each one, identify the complete groups and the leftover.
Hints
- Ask how many complete groups of the divisor fit into the dividend.
- The modulus result is the amount left after those complete groups.
- If the division is even, the result is 0.
- If the dividend is smaller than the divisor, the dividend remains.
A repeating position uses the expression n % 4. List the results for n values 0 through 7. Then describe the cycle you observe and explain how the result could represent one of four repeating positions.
Hints
- The possible results are bounded by the divisor.
- Start with 0 % 4 and increase the dividend one step at a time.
- Look for the point where the result returns to 0.
Modulus Checklist
- 1The % operator returns the remainder after one integer is divided by another.
- 2In a % b, a is the dividend and b is the divisor.
- 3A result of 0 means the dividend divides evenly by the divisor.
- 4For a fixed divisor, the remainders form a predictable repeating cycle.
- 5Use modulus for leftovers, divisibility checks, repeating patterns, buckets, and circular positions.
Key Takeaways
- Modulus, written as %, extracts the remainder from integer division.
- The expression a % b uses a as the dividend and b as the divisor.
- A remainder of 0 indicates even division.
- Repeated modulus operations with the same divisor create cycles of predictable positions.
- Modulus is useful for divisibility checks, even-or-odd patterns, buckets, and circular structures.