Syntax of the Modulo Operator in Python
To use the modulus operator in Python, we have to write Dividend first, then use the modulo symbol, and at last, you have to write a divisor.
dividend % divisor
The above syntax will return the remainder when a is divided by b.
Example 1: Modulo Operator with Int
Find the remainder when a number ‘a’ is equal to 9 and is divided by a number ‘b,’ which is equal to 5, with the help of the modulo operator in Python.
Implementation in Python
Python
a = 9
b = 5
print(‘The mod of these numbers is’, a % b)

You can also try this code with Online Python Compiler
Run Code
Output
The mod of these numbers is 4
Example 2: Modulo Operator with Float
Find the remainder when a floating number ‘a’ is equal to 17.65 and is floating divided by a number ‘b,’ which is equal to 6.32, with the help of a modulo operator in Python.
Implementation in Python
Python
a = 17.65
b = 6.32
print(‘The mod of these numbers is’, a % b)

You can also try this code with Online Python Compiler
Run Code
Output
The mod of these numbers is 5.009999999999998
Example 3: Modulo Operator with Negative Operand
Find the remainder when a floating number ‘a’ equals 23.00 and is divided by a negative number ‘b,’ equal to -6 with the help of a modulo operator in Python.
Implementation in Python
Python
a = 23.00
b = -6
print(‘The mod of these numbers is’, a % b)

You can also try this code with Online Python Compiler
Run Code
Output
The mod of these numbers is -1.0
Precedence of Modulo Operator
The precedence tells about the priority of any operator while solving an equation with multiple operators. Operators with higher precedence will be solved first.
The precedence of the Modulo (%) operator is equal to the division ( / ), multiplication ( * ), and floor division ( // ) operators. These operators have the highest precedence.
Example
Let we have a equation a % b + c * d, where a = 5, b = 2, c = 6, d = 3
As we know, % and * have the highest precedence so that they will be solved first then we will add them.
= a % b
= 5 % 2
= 1
c * d
= 6 * 3
= 18
So,
Ans = 1 + 18 = 19
Let’s see the answer by Python code of the same equation.
Implementation in Python
Python
a = 5
b = 2
c = 6
d = 3
n = a % b + c * d
print("The ans of the above equation is: ", n)

You can also try this code with Online Python Compiler
Run Code
Output
The ans of the above equation is: 19
ZeroDivisionError in Python
ZeroDivisionError occurs when the divisor in the division or Modulo is zero because the result becomes undefined when a number is divided by zero.
Example
Implementation in Python
Python
a = 23
b = 0
print(‘The mod of these numbers is’, a % b)

You can also try this code with Online Python Compiler
Run Code
Output
ERROR!
Traceback (most recent call last):
File "<string>", line 3, in <module>
ZeroDivisionError: integer modulo by zero
The above code shows the ZeroDivisionError because b is zero in the given code.
Applications of the Modulo Operator in Python
Some of the applications of the modulo operator in Python are as follows
- It is used to find the remainder when the divisor divides the dividend.
- The Modulo operator is also used to check the divisibility of a number.
The - Modulo Operator in Python can also be used to find the remainder of a floating and negative operand.
- Modulo operators can help make a number divisible by some number by subtracting the remainder (By the modulo operator) from that number.
Frequently Asked Questions
What are operators?
Operators are used to tell the compiler about manipulating any arithmetic or logical operation in the programming languages. Some operators used in most languages are '+,' '>,' '-,' '/,' and so on.
What does %= mean in Python?
In Python, %= is the modulus assignment operator. It updates the value of a variable by taking its modulus with another value.
What is an example of a modulus operator?
An example of the modulus operator is a % b, which returns the remainder when a is divided by b.
What is modulo 2 in Python?
x % 2 in Python checks if a number x is even or odd. If the result is 0, x is even; otherwise, it's odd.
What are mod and div in Python?
In Python, `div` refers to integer division using the `//` operator, which divides two numbers and returns the quotient without the remainder. `mod`, using the `%` operator, returns the remainder of a division.
Conclusion
In this article, we learned about one of the operators used in the programming language. We mainly focused on the modulo operator in Python, in which we learned about the basic definition of this operator along with some examples.
Recommended Readings: