Python Modulo Operator (%)

In Python, the modulo operator is represented by the percentage symbol (%). It returns the remainder of dividing the left operand by the right operand. Syntax:
x % y

Example 1:

x=8 y=4 print(x % y) //Output: 0
8 % 2 evaluates to 0 because there's no remainder if 8 is divided by 2 ( 4 times ).

Example 2:

x=9 y=2 print(x % y) //Output: 1
9 % 2 evaluates to 1 because there's a remainder of 1 when 9 is divided by 2 . So, the Python modulo operator returns the remainder of a division operation, or 0 if there is no remainder. The modulo operator(%) is considered an arithmetic operation, along with +, –, /, *, **, //.

Modulo Operator With float


Python modulo operator
You can use modulo operator with a float, it will return the remainder of division, but as a float value

Example 1:

x=15.5 y=10.5 print(x % y) //Output: 5.0

Example 2:

x=15.5 y=2.3 print(x % y) //Output: 1.700000000000001

Modulo Operator With Negative numbers

You can use Modulo Operator with negative numbers. The sign of the result always will be the sign of the divisor.
x=10 y=-3 print(x % y) //Output: -2
x=-80 y=19 print(x % y) //Output: 15

Modulo Operator | ZeroDivisionError

x=15 y=0 print(x % y)
//Output: Traceback (most recent call last): File "./prog.py", line 5, in <module> ZeroDivisionError: integer division or modulo by zero
A ZeroDivisionError is raised when an attempt is made to divide a number by zero. This error occurs when the denominator of a division operation is zero.

Modulo Operation in Mathematics

The modulo operation can also be thought of as the "clock arithmetic" operation where the result of the modulo operation is always in the range of 0 to (divisor-1). This operation is useful in many branches of mathematics, including number theory, cryptography, and computer science.