0
why would you use %= in Python?
I understand how it works, but in what situation would you use this?
1 Resposta
+ 1
Modulo operator is not a python-specific thing. It is widely used in mathematics, for example in calculating greatest common divisor:
-----
from functools import reduce
numlist = [54, 144, 18]
def gcd(num1, num2):
res = num1 % num2
if res == 0:
return num2
else:
return gcd(num2, res)
print(reduce(gcd,numlist))
-----
Modulo is the key tool for Fermat's Little Theorem primality test, and has lots of other uses.