Module 4 · Operators — topic 7 of 16
Modulus
The % operator returns the remainder of a division. Use it to test even and odd numbers, check divisibility, cycle through values, extract digits and work with clocks.
Learning objectives
- Calculate remainders with %
- Test divisibility and even/odd
- Use % for cycling and digit extraction
Definition
The % operator returns the remainder of a division. Use it to test even and odd numbers, check divisibility, cycle through values, extract digits and work with clocks.
Introduction
The modulus operator % gives the remainder after division. 17 % 5 is 2, because 5 goes into 17 three times with 2 left over. It is sometimes called “mod”.
Why it matters
Remainders answer many everyday questions: Is this number even? Is the year a leap year? Which day of the week is 100 days from now? What is the last digit of a number? % solves all of these in a single expression.
Detailed explanation
n % 2 is 0 for even numbers and 1 for odd numbers.
a % b == 0 means a is divisible by b, for example year % 4 == 0.
Wrapping around: (index + 1) % length cycles through positions 0, 1, … length − 1 and back to 0. Clocks and weekdays work this way.
Digits: n % 10 gives the last digit of a positive integer, and n // 10 removes it.
With a positive divisor, Python’s % result is always between 0 and the divisor minus one, even for negative numbers: -7 % 3 is 2. This makes wrapping backwards easy.
% also works with floats (7.5 % 2 is 1.5) and raises ZeroDivisionError for a zero divisor.
In strings, % is an old formatting operator ("%d items" % 3); f-strings are preferred today.
Syntax
- % gives the remainder of a / b.
- Comparing with 0 tests divisibility.
- Adding then taking % wraps a position around.
Code examples
Remainders
2 0 2
Even or odd
4 even 7 odd 10 even
More examples
Leap years, weekdays and digits
[2000, 2024] 100 days from Thursday: Sat digit sum: 22
Step-by-step explanation
- is_leap applies the Gregorian rule with three % tests; 1900 fails because it is divisible by 100 but not 400.
- (today + 100) % 7 wraps around the week: (3 + 100) % 7 is 5, which is Saturday.
- In the loop, n % 10 takes the last digit and n //= 10 removes it.
- The digits 4 + 7 + 2 + 9 add up to 22.
Expected output
Running “Leap years, weekdays and digits” prints exactly:
Practical applications
- Calendars use % to find weekdays and leap years.
- Hash tables place items in buckets with hash(key) % bucket_count.
- Check digits on ISBNs and bank cards are verified with % calculations.
Best practices
- Compare % results with == 0 for divisibility checks.
- Use % to wrap indexes instead of if statements.
- Prefer f-strings over % string formatting in new code.
Common mistakes
Testing even with / instead of %
Avoid
Prefer
n / 2 is half of n; only the remainder tells you whether n is even.
Forgetting parentheses when wrapping
Avoid
Prefer
% binds tighter than +, so without parentheses only 100 is wrapped.
Key takeaways
- % returns the remainder of a division.
- n % 2 == 0 tests for even numbers; a % b == 0 tests divisibility.
- (i + step) % length wraps around a sequence.
Try it yourself
- 1Print the numbers from 1 to 30 that are divisible by both 3 and 5.
- 2Given the hour 22 on a 24-hour clock, print the hour 5 hours later using %.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?