Introduction to Python Modulus Operator
In any programming, the basic building blocks are the variables (container for storing data of different types) and the calculations which take place on these variables. Operators are predefined functions that help with these calculations. The Modulus Operator helps us to get the remainder of a mathematical division operation. Due to this, the Modulus Operator is also known as the Remainder Operator. It is denoted by the “%” (percentage) sign.
Examples of Modules Operation
Let us have two numbers, 10 and 3. We are dividing 10 by 3. So here, 10 becomes the dividend and 3 becomes the divisor. A normal division operation, i.e. 10/3, will yield the following result 10/3=3.3333…Since 10 is not wholly divisible by 3, it will leave behind a remainder. The modules operator gets this remainder value.
We know when 10 is divided by 3, we get the remainder as 1, which the following way can further explain: – 10 = 3*3+1
The following operation can be performed in Python in the following way:
Code:
a = 10
b = 3
c = a%b
print(" The value of modulus operation when " + str(a) + " is divided by " + str(b) + " is " + str(c))
Output:
The modulus operator is helpful to determine whether a number is fully divisible by any number or not.
If the dividend is fully divisible by the divisor, then the result of the modulus operation is 0.
Example:
10 % 5
Output:
(Since 10 is fully divisible by 5, we get 0 as the result of the Modulus operation)
If the dividend is not fully divisible by the divisor, then the modulus operation’s result would be in a range from 0 to divisor-1.
Example:
10 % 8
Output:
(Since 10 is not fully divisible by 8, we get 2 as the result of the Modulus operation)
Types of Python Modulus Operators
Below are the types of python modulus operator:
1. Modulo with Integers
In this scenario, both the divisor and the dividend are integers. The result of the Modulus Operation is also an integer.
Example:
10 % 3
Output:
2 % 2
Output:
2. Modulo with Float
In this scenario, a divisor is a floating-point number. The result of the Modulus Operation is afloat in this case.
Example:
10 % 5.0
Output:
10 % 3.0
Output:
Example:
10.0 % 5.0
Output:
3. ZeroDivisionError
When the divisor is 0, the modulus operation throws ZeroDivisionError. We can use try catch block to catch this error. The following is an example when the divisor is 0, and we perform Modulus operation:
Code:
a = 12.5
b = 0
c = a%b
print(" The value of modulus operation when " + str(a) + " is divided by " + str(b) + " is " + str(c))
Output:
The following is an example when the divisor is 0, and we perform Modulus operation handling the exception within try-catch block in Python:
Code:
a = 12.5
b = 0
try:
print( f '{a} % {b} = {a % b}' )
except ZeroDivisionError as zde:
print("We cannot divide by 0")
Output:
4. Modulo with Negative Number
We can have the divisor and the dividend as negative numbers, but the Modulus Operation will only return the remainder having the same sign as the divisor. The following examples would help to illustrate it: –
Example:
-10 % 3
Output:
10 % -3
Output:
5. Python Modulo math.fmod()
This is the module function which is an inbuilt function of the math module of function. In this case, the sign of the Modulus operation depends on the sign of the dividend. If the dividend is negative, the result of the Modulus Operation is negative, and if it is positive, then the result is positive. The Modulus Operation result is a floating point value even if the dividend and divisor are integer values.
The following examples would help to illustrate the above concepts:
Example:
When dividend = -5 and divisor =3 then the result is -2.0
Code:
import math
print(math.fmod(-5, 3))
Output:
When dividend = 5 and divisor = -3 then the result is 2.0
Code:
import math
print(math.fmod(5, -3))
Output:
Code:
import math
print(math.fmod(5, 0))
Output:
6. Numpy Modulo numpy.mod()
Numpy is a module in Python which mainly helps us to perform numeric calculations. It is got a mod function to do Modulus Operations as well. The following code snippet illustrates the use of the mod function of numpy:
Code:
import numpy as np
a = 10
b = 3
print(np.mod(a, b))
Output:
The mod operation from numpy caters to array operations as shown below:
Code:
import numpy as np
a = np.array([2, -4, 7])
b = np.array([2, 3, 4])
print(np.mod(a, b))
Output:
Conclusion
So in this article, we had a brief discussion about the different Arithmetic Operators followed by a detailed discussion about the Modulus Operator along with examples. Modulus Operator is an integral part of any programming, and its application is quite varied. Now, as you have known all the basics of Modulus Operators, it is time to get down to coding with your preferred programming language and apply all the scenarios mentioned above.
Recommended Articles
We hope that this EDUCBA information on “Python Modulus Operator” was beneficial to you. You can view EDUCBA’s recommended articles for more information.