Skip to content

Python Modulo Operator: Understanding % in Python

Python Modulo Operator Understanding % in Python Cover Image

In this tutorial, you’ll learn how the modulo operator (%) works in Python. The Python modulo operator is one of the many arithmetic operators that are available in Python and one that you’ll encounter often. Because of this, understanding the details of how this operator works is an important skill for any Python developer. The modulo operator returns the remainder of dividing two numbers.

By the end of this tutorial, you’ll have learned:

  • How the modulo operator works in Python
  • How the Python modulo operator works with different numeric data types
  • How to use the modulo operator with practical examples

How the Modulo Operator (%) Works in Python

The modulo is a mathematical operation that is returns the remainder of a division between two values. In Python, as well as many other languages, the % percent sign is used for modulo operations. Let’s take a look at how the operation is handled in Python:

# Taking a Look at the Modulo Operator in Python
remainder = 7 % 3
print(remainder)

# Returns: 1

In the example above, we find the modulo returned from 7 % 3. In this example, 7 is divided by 3, which returns 2, with a remainder of 1. Because of this, the operation returns the value of 1.

The modulo operator will always return a value with two numeric types, except when the operator is used with 0 as a denominator. In this case, a ZeroDivisionError is raised because numbers cannot be divided by 0.

Let’s take a look at what this looks like:

# Raising a ZeroDivisionError with Python Modulo
remainder = 7 % 0
print(remainder)

# Raises: ZeroDivisionError: integer division or modulo by zero

In the next section, you’ll learn how to use the Python modulo operator with integers.

Python Modulo with Integers

When the Python modulo is used with two integers, the result will always be an integer. This is not the case when an integer is used with a floating point value. Knowing this can be an important consideration in how you use the operator.

Let’s take a look at how we can use two integers with the modulo operator:

# Using the Modulo Operator with 2 Integers
remainder = 13 % 5
print(remainder)

# Returns: 3

In the next section, you’ll learn how to use the modulo operator with floating point values.

Python Modulo with Floating Point Values

When the Python modulo operator is used with either two floating point values or a single floating point value, the operator will return a floating point value. Let’s take a look at how this works in Python:

# Using Floating Point Values with Modulo in Python
remainder = 13.0 % 5
print(remainder)

# Returns: 3.0

In the next section, you’ll learn how the modulo operator works with negative values.

Python Modulo with Negative Values

When working with negative numbers, the modulo operator can return some unexpected results. The sign of the result will always be the sign of the divisor. The reason for this is that different computer languages make this decision in different ways.

This leaves us with three possibilities with negative values:

  1. +num % -num returns a negative number
  2. -num % +num returns a positive number
  3. -num % -num returns a negative number

Let’s take a look at an example:

# Using Negative Values with Python Modulo
remainder = -13 % 5
print(remainder)

# Returns: 3

Similarly, take a look at the example below to see how the modulo operator handles a negative divisor:

# Using a Negative Divisor with Python Modulo
remainder = 13 % -5
print(remainder)

# Returns: -2

In the following sections, you’ll learn how to use the Python modulo operator for some practical use cases.

Using Python Modulo to Check if a Number is Even

One of the most common use cases of the Python modulo operator you’ll encounter is to check whether a number is even or odd. Because any even number divided by 2 will not have a remainder, you can evaluate whether the modulo of any number and 2 is 0.

Let’s see how you can use Python to check if a number is even or odd in Python:

# Checking if a Number is Even or Odd in Python
def even_or_odd(num):
    if num % 2 == 0:
        print('Number is even')
    else:
        print('Number is odd')

even_or_odd(51)

# Returns: Number is odd

In the code above, we developed a function that takes a single argument. If the argument has a remainder when divided by 2, then the function indicates that it’s even. Otherwise, the function prints out that it’s odd.

Printing Every n Records with Python Modulo

When you’re working with programs that repeat something many times, you may want to print out some indicator of progress. In these cases, it may not be practical to print every single instance of an operation. In these cases, it may make sense to print out every n number of instances of an action.

Take a look at the code below to see how you can do this:

# Printing Out Every 50 Records in a Python Loop
for i in range(201):
    # Do something

    # Check iteration
    if i % 50 == 0:
        print(f'Iteration #{i}')

# Returns:
# Iteration #0
# Iteration #50
# Iteration #100
# Iteration #150
# Iteration #200

Conclusion

In this tutorial, you learned how to use the Python modulo operator, which is represented by the percent sign. The modulo operator is used to return the remainder of a division of two numeric values. You first learned how to use the operator with integers, floating point values, and negative values. Then, you learned how to use the operator with two practical examples that represent real-work methods of how you’d apply the operator in your code.

Additional Resources

To learn more about related topics, check out the tutorials below:

Tags:

Leave a Reply

Your email address will not be published.