Reputation: 36414
Is there a modulo function in the Python math
library?
Isn't 15 % 4
, 3? But 15 mod 4
is 1, right?
Upvotes: 175
Views: 744798
Reputation: 355
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.
Upvotes: 28
Reputation: 1460
you can also try divmod(x, y)
which returns a tuple (x // y, x % y)
Upvotes: 60
Reputation: 907
I don't think you're fully grasping modulo. a % b
and a mod b
are just two different ways to express modulo. In this case, python uses %
. No, 15 mod 4
is not 1
, 15 % 4 == 15 mod 4 == 3
.
Upvotes: 5
Reputation: 406135
>>> 15 % 4
3
>>>
The modulo gives the remainder after integer division.
Upvotes: 43
Reputation: 40272
There's the %
sign. It's not just for the remainder, it is the modulo operation.
Upvotes: 263