Python Modulo in Practice: How to Use the % Operator

Posted in /  

Python Modulo in Practice: How to Use the % Operator

Vinay Khatri
Last updated on April 14, 2024

    In Python, there are various arithmetic operators which operate on Python numeric data types. Also, one of the important Python Arithmetic operators that you may find useful is modulo, represented by the percentage symbol (%). Like other Python arithmetic operators, modulo operator (%) operates between two numbers. It divides one number by another and returns the remainder. In this Python tutorial, we have covered the different aspects of the Python modulo operator. By the end of this tutorial, you will have a complete idea about the Python modulo operator and how it works.

    Modulo Operator in Mathematics

    The term modulo in mathematics deals with modular arithmetic. In general, modular arithmetic defines a fixed set of numbers that follow a circular approach. Also, all the arithmetic operations on the modular return a result from the fixed set of numbers called modulus. The twelve-hour clock is a classic example of modular arithmetic. It does not matter if we add or reduce time in the twelve-hour clock. The time will always remain between 1 and 12 hours. The twelve-hour clock can be divided into 12 modulo and represented as “mod 12”. Suppose we want to evaluate the time of the 24-hour clock in terms of the 12-hour clock, and we can do this with the help of “mod 12.”

    Example: convert the 15 o’clock of the 24-hour clock into the 12-hour clock. 15 o’clock mod 12 o’clock = 3 o’clock

    Python Modulo Operator

    The Modulo operator is an arithmetic operator, and like other arithmetic operators, it operates between two Python numbers that can be of one of the two data types: integer and float.

    Python Modulo Operator with Python Integers

    Most of the time, you will be using the modulo operator with Python integer data type. If we use the Python modulo operator (%) between two integer values a and b, the modulo operator will return the remainder by dividing a by b. The return output will also be an integer value.


    >>> a = 20
    >>> b = 3
    >>> a % b
    >>> b % a

    ZeroDivision Error: The modulo operator first performs the division operation (/) between the numbers and then returns the remainder. If the second number is 0, the operator returns the ZeroDivisionError.


    >>> a = 20
    >>> b =0
    >>> a % b
    Traceback (most recent call last):
    ZeroDivisionError: integer division or modulo by zero

    Python Modulo Operator with Python Floating Numbers

    If we perform the modulo operator between two floating-point numbers, it will return the division's reminder. The return remainder value will also be a floating-point number .


    >>> a= 20.3
    >>> b = 3.2
    >>> a % b
    >>> b % a

    Python math.fmod() method

    The fmod() method is an alternative to the float with modulo operator(%). The fmod(a,b) method accepts two arguments and returns the remainder of their division. Regardless of the numbers data type, the fmod() method returns a floating-point number.


    >>> import math
    >>> math.fmod(20, 3)  #equivalent to 20.0 % 3.0
    >>> math.fmod(20.2, 3.3)

    Python modulo operator with negative numbers

    By far, we have only performed the modulo operator on positive numbers, and their outputs are also positive. But if we use the modulo operator between negative numbers, things get tricky. If either of the numbers is negative, the modulo operator uses the following formula to calculate the remainder:

    r = dividend - ( divisor * floor (dividend / divisor))
    23 % -3
    r = 23 - (-3 * floor(8 / -3))
    r = 23 - (-3 * -8)
    r= 23  - (24)
    r = -1


    >>> dividend = 23
    >>> divisor = -3
    >>> r = dividend % divisor
    >>> r

    Python math.fmod() with negative numbers

    As we discussed above, the Python fmod() method is an alternative for Python float with a modulo operator. But this is only true for positive numbers. If we pass a negative number in fmod() method, it will not give the same result as the modulo operator % . If the passed arguments numbers are negative, the output will be different. The math.fmod() use the following formula to output the remainder of a number:

    r = dividend - ( divisor * int (dividend/ divisor))
    math.mod(23.0, -3.0)
    r = 23.0 -( -3.0 * int(23.0/ -3.0 ))
    r = 23.0 -( -3.0 * -7)
    r = 23.0 – 21.0
    r = 2


    >>> dividend = 23
    >>> divisor = -3
    >>> dividend % divisor
    >>> math.fmod( 23, -3)

    Python divmod() method

    Python provides an inbuilt-method divmod() which returns a tuple containing the floor division and reminder.


    >>> 35 // 7
    >>> 35 % 7
    >>> divmod(35, 7)
    (5, 0)


    In this tutorial, you learned about the Python modulo operator. To represent the modulo operator, we use the % symbol. The modulo operator operates between two numbers and returns the remainder of their division. Also, there are two built-in Python methods, namely math.fmod() and divmod() that you can use to find the remainder between two numbers.

    People are also reading:

    Leave a Comment on this Post