Python Modulo in Practice: How to Use the % Operator

By | September 27, 2021
Python Modulo in Practice

In Python, you will get all the important arithmetic operators, which operate on Python numeric data types. And one of the important Python Arithmetic operators is modulo, represented by the percentage symbol (%). Like other Python arithmetic operators, modulo operator (%) operates between two numbers and returns the remainder by dividing one number with another.

Here in this 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.

Vamware

Modulo operator in Mathematic

The term modulo in mathematics deals with modular arithmetic. Modular Arithmetic defines a fixed set of numbers that follow a circular approach. And 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 reside between 1 to 12 hours.

The 12hour clock can be divided into 12 modulo and represented as “mod 12”. And suppose we want to evaluate the time of 24 hours clock into 12 hour’s clocks, we can do this with the help of “mod 12.”

Example

convert the 15 o’clock of 24hours clock into 12-hour clocks.

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 numeric data types, like int and float.

Python Modulo Operator with Python integers

Most of the time, you will be using the modulo operator on the 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.

Example

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

ZeroDivision Error: The modulo operator first performs the division operation (/) between the number then return the remainder. And if the second number is 0, the operator returns the ZeroDivisionError. Because in Python, throw this number if any number is divided by 0.

Example

>>> 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.

Example

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

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.

Example

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

Python modulo operator with negative numbers

By far, we have only performed the modulo operator on positive numbers, and the outputs we received were also positive. But if we perform the modulo operators 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

Example

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

Python math.fmod() with negative numbers

As we discussed above, the Python fmod() method is an alternative for Python float with 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 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

Example

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

Python divmod() method

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

Example

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

Conclusion

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. There are two pythons built-in methods math.fmod() and divmod() which can also be used to find out the reminder of a number.

People are also reading: 

Leave a Reply

Your email address will not be published. Required fields are marked *