• Home
  • 3.12 Modulus Operator

3.12 Modulus Operator

View Categories

3.12 Modulus Operator

2 min read

The modulus operator (%) returns the remainder after dividing one number by another.

Unlike the division operator (/), which returns the quotient, the modulus operator tells you what is left over after the division.

The modulus operator is commonly used for checking whether a number is even or odd, finding cyclic patterns, validating input, and determining if one number is divisible by another.


Syntax #

result = dividend % divisor

Components #

Component Description
dividend The number to be divided.
% Modulus operator.
divisor The number that divides the dividend.
result The remainder after division.

Example 1: Finding the Remainder #

main.py

print(17 % 5)

Output #

2

Explanation #

  • 17 รท 5 gives a quotient of 3 with a remainder of 2.
  • The modulus operator returns the remainder.
  • Therefore, the result is 2.

Example 2: Exact Division #

main.py

print(20 % 4)

Output #

0

Explanation #

  • 20 is exactly divisible by 4.
  • No remainder is left after the division.
  • Therefore, the result is 0.

Example 3: Checking Even or Odd #

main.py

number = 17

print(number % 2)

Output #

1

Explanation #

  • Dividing 17 by 2 leaves a remainder of 1.
  • A remainder of 1 indicates an odd number.
  • A remainder of 0 indicates an even number.

Example 4: Modulus with Floating-Point Numbers #

main.py

print(10.5 % 3)

Output #

1.5

Explanation #

  • The modulus operator also works with floating-point numbers.
  • 10.5 รท 3 leaves a remainder of 1.5.
  • Python returns the floating-point remainder.

Example 5: Negative Dividend #

main.py

print(-17 % 5)

Output #

3

Explanation #

  • Python’s modulus operator follows floor division.
  • The result has the same sign as the divisor.
  • Therefore, -17 % 5 evaluates to 3.

Example 6: Negative Divisor #

main.py

print(17 % -5)

Output #

-3

Explanation #

  • The divisor is negative.
  • The remainder also has the sign of the divisor.
  • Therefore, the result is -3.

Example 7: User Input #

main.py

number = int(input("Enter a number: "))

print(number % 2)

Sample Input #

24

Output #

0

Explanation #

  • The user enters an integer.
  • The modulus operator calculates the remainder after division by 2.
  • Since the remainder is 0, the number is even.

Example 8: Finding the Last Digit #

main.py

number = 5387

print(number % 10)

Output #

7

Explanation #

  • Dividing by 10 leaves the last digit as the remainder.
  • Therefore, the last digit of 5387 is 7.

Example 9: Using Modulus in an Expression #

main.py

result = (15 + 8) % 6

print(result)

Output #

5

Explanation #

  • First, 15 + 8 equals 23.
  • Then 23 % 6 is calculated.
  • The remainder is 5.

Example 10: Checking the Return Type #

main.py

print(type(17 % 5))
print(type(17.5 % 5))

Output #

<class 'int'>
<class 'float'>

Explanation #

  • When both operands are integers, the result is an integer.
  • If either operand is a floating-point number, the result is a float.

Common Mistakes #

1. Confusing Modulus with Division #

Incorrect

print(15 % 4)

Incorrect Expectation #

3.75

Actual Output #

3

Reason

The modulus operator returns the remainder, not the quotient.


2. Assuming Negative Results Behave Like Other Languages #

Incorrect

print(-10 % 3)

Incorrect Expectation #

-1

Actual Output #

2

Reason

Python calculates the remainder based on floor division.
The remainder has the same sign as the divisor.


3. Dividing by Zero #

Incorrect

print(10 % 0)

Output #

ZeroDivisionError: integer modulo by zero

Reason

The divisor cannot be zero.


4. Expecting the Original Variable to Change #

Incorrect

number = 19

number % 5

print(number)

Output #

19

Reason

The modulus operator returns a new value.
It does not modify the original variable.

Correct

number = number % 5

print(number)

Best Practices #

  • Use % to determine whether a number is divisible by another number.
  • Use % 2 when checking whether a number is even or odd.
  • Remember that the remainder follows Python’s floor division rules for negative numbers.
  • Ensure the divisor is not zero before using the modulus operator.
  • Store the result if it will be reused later.

Key Points to Remember #

  • % is the modulus operator in Python.
  • It returns the remainder after division.
  • If the remainder is 0, the dividend is exactly divisible by the divisor.
  • The modulus operator works with integers and floating-point numbers.
  • The remainder follows Python’s floor division rules and has the same sign as the divisor.
  • Dividing or taking the modulus by zero raises a ZeroDivisionError.
  • The modulus operator is commonly used for checking divisibility, even/odd numbers, and extracting digits.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *