The percent sign does not mean percent in code. Here is what it actually does.
Most people see % and think percentage. In programming, % is the modulo operator. It gives you the remainder after division. That is it. Once that clicks, a lot of things that looked like magic in other people’s code start making sense.
What Modulo Does
When you divide two numbers, you get a quotient and a remainder.
17 divided by 5:
quotient = 3 (5 goes into 17 three times)
remainder = 2 (3 * 5 = 15, and 17 - 15 = 2)
Modulo gives you that remainder.
17 % 5 # 2
10 % 3 # 1
9 % 3 # 0
8 % 2 # 0
If the result is 0, it divided evenly. That turns out to be very useful.
The Most Common Use: Even and Odd
number = 7
if number % 2 == 0:
print("even")
else:
print("odd")
If a number divided by 2 has a remainder of 0, it is even. If the remainder is 1, it is odd. This is something you will see in nearly every codebase.
Cycling Through a Range
This is where modulo gets genuinely useful. Say you have a list of 5 items and you want to cycle through them repeatedly, no matter how high a counter gets.
items = ["a", "b", "c", "d", "e"]
for i in range(20):
print(items[i % 5])
When i is 0 through 4, i % 5 gives 0 through 4. When i hits 5, 5 % 5 is 0, and the cycle starts over. When i is 7, 7 % 5 is 2, so you get items[2].
This pattern shows up in rotating banners, round-robin task assignment, looping animations, anything where you need to wrap around.
Clock Math
Modulo is how clock arithmetic works. If it is 10 o’clock and you add 5 hours, you do not get 15 o’clock. You get 3 o’clock.
(10 + 5) % 12 # 3
(11 + 2) % 12 # 1
(6 + 6) % 12 # 0 (which represents 12)
Same idea: after you hit the ceiling, you wrap back to the start.
Checking Divisibility
If you need to do something every Nth iteration, modulo is the tool.
for i in range(1, 101):
if i % 10 == 0:
print(f"Checkpoint at {i}")
This prints at 10, 20, 30, and so on. The check i % 10 == 0 is true only when i is a multiple of 10.
FizzBuzz, the classic interview problem, is built on this:
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Negative Numbers: Where It Gets Weird
This is the part that catches people off guard.
In Python, modulo follows the sign of the divisor (the number on the right).
-7 % 3 # 2 (not -1)
7 % -3 # -2 (not 1)
If you are coming from C, C++, or JavaScript, this will feel wrong. Those languages follow the sign of the dividend (the number on the left) instead.
Python: -7 % 3 = 2
C/JavaScript: -7 % 3 = -1
Python’s behavior is mathematically consistent with how modulo is defined in number theory, but it can produce surprising results if you are not expecting it. If you are writing Python that might also be ported to another language, keep this in mind.
Modulo in Excel
Excel has a MOD() function that does the same thing.
=MOD(17, 5) returns 2
=MOD(10, 3) returns 1
=MOD(9, 3) returns 0
It follows Python’s behavior for negatives (result takes the sign of the divisor).
Common Mistakes
Dividing by zero. x % 0 throws a ZeroDivisionError in Python, same as regular division. Always check that your divisor is not zero if it comes from user input or variable data.
Confusing % with integer division. The // operator gives you the quotient. The % operator gives you the remainder. They are related but different.
17 // 5 # 3 (quotient)
17 % 5 # 2 (remainder)
Forgetting the sign rules with negatives. If your code behaves strangely with negative numbers, check whether you expected C-style or Python-style modulo behavior.
Related Posts
- Order of Operations: What Runs First and Why It Matters - modulo has a precedence like any other operator
- Order of Operations: The Deep Dive - covers associativity and how
%fits in
Your Turn
Modulo is one of those things that feels obscure until it suddenly appears everywhere. What was the first time you actually needed it in real code? Or if you are still getting your head around it, drop a specific question below and we will work through it.

