Skip to main content

Section 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 right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly, x % 100 yields the last two digits.

Checkpoint 2.8.1.

    csp-10-2-2: What is the result of 18 % 5?
  • 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.

Checkpoint 2.8.2.

    csp-10-2-3: What is the result of 2 % 3?
  • 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.

Checkpoint 2.8.3.

Checkpoint 2.8.4.

csp-10-2-5: What is the result of 25 % 4?