Python Floor Division (//): Integer Quotient Without the Decimal
Regular division in Python always returns a float — 7 / 2 gives 3.5. But countless real problems need only the whole-number part: how many full rows fit on a page, how many complete hours in a duration, which "bucket" an item falls into. The // operator gives you that integer quotient directly, in one step, with no rounding or conversion needed. Paired with % for the remainder, it's one of the most practically useful operator pairs in Python.
// vs / : The Key Difference
print(10 / 3) # 3.3333333333333335 — true division, always float
print(10 // 3) # 3 — floor division, integer quotient only
print(10 % 3) # 1 — modulo, the remainder
# They always satisfy this relationship:
# (a // b) * b + (a % b) == a
# (3) * 3 + 1 == 10 ✓
The Identity: // and % Always Add Up
Floor division and modulo are two halves of the same operation. This invariant always holds:
# For any a and b (b ≠ 0):
# (a // b) * b + (a % b) == a
a, b = 26, 3
quotient = a // b # 8
remainder = a % b # 2
print(quotient * b + remainder == a) # True (8*3 + 2 = 26)
Real-World Patterns
# 1. Convert seconds to minutes and seconds:
total_seconds = 137
minutes = total_seconds // 60 # 2
seconds = total_seconds % 60 # 17
print(f"{minutes}m {seconds}s") # 2m 17s
# 2. Binary search midpoint (avoids overflow in other languages):
left, right = 0, 100
mid = (left + right) // 2 # 50
# 3. Distribute items into rows:
items = 29
per_row = 4
full_rows = items // per_row # 7 (full rows)
leftover = items % per_row # 1 (items in the partial row)
print(f"{full_rows} full rows, {leftover} left over")
# 4. Pagination — total pages needed:
total_items = 95
per_page = 10
pages = (total_items + per_page - 1) // per_page # 10 (ceiling division trick)
print(f"{pages} pages")
divmod(): Get Both at Once
Python has a built-in divmod(a, b) that returns (a // b, a % b) as a tuple — cleaner when you need both:
quotient, remainder = divmod(26, 3)
print(quotient) # 8
print(remainder) # 2
# Same as:
# quotient = 26 // 3
# remainder = 26 % 3
Negative Numbers: Floors Toward −∞, Not Toward Zero
This is the most important gotcha. // always floors toward negative infinity — not toward zero. That means it rounds down for negative results, not toward zero:
print(-10 // 3) # -4 (not -3!)
# -10 / 3 = -3.333... → floor(-3.333) = -4
print(10 // -3) # -4 (same logic)
print(-10 // -3) # 3 (both negative → positive quotient, floors normally)
# Compare with truncation-toward-zero (which int() does):
import math
print(math.trunc(-10 / 3)) # -3 (truncates toward zero)
print(-10 // 3) # -4 (floors toward -∞)
In practice: when both numbers are positive (the common case), // behaves exactly as you'd expect. Be mindful with negatives.
Key Takeaways
a // bgives the integer quotient;a % bgives the remainder. Together they always satisfy(a // b) * b + (a % b) == a.- Use
divmod(a, b)when you need both at once. //floors toward −∞, not toward zero — matters for negative numbers.- Common uses: time conversion, binary search midpoint, pagination, distributing items into rows.
What to Learn Next
With // and % solid, explore the full set of Python arithmetic operators: exponentiation (**) and augmented assignment (//=, %=) for updating variables in place. Then apply % to practical algorithm problems: checking even/odd (n % 2 == 0), circular array indexing (i % len(arr)), and FizzBuzz — a classic interview exercise built entirely on %.
