Skip to content

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.

Beginner 12 min read·Lesson 50 of 306 in Python Programming

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

Python
remainder = a % b
is_even = n % 2 == 0
next_index = (index + 1) % length
  • % gives the remainder of a / b.
  • Comparing with 0 tests divisibility.
  • Adding then taking % wraps a position around.

Code examples

Remainders

Python
print(17 % 5)
print(20 % 4)
print(-7 % 3)
Output
2
0
2

Even or odd

Python
for n in [4, 7, 10]:
print(n, "even" if n % 2 == 0 else "odd")
Output
4 even
7 odd
10 even

More examples

Leap years, weekdays and digits

Python
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
print([y for y in (1900, 2000, 2024, 2026) if is_leap(y)])
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
today = 3 # Thursday
print("100 days from Thursday:", days[(today + 100) % 7])
n = 4729
digit_sum = 0
while n > 0:
digit_sum += n % 10
n //= 10
print("digit sum:", digit_sum)
Output
[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:

TextOutput
[2000, 2024]
100 days from Thursday: Sat
digit sum: 22

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

if n / 2 == 0:

Prefer

if n % 2 == 0:

n / 2 is half of n; only the remainder tells you whether n is even.

Forgetting parentheses when wrapping

Avoid

days[today + 100 % 7]

Prefer

days[(today + 100) % 7]

% 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

  1. 1
    Print the numbers from 1 to 30 that are divisible by both 3 and 5.
  2. 2
    Given 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?
Practise this topic