Python Modulus Operator

The modulus operator (%) in Python is used to find the remainder of a division operation. It is a binary operator, meaning it works with two operands: the dividend and the divisor. Here’s a comprehensive breakdown:

Syntax

remainder = dividend % divisor
  • dividend: The number to be divided.
  • divisor: The number by which the dividend is divided.
  • remainder: The result of the modulus operation.

How Modulus Works

The result of the modulus operation is the remainder left after division. For example:

  • If a = 10 and b = 3, dividing 10 by 3 gives:

10÷3=3 (quotient), remainder = 10−(3×3)=1

So, 10 % 3 = 1.

Examples and Outputs

1. Basic Examples

print(10 % 3)  # Output: 1 (10 ÷ 3 = 3 remainder 1)
print(20 % 5)  # Output: 0 (20 ÷ 5 = 4 remainder 0)
print(7 % 4)   # Output: 3 (7 ÷ 4 = 1 remainder 3)

Output:

1
0
3

2. Modulus with Negative Numbers

Python ensures that the remainder has the same sign as the divisor.

print(10 % -3)   # Output: -2 (10 - (-3 * -4) = -2)
print(-10 % 3)   # Output: 2  (-10 - (3 * -4) = 2)
print(-10 % -3)  # Output: -1 (-10 - (-3 * 3) = -1)

Output:

-2
2
-1

Applications of the Modulus Operator

1. Determine if a Number is Even or Odd

  • A number is even if it is divisible by 2 (remainder = 0).
  • A number is odd if the remainder is 1 when divided by 2.
num = 7
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Output:

Odd

2. Check for Divisibility

The modulus operator can be used to check if one number is divisible by another. If dividend % divisor == 0, the number is divisible.

number = 15
if number % 5 == 0:
    print(f"{number} is divisible by 5")
else:
    print(f"{number} is not divisible by 5")

Output:

15 is divisible by 5

3. Cycle Through a Sequence

The modulus operator helps cycle through a sequence, such as wrapping indices in a circular manner.

for i in range(10):
    print(i % 3, end=" ")

Output:

0 1 2 0 1 2 0 1 2 0

4. Handle Circular Indexing

When working with lists or arrays, the modulus operator can “wrap” the index to stay within bounds.

items = ['a', 'b', 'c']
index = 5
print(items[index % len(items)])  # Wraps index 5 to 2

Output:

c

Modulus with Floating-Point Numbers

The modulus operator also works with floating-point numbers.

print(10.5 % 4)  # Output: 2.5 (10.5 - 4 * 2 = 2.5)
print(15.7 % 2.3)  # Output: 0.8999999999999999 (small floating-point precision errors)

Output:

2.5
0.8999999999999999

Relation Between Modulus and Floor Division

The modulus operator and floor division operator (//) are closely related:

  1. Floor division gives the quotient (integer part of division).
  2. Modulus gives the remainder.

Example:

dividend = 10
divisor = 3

quotient = dividend // divisor
remainder = dividend % divisor

print(f"Quotient: {quotient}")  # Output: 3
print(f"Remainder: {remainder}")  # Output: 1

Output:

Quotient: 3
Remainder: 1

Key Formula

The result of the modulus operator satisfies the equation:

dividend=(divisor×quotient)+remainder

Example: For 10 % 3:

  • Dividend = 10
  • Divisor = 3
  • Quotient = 3
  • Remainder = 1

10=(3×3)+1

Practice Problems

1. Check if a Number is Divisible by Both 3 and 5

num = 30
if num % 3 == 0 and num % 5 == 0:
    print(f"{num} is divisible by both 3 and 5")
else:
    print(f"{num} is not divisible by both 3 and 5")

Output:

30 is divisible by both 3 and 5

2. Find the Remainder of Two User-Input Numbers

dividend = int(input("Enter the dividend: "))
divisor = int(input("Enter the divisor: "))

if divisor != 0:
    remainder = dividend % divisor
    print(f"The remainder when {dividend} is divided by {divisor} is {remainder}")
else:
    print("Error: Division by zero is not allowed!")

Example Input:

Enter the dividend: 23
Enter the divisor: 4

Output:

The remainder when 23 is divided by 4 is 3

3. Rotate Through a List Using Modulus

items = ['Python', 'Java', 'C++']
for i in range(10):
    print(items[i % len(items)])

Output:

Python
Java
C++
Python
Java
C++
Python
Java
C++
Python