How to calculate a mod b in Python?
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