Last modified: Apr 04, 2026 By Alexander Williams
Python Modulo Operator Guide: % Usage & Examples
The modulo operator is a fundamental tool. It is represented by the percent symbol %. This operator returns the remainder of a division. It is simple but incredibly powerful.
You will use it for many common tasks. These include checking for even numbers, wrapping values, and cycling through data. Understanding it is key for any Python programmer.
What is the Modulo Operator?
The modulo operator performs a division. It does not return the quotient. Instead, it gives you the remainder. For example, 10 divided by 3 is 3 with a remainder of 1. The modulo operation 10 % 3 returns 1.
Its syntax is straightforward. You place the % symbol between two numbers. The number on the left is the dividend. The number on the right is the divisor. The result is the remainder.
# Basic modulo operation
result = 10 % 3
print(result) # This will print the remainder
1
Basic Usage and Examples
Let's look at some simple examples. This will solidify your understanding of how % works in practice.
print(17 % 5) # Remainder is 2
print(20 % 6) # Remainder is 2
print(8 % 2) # Remainder is 0 (even division)
print(7 % 10) # When dividend is smaller, result is the dividend: 7
2
2
0
7
Common Practical Applications
The real power of modulo is in its applications. Here are the most common uses you will encounter.
1. Checking for Even or Odd Numbers
This is perhaps the most frequent use. Any even number divided by 2 has a remainder of 0. An odd number has a remainder of 1.
def check_number(num):
if num % 2 == 0:
return f"{num} is even."
else:
return f"{num} is odd."
print(check_number(4))
print(check_number(7))
4 is even.
7 is odd.
2. Implementing Cyclic Behavior
Modulo is perfect for cycles. Think of a clock or looping through a list. The result always "wraps around" within the limit of the divisor.
# Simulating a 12-hour clock
current_hour = 10
hours_to_add = 5
new_hour = (current_hour + hours_to_add) % 12
print(f"The new hour is {new_hour} o'clock.")
The new hour is 3 o'clock.
This concept is vital for game development, animations, and managing circular data structures.
3. Checking Divisibility
You can check if one number is divisible by another. If a % b == 0, then a is perfectly divisible by b.
def is_divisible(dividend, divisor):
return dividend % divisor == 0
print(is_divisible(15, 5)) # True
print(is_divisible(15, 4)) # False
True
False
Modulo with Negative Numbers
Modulo behavior with negatives can be confusing. In Python, the result takes the sign of the divisor. This is not the case in all programming languages.
print(10 % 3) # Positive divisor, positive result: 1
print(10 % -3) # Negative divisor, negative result: -2
print(-10 % 3) # Positive divisor, positive result: 2
print(-10 % -3) # Negative divisor, negative result: -1
1
-2
2
-1
Remember this rule. The remainder has the same sign as the divisor. This ensures consistency in mathematical properties.
Modulo with Floating-Point Numbers
The modulo operator also works with floats. It returns the floating-point remainder.
print(10.5 % 3.2)
print(7.0 % 2.5)
0.8999999999999995
2.0
Be aware of floating-point precision issues. The result might not be exact due to how computers store decimals.
divmod(): Getting Quotient and Remainder
Python provides a built-in function called divmod(). It returns both the quotient and the remainder in one step. This is more efficient than using // and % separately.
quotient, remainder = divmod(17, 5)
print(f"Quotient: {quotient}, Remainder: {remainder}")
Quotient: 3, Remainder: 2
Advanced Use Case: Data Wrangling
Modulo is useful in data processing. For instance, you can assign items to different groups or buckets based on a cycle.
# Assigning a list of IDs to 3 different groups
ids = [101, 102, 103, 104, 105, 106, 107]
groups = {0: [], 1: [], 2: []}
for item_id in ids:
group = item_id % 3
groups[group].append(item_id)
print(groups)
{0: [102, 105], 1: [103, 106], 2: [101, 104, 107]}
Potential Pitfalls and Tips
Avoid using 0 as the divisor. number % 0 will raise a ZeroDivisionError. Always ensure your divisor is not zero.
For very large numbers or performance-critical code, modulo can be slower than bitwise operations for powers of two (e.g., use & for % 2).
Understand the difference between floor division (//) and modulo. They are related but serve different purposes. Mastering both is crucial for numerical computing in Python.
Conclusion
The Python modulo operator is a small symbol with vast utility. It is essential for arithmetic, control flow, and algorithm design.
You learned its core function: finding the remainder. You saw its use in checking parity, creating cycles, and validating divisibility. You also explored its behavior with negative and floating-point numbers.
Start by practicing the basic examples. Then, incorporate it into your projects. Look for patterns where a value needs to wrap around or where you need to check a remainder. This operator will quickly become an indispensable part of your Python toolkit.