The Python modulo operator, %, is used to retrieve the remainder after a division.
The basic syntax of the python modulo used in an equation is as follows:
# Syntax to get a remainder from a division in Pythonargument1 % argument2 = remainder of (argument1/argument2)
In this example, when we divide the first argument, 6, by the second argument, 2 - the result of the division is 3 with 0 as a remainder.
So in Python, 6 % 2 = 0
In this example, when we divide the first argument, 9, by the second argument, 7 - the result of the division is $ {1}\frac{2}{7}$ with 2 as a remainder.
So in Python, 9 % 7 = 2
The only exception you get with a python modulo operator ā%ā is when the second argument is 0. This means that the divider operand of the modulo operator cannot be zero.
For example:
argument1 = 8argument2 = 0exceptionRemainder = argument1 % argument2print(exceptionRemainder)
Free Resources