Modulo & Integer
Division in Python
Master the % and // operators: two essential tools for breaking numbers apart, detecting patterns, and solving real-world programming problems.
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.
Meet the % Operator: Modulo
The modulo operator returns the remainder after dividing the left operand by the right. In Python you write it as %.
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 //.
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:
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.
/ 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:
How to Apply % and //
Example 1: Even or Odd
Problem: Write a function that returns "Even" or "Odd" for any integer.
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.
# 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.
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?
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) # 3 (not -2!)
# Verify: 5 * (-4) + 3 = -20 + 3 = -17 ✓
Common Mistakes
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?