It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem. The modulo operator is considered an arithmetic operation, along with + , - , / , * , ** , // . In the previous example a is divided by b , and the remainder is returned.
As we said, a mod b is simply an expression representing the remainder when we divide a by b. Therefore, if a / b = q remainder r, then a mod b = r.
The Python modulo operator calculates the remainder of dividing two values. This operator is represented by the percentage sign (%). The syntax for the modulo operator is: number1 % number2. The first number is divided by the second then the remainder is returned.
Definition(s): The modulo operation of integers a and b. “a mod b” returns the remainder after dividing a by b.
There's the %
sign. It's not just for the remainder, it is the modulo operation.
you can also try divmod(x, y)
which returns a tuple (x // y, x % y)
>>> 15 % 4
3
>>>
The modulo gives the remainder after integer division.
mod = a % b
This stores the result of a mod b
in the variable mod
.
And you are right, 15 mod 4
is 3, which is exactly what python returns:
>>> 15 % 4
3
a %= b
is also valid.
Why don't you use % ?
print 4 % 2 # 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With