Detecting Patterns in Sequences
The modulus operator (%) returns the remainder when one integer is divided by another.
The Leftover That Reveals a Pattern
Suppose 7 apples must be placed into groups of 3. Two complete groups can be made, and 1 apple remains. That leftover is the remainder. In Python, the modulus operator extracts this leftover value. It is written with the percent sign: a % b, where a is the dividend and b is the divisor.
The modulus operator (%) returns the remainder when one integer is divided by another.
Reading a Modulus Expression
In the expression a % b, a is the number being divided and b is the number that sets the group size. The result is the amount left after making as many complete groups of size b as possible. If a divides evenly by b, the result is 0. Otherwise, the result is the ungrouped remainder.
| Operator | What it gives | Question it answers |
|---|---|---|
| // | The number of complete groups | How many complete groups fit? |
| % | The remainder | What is left after making complete groups? |
| / | The decimal result | What is the result of the division? |
What do you think happens?
What remainder does 7 % 3 produce?
Reveal answer
Answer: 1
Two complete groups of 3 use 6 items, leaving 1 item ungrouped.
Following the Repeating Cycle
Modulus becomes especially useful when the dividend increases through a sequence. With a divisor of 3, the remainders repeat as 0, 1, 2, 0, 1, 2, 0, 1, 2. The values never exceed 2, which is one less than the divisor. This predictable cycle lets a program recognize where a number falls within a repeating pattern.
A modulus result identifies a position within a repeating cycle. Because the results repeat through a fixed set of values, modulus can help detect patterns, check divisibility, and handle position-based problems.
Predicting Different Remainders
Evaluating Three Modulus Expressions
Predict the result of 8 % 3, 9 % 3, and 5 % 8.
8 % 3: Two complete groups of 3 use 6, leaving 2.
9 % 3: Three complete groups of 3 use all 9, so nothing is left.
5 % 8: The divisor is larger than the dividend, so no complete group of 8 can be made. The entire 5 remains.
The results are 2, 0, and 5.
Patterns in Real Programs
The repeating behavior of modulus makes it useful whenever a program needs to recognize a cycle. It can detect even and odd numbers, check whether a number divides evenly, distribute items into buckets, and calculate positions in circular structures such as clocks or calendars.
Before using modulus, decide whether you need the number of complete groups or the leftover. Use floor division when you need complete groups, and use modulus when you need the remainder or a repeating position.
Mistakes to Avoid
Treating % as ordinary division
Modulus returns what remains after complete groups are made, not the number of complete groups.
Fix:
Use % for the remainder and // for the number of complete groups.Assuming a remainder must be nonzero
When a number divides evenly, nothing is left over.
Fix:
The modulus result is 0 when the division is exact.Assuming the result can be as large as the divisor
The remainder stays between 0 and one less than the divisor.
Fix:
For divisor 3, the possible repeating remainders are 0, 1, and 2.Assuming a smaller dividend becomes zero
No complete group of 8 fits inside 5, so all 5 remains.
Fix:
When the dividend is smaller than the divisor, the dividend is the remainder.
Check Your Predictions
Predict the result of each expression before checking: 10 % 3, 12 % 4, 2 % 5, and 14 % 5. For each one, identify the complete groups and the leftover.
Hints
- Ask how many complete groups of the divisor fit into the dividend.
- The leftover is the modulus result.
- If the dividend is smaller than the divisor, the dividend remains.
- If the division is exact, the result is 0.
What do you think happens?
What results should the practice expressions produce?
Reveal answer
Answer: 10 % 3 is 1, 12 % 4 is 0, 2 % 5 is 2, and 14 % 5 is 4.
Each result is the amount left after making as many complete groups as possible.
Key Takeaways
- The modulus operator uses the syntax a % b and returns the remainder after integer division.
- Floor division gives the number of complete groups, while modulus gives what is left over.
- A modulus result is 0 when the division is exact and otherwise stays below the divisor.
- Applying the same modulus to increasing numbers creates a predictable repeating cycle.
- These cycles make modulus useful for divisibility checks, even and odd detection, buckets, and circular positions.
Key Takeaways
- The % operator returns the leftover from integer division.
- In a % b, a is the dividend and b is the divisor.
- The remainder is 0 for exact division and otherwise falls between 0 and b - 1.
- Increasing dividends create repeating remainder cycles when the divisor stays the same.
- Modulus helps programs recognize divisibility, repeating positions, and other patterns.