01 / 20
% //

Modulo & Integer
Division in Python

Master the % and // operators: two essential tools for breaking numbers apart, detecting patterns, and solving real-world programming problems.

Python

What Is Division, Really?

When you divide \(17 \div 5\), you get \(3\) with a remainder of \(2\). In elementary school you wrote this as: \(17 = 5 \times 3 + 2\). Python gives you two separate operators to extract each piece of that equation.

17 = 5 × 3 + 2 dividend divisor quotient
\(17 \mathbin{//} 5\)
remainder
\(17 \mathbin{\%} 5\)

Meet the % Operator: Modulo

The modulo operator returns the remainder after dividing the left operand by the right. In Python you write it as %.

# Modulo returns the remainder
print(17 % 5)  # Output: 2
print(20 % 4)  # Output: 0
print(7 % 3)   # Output: 1

Meet the // Operator: Floor Division

Integer division (also called floor division) returns the quotient rounded down to the nearest integer. In Python you write it as //.

# Floor division returns the whole-number quotient
print(17 // 5)  # Output: 3
print(20 // 4)  # Output: 5
print(7 // 3)   # Output: 2

The Core Identity

These two operators are bound together by a fundamental identity that is always true in Python:

DIVISION IDENTITY
$$ \text{dividend} = \text{divisor} \times (\text{dividend} \mathbin{//} \text{divisor}) + (\text{dividend} \mathbin{\%} \text{divisor}) $$
# Verify the identity for any a, b
a, b = 17, 5
print(a == b * (a // b) + (a % b))  # True
# 17 == 5 * 3 + 2 → 17 == 17 ✓

Visualizing Modulo on a Number Line

Think of \(17 \mathbin{\%} 5\): you count by 5's along the number line. The last full group lands at 15. The gap from 15 to 17 is the remainder.

0 5 10 15 17 20 3 full groups of 5 remainder = 2

/ vs //: The Key Distinction

Python has two division operators. Confusing them is one of the most common bugs.

/ (True Division) // (Floor Division)
Returns A float An int (or floored float)
\(17 \div 5\) 3.4 3
\(20 \div 4\) 5.0 5
\(-17 \div 5\) -3.4 -4
Use when You need the exact result You need whole groups / indices

Why % and // Matter

These operators appear everywhere in real programming. Here are the most common use cases:

Even / Odd
\(n \mathbin{\%} 2 == 0\) checks if a number is even.
Cycling / Wrapping
\(\text{index} \mathbin{\%} \text{length}\) wraps an index back to the start of a list.
Digit Extraction
\(n \mathbin{\%} 10\) gets the last digit. \(n \mathbin{//} 10\) removes it.
Time Conversion
\(\text{minutes} \mathbin{//} 60\) gives hours. \(\text{minutes} \mathbin{\%} 60\) gives leftover minutes.

How to Apply % and //

1
Identify what you need: Do you want the whole-number quotient, or the remainder?
2
Choose the operator: Use // for "how many full groups?" Use % for "what's left over?"
3
Write the expression: Put the dividend on the left, divisor on the right: \(a \mathbin{//} b\) or \(a \mathbin{\%} b\).
4
Verify with the identity: Check that \(b \times (a \mathbin{//} b) + (a \mathbin{\%} b) == a\).

Example 1: Even or Odd

Problem: Write a function that returns "Even" or "Odd" for any integer.

def even_or_odd(n):
    if n % 2 == 0:
        return "Even"
    else:
        return "Odd"

print(even_or_odd(42))  # "Even"
print(even_or_odd(7))   # "Odd"

Example 2: Extracting Digits

Problem: Given the number 4829, extract each digit from right to left.

n = 4829

# Last digit
print(n % 10)   # 9
n = n // 10      # n = 482

print(n % 10)   # 2
n = n // 10      # n = 48

print(n % 10)   # 8
n = n // 10      # n = 4

print(n % 10)   # 4

Example 3: Seconds to H:M:S

Problem: Convert 7385 seconds into hours, minutes, and seconds.

total = 7385

hours = total // 3600       # 2
remaining = total % 3600   # 185

minutes = remaining // 60   # 3
seconds = remaining % 60    # 5

print(f"{hours}h {minutes}m {seconds}s")
# Output: 2h 3m 5s

Example 4: Wrapping Around a List

Problem: Cycle through the days of the week. If today is index 5 (Saturday), what day is it 10 days from now?

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

today = 5                # Saturday
future = (today + 10) % 7  # (15) % 7 = 1

print(days[future])        # "Tue"

Negative Numbers: The Tricky Part

When negatives are involved, Python's // rounds toward negative infinity, not toward zero. This affects % as well.

print(-17 // 5)  # -4 (not -3!)
print(-17 % 5)   # 3 (not -2!)

# Verify: 5 * (-4) + 3 = -20 + 3 = -17 ✓
-20 -17 -15 -10 -5 0 floor to -20 remainder = 3

Common Mistakes

✕ Using / instead of //
Writing \(17 / 5\) gives 3.4 (a float), not 3. If you need an integer, always use //.
✕ Assuming truncation
In Python, \(-17 \mathbin{//} 5 = -4\), not -3. It floors toward negative infinity, unlike C or Java which truncate toward zero.
✕ Dividing by zero
Both \(a \mathbin{//} 0\) and \(a \mathbin{\%} 0\) raise a ZeroDivisionError. Always guard against zero divisors.

Cheat Sheet: % and // at a Glance

Expression Result Why
\(17 \mathbin{//} 5\) 3 3 full groups of 5 fit in 17
\(17 \mathbin{\%} 5\) 2 17 − 15 = 2 left over
\(20 \mathbin{\%} 4\) 0 20 is evenly divisible by 4
\(-17 \mathbin{//} 5\) -4 Floor rounds toward -∞
\(-17 \mathbin{\%} 5\) 3 \(5 \times (-4) + 3 = -17\)
\(10 \mathbin{\%} 10\) 0 Wraps back to 0

Question 1 of 5

What is the output of \(23 \mathbin{\%} 7\) in Python?

Question 2 of 5

What is the output of \(45 \mathbin{//} 6\) in Python?

Question 3 of 5

Which expression correctly checks if a variable \(n\) is divisible by 3?

Question 4 of 5

What is the value of \(-13 \mathbin{//} 4\) in Python?

Question 5 of 5

You have \(\text{total_seconds} = 5000\). Which expression correctly computes the number of complete minutes?