Modulo operator (%) in Python
Last Updated :
20 Dec, 2025
The modulo operator (%) in Python is used to find the remainder after dividing one number by another. It works with both integers and floating-point numbers and is commonly used in tasks like checking even or odd numbers, repeating patterns, and calculations based on cycles. For Example:
10 % 3 = 1
This means when 10 is divided by 3, the remainder left is 1.
Let’s see how the modulo operator works in a Python program.
Python
Explanation: 10 % 4 calculates the remainder when 10 is divided by 4, Since 4 goes into 10 two times with 2 left over, the result is 2
Syntax
a % b
Here, a is divided by b, and the modulo operator returns the remainder left after the division.
Modulo Operator with Negative or Float Numbers
Python also supports modulo operations with floating-point and negative numbers. The result always follows Python’s rule: the remainder has the same sign as the divisor.
Python
x = 15.0
y = -7.0
print(x % y)
Explanation: 15.0 is divided by -7.0. The remainder is adjusted to match the sign of the divisor (-7.0)
Using Modulo to Find Remainders in a Range
The modulo operator is often used to check patterns, such as remainders of numbers in a sequence.
Python
n = 5
k = 3
for i in range(1, n + 1):
print(i, "%", k, "=", i % k)
Output1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
Explanation:
- Each number from 1 to 5 is divided by 3
- % returns the remainder for each division
- When the remainder is 0, the number is exactly divisible
ZeroDivisionError with Modulo Operator
Just like division, the modulo operator cannot work if the divisor is zero. Doing so raises a ZeroDivisionError.
Python
a = 14
b = 0
print(a % b)
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3, in <module>
ZeroDivisionError: integer modulo by zero
Explanation:
- The right operand of % must not be 0
- Python raises an error to prevent invalid mathematical operations
Checking Even or Odd Numbers Using Modulo
One of the most common uses of the modulo operator is to check whether a number is even or odd. A number is even if it leaves a remainder of 0 when divided by 2.
Python
n = 8
if n % 2 == 0:
print("Even number")
else:
print("Odd number")
Explanation:
- n % 2 checks the remainder when n is divided by 2
- If the remainder is 0, the number is even
- Otherwise, the number is odd
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice