2.8. Modulus operatorΒΆ
The modulus operator works on integers and yields the
remainder when the first operand is divided by the second. In Python,
the modulus operator is a percent sign ( %
). The syntax is the same as
for other operators:
So 7 divided by 3 is 2 with 1 left over.
The modulus operator turns out to be surprisingly useful. For example,
you can check whether one number is divisible by another: if x %
y
is zero, then x
is divisible by y
.
You can also extract the rightmost digit or digits from a number. For
example, x % 10
yields the rightmost digit of
x
(in base 10). Similarly, x % 100
yields the
last two digits.
 0
 This would be correct if it was
18 % 2
, but what is the remainder of 18 divided by 5?  1
 This would be correct if it was
18 % 17
, since 17 goes into 18 one time and the remainder is 18  17 = 1.  2
 What is the highest multiple of 5 that is less than or equal to 18? The remainder is 18  that number.
 3
 The reminder is 3 since 5 goes into 18 three times (15) and 18  15 = 3.
csp1022: What is the result of 18 % 5
?
 2
 The remainder when a smaller number is divided by a larger number is the smaller number.
 8
 This would be true if it was 2 raised to the 3rd power, but it is modulus.
 1
 This would be true if it was
3 % 2
.  0.66666
 This would be true if it was floating point division, but it is modulus.
csp1023: What is the result of 2 % 3
?

csp1024: Match each expression with the type of division the operator performs.
Try out these expressions in terminal or review the operators and operands section.
 7 / 2
 division
 90 // 4
 floored division
 45 % 6
 remainder
csp1025: What is the result of 25 % 4
?
You have attempted of activities on this page