tags:

views:

303

answers:

5

is there any modulo function in math lib in python?

isn't 15 % 4 3?? but 15 mod 4 is 1, right??

+3  A: 

There's the % sign. It's not just for the remainder, it is the modulo operation.

eduffy
+3  A: 

Why don't you use % ?


print 4 % 2 # 0
Geo
+3  A: 

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
>>> `

@Macarse: Yes, but that is true for any \= construct in python, so I should think there's no need to point that out seperately for modulo.

Merijn
Note: a %= b is also valid.
Macarse
+1  A: 
>>> 15 % 4
3
>>>

The modulo gives the remainder after integer division.

Bill the Lizard
+2  A: 

you can also try divmod(x, y) which returns a tuple (x / y, x % y)

paffnucy