Modulus OperatorΒΆ
The modulus operator %
can be used to find the remainder resulting from dividing one integer by another. If a
and b
are integers, then the expression a % b
will evaluate to the remainder resulting from dividing a
by b
.
print(37 % 5)
2
print(38 % 5)
3
We can use the modulus operator to determine if one number is evenly divisible by another.
x = 42
print(x % 7 == 0)
True
x = 32
print(x % 7 == 0)
False
x = 17
print((x > 5) and (x % 2 == 1))
True