Last modified: Feb 04, 2026 By Alexander Williams

Python Mod Function: A Beginner's Guide

In Python, the mod function is a fundamental operator. It is essential for many programming tasks. This guide explains everything you need to know.

What is the Mod Function?

The mod function finds the remainder of a division. In Python, it uses the percent symbol %. It is also called the modulo operator.

It does not return the quotient. It returns what is left over after division.

Basic Syntax and Usage

The syntax is simple: dividend % divisor. The result is the remainder. Understanding basic Python function syntax helps here.

Let's look at a basic example.


# Basic modulo operation
result = 10 % 3
print(result)
    

1
    

Here, 10 divided by 3 is 3 with a remainder of 1. The operator returns 1.

Common Use Cases and Examples

The mod function is very practical. It is not just a math exercise.

1. Checking for Even or Odd Numbers

This is the most common use. A number modulo 2 is 0 if even, and 1 if odd.


def check_parity(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

print(check_parity(7))
print(check_parity(12))
    

Odd
Even
    

2. Cycling Through Data in a Loop

You can use it to cycle through a fixed list. This is great for round-robin patterns.


colors = ['Red', 'Green', 'Blue']
for i in range(10):
    current_color = colors[i % len(colors)]
    print(f"Step {i}: {current_color}")
    

Step 0: Red
Step 1: Green
Step 2: Blue
Step 3: Red
Step 4: Green
Step 5: Blue
Step 6: Red
Step 7: Green
Step 8: Blue
Step 9: Red
    

The index i % 3 always cycles between 0, 1, and 2.

3. Data Validation and Constraints

You can ensure a number stays within a specific range. This is useful for wrapping values.


# Ensure an angle stays between 0 and 360 degrees
angle = 725
normalized_angle = angle % 360
print(f"725 degrees normalized is: {normalized_angle} degrees")
    

725 degrees normalized is: 5 degrees
    

Important Behaviors to Remember

The mod function has specific rules. Knowing them prevents bugs.

The result takes the sign of the divisor. This is a key Python rule. It differs from some other languages.


print( 10 % 3)   # Positive divisor
print( 10 % -3)  # Negative divisor
print(-10 % 3)   # Positive divisor
print(-10 % -3)  # Negative divisor
    

1
-2
2
-1
    

Notice how the sign of the result matches the divisor (the second number).

Modulo with zero raises an error. You cannot divide by zero.


# This will cause an error
# result = 5 % 0
    

Modulo with Other Data Types

The % operator is versatile. It works beyond integers.

It works with floats. The result is a float.


print(10.5 % 3.2)
    

0.8999999999999995
    

Note the floating-point precision issue. For financial math, use the decimal module.

It also works with complex numbers in some Python versions. But this is less common.

Advanced Usage: divmod() Function

Python has a built-in divmod() function. It returns both the quotient and the remainder at once.

This can be more efficient than two separate operations. It relates to concepts like Python function argument unpacking.


quotient, remainder = divmod(17, 5)
print(f"Quotient: {quotient}, Remainder: {remainder}")
    

Quotient: 3, Remainder: 2
    

Conclusion

The Python mod function is a small but powerful tool. It is key for checking parity, cycling data, and wrapping values.

Remember its sign rule and that division by zero is illegal. Start using % in your loops and conditionals today.

It will make your code more efficient and elegant.