Apps Productivity How-To

How to Use // in Python for Floor Division

Learn what // does, how it differs from /, and when to use it with ints, floats, and negatives.

Learn what // does, how it differs from /, and when to use it with ints, floats, and negatives.

Python’s floor division operator // divides two numbers and rounds the result down to the nearest integer. Using it correctly prevents subtle rounding bugs, especially when porting integer-division logic or working with negative values.

Method 1: Compute Floor Division with //

Use // to get the quotient rounded down.
print(5 // 2)      # 2
print(10 // 3)     # 3
print(9 // 0.5)    # 18.0 (float operand → float result)
print(8.0 // 3)    # 2.0  (still floors)
Verify the division identity a == (a // b) * b + (a % b).
a, b = 17, 5
print(a == (a // b) * b + (a % b))  # True
Compare with / to see the difference.
print(5 / 2)   # 2.5 (true division)
print(5 // 2)  # 2   (floor division)

Method 2: Replace int(x / y) with x // y (when safe)

Find places where you divide and immediately convert to int.
# Before
mid = int(len(items) / 2)
Replace with floor division for clearer intent and fewer conversions.
# After
mid = len(items) // 2
Run tests to confirm identical results for positive numbers.
for n in range(1, 11):
    a = int(n / 2)
    b = n // 2
    assert a == b

Method 3: Get Quotient and Remainder Together with divmod

Use divmod(a, b) to get both floor division and modulus in one call.
duration = 500
minutes, seconds = divmod(duration, 60)
print(f"{minutes}:{seconds}")  # 8:20
Chain divmod for hours, minutes, and seconds.
duration = 9907
minutes, seconds = divmod(duration, 60)
hours, minutes = divmod(minutes, 60)
print(f"{hours}:{minutes:02d}:{seconds:02d}")  # 2:46:07

Method 4: Handle Negative Numbers Correctly

Check how // rounds with negatives (it always rounds down toward negative infinity).
print(-5 / 2)   # -2.5
print(-5 // 2)  # -3  (floors down)
Use int(x / y) or math.trunc(x / y) if you need truncation toward zero instead of flooring.
import math
print(int(-5 / 2))     # -2 (truncates toward zero)
print(math.trunc(-5/2))# -2
Prefer // only when floor behavior is required for negative results.
def pages_needed(total, per_page):
    # Floors down even for negatives; safe for totals ≥ 0
    return (total + per_page - 1) // per_page

Method 5: Update Values In-Place with //=

Use //= to floor-divide and assign back to the same variable.
x = 23
x //= 4
print(x)  # 5
Remember that mixing with floats returns a float.
y = 23
y //= 4.0
print(y, type(y))  # 5.0 <class 'float'>
Confirm that the operator expresses intent clearly when the remainder isn’t needed.
batch = 137
batch //= 10  # number of full tens
print(batch)  # 13

Quick tips:

  • // floors; / returns the exact quotient as a float.
  • With any float operand, // returns a float (but still floors the numeric result).
  • Use divmod(a, b) when you need both the quotient and remainder.
  • For negative values, be explicit about whether you want flooring (//) or truncation (int()/math.trunc()).

That’s the gist: reach for // when you want the quotient rounded down, and keep divmod in your toolkit when you also need the remainder.