Floor Division in Python


TL ; DR:

  • Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

    print(10 // 2) # Output: 5
    print(15 // 4) # Output: 3
    print(20 // 3) # Output: 6
    

  • It can also be used with variables:

    a = 3
    b = 7
    
    print(20 // a) # Output: 6
    print(b // a) # Output: 2
    





Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

Floor division

Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6

It can also be used with variables:

a = 3
b = 7

print(20 // a) # Output: 6
print(b // a) # Output: 2

Modulo

The modulo operator (%) calculates the remainder of dividing two values:

print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2

# Can be used with variables:
a = 2
b = 4

print(b % a) # Output: 0
print(11 % b) # Output: 3

Quotient and remainder

In programming, we combine both these concepts to get the quotient and remainder of some divison:

# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3

print(quotient) # Output: 8
print(remainder) # Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


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 // b gives the integer quotient; a % b gives 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 %.