Python Double Slash //: Floor Division Explained

Quick answer: Python’s double slash is the floor-division operator. It divides and rounds down toward negative infinity, so its result differs from truncation for negative operands.

Python double slash infographic comparing true division, floor division, negative operands, and divmod
Python // returns the floor of the quotient, which differs from truncation when operands are negative.

Python double slash, written as //, performs floor division. It divides the left operand by the right operand and returns the quotient rounded down to the floor. For positive whole-number counts, this often looks like normal integer division.

The official Python binary arithmetic operations reference defines /, //, and %. The numeric types documentation describes how numeric operators behave across integers and floats.

Use // when you want the floor quotient from a division. Common examples include page counts, chunk indexes, grid positions, time conversion, and splitting totals into fixed-size groups. Use / when you need the exact division result as a floating-point value.

The floor part matters. For positive inputs, 17 // 5 returns 3. For negative inputs, floor division moves lower on the number line, so -17 // 5 returns -4, not -3. That behavior is correct, but it surprises people who expect truncation toward zero.

Do not use // only to remove a decimal part from a negative number. If your rule says to drop the fractional part toward zero, use math.trunc() or int() instead. If your rule says to move to the lower integer, // and math.floor() fit that model.

It is also worth naming the result carefully. A result from // is usually a count of complete groups, a lower bucket, or a floor quotient. Names such as full_pages, complete_batches, or row_index make the rounding rule easier to understand than a vague name such as result.

When input comes from configuration or a form, convert and validate it before division. Floor division can only be as correct as the numbers you pass into it. A zero divisor, negative page size, or string that was not parsed correctly should be handled before the arithmetic step.

Compare Slash And Double Slash

The single slash operator returns true division. The double slash operator returns floor division.

print(17 / 5)
print(17 // 5)
print(20 / 5)
print(20 // 5)

The first result is 3.4, while 17 // 5 returns 3. When the division is exact, / still returns a float for integer inputs, while // returns an integer.

That type difference can matter when the result is used as a list index, count, or range boundary. Counts should normally stay as integers.

If you mix integers and floats, review the result type before passing it to APIs such as range() or list indexing. Those APIs need integer values, so a floored float such as 3.0 may still need an explicit conversion after you decide that conversion is safe.

Use Floor Division For Counts

Floor division is useful when calculating complete groups.

items = 47
page_size = 10

full_pages = items // page_size
leftover = items % page_size

print(full_pages)
print(leftover)

This says there are four complete pages and seven leftover items. If a partial page should also be shown, add one more page when the remainder is nonzero.

Pairing // with % is common because the quotient and remainder answer different parts of the same grouping question.

For user interfaces, this pattern helps explain what is happening: complete pages are displayed normally, and a leftover count decides whether a final partial page exists. For batch jobs, it can decide how many full chunks are processed before a smaller final chunk.

Handle Negative Numbers Carefully

Floor division follows floor rules, not truncation rules.

pairs = [(17, 5), (-17, 5), (17, -5), (-17, -5)]

for left, right in pairs:
    print(left, right, left // right)

The negative cases show why tests should include both positive and negative inputs when a formula may cross zero. Floor division rounds down, so it can move away from zero.

If negative values do not make sense for the domain, reject them before the calculation instead of quietly accepting a mathematically valid but meaningless result.

For coordinate systems, negative floor division may be exactly what you want because grid buckets extend in both directions. For counts, inventory, and pagination, negative values usually indicate bad input. The operator is the same, but the domain rule is different.

Use divmod When You Need Both Parts

divmod(a, b) returns the same quotient and remainder pair as (a // b, a % b).

total_seconds = 367

minutes, seconds = divmod(total_seconds, 60)

print(minutes)
print(seconds)

Use divmod() when both parts belong together. Use // alone when the quotient is all you need, and use % alone when only the remainder matters.

This distinction keeps code compact without hiding intent. A reader can tell whether the result is a count of complete groups or a quotient-and-leftover pair.

Floor Division With Floats

If either operand is a float, floor division returns a float result that represents the floored quotient.

print(17.0 // 5)
print(17 // 5.0)
print(-17.0 // 5)

The value may look like an integer, but the type is float. Convert deliberately if a later API requires an integer count.

For exact decimal policy, use the decimal module rather than relying on binary floating-point arithmetic.

In reporting code, keep the calculation result and display formatting separate. Use // to compute the floor quotient, then format the value for output after the numeric rule is complete.

Avoid Zero Divisors

Like normal division, floor division raises an error when the right operand is zero.

def safe_floor_divide(total, size):
    if size == 0:
        return None
    return total // size

print(safe_floor_divide(10, 3))
print(safe_floor_divide(10, 0))

In real code, choose the error-handling rule that matches the domain. Returning None, raising a custom error, or rejecting bad input before calculation are all better than letting a hidden zero crash an unrelated part of the program.

In short, use // for floor quotients, / for true division, % for remainders, and divmod() when quotient and remainder should be calculated together. Always think through negative inputs and zero divisors before using floor division in production logic.

True Division Versus Floor Division

A single slash answers a measurement question and returns true division. Double slash answers how many whole floor-divided units fit, while preserving the floor rule for negative values. With integers the result is an integer; with floats Python still applies floor division and returns a float.

values = [(7, 2), (-7, 2), (7, -2)]

for left, right in values:
    print(left, right, left / right, left // right)

Negative Values Round Down

Flooring is not the same as cutting off the decimal part. The floor of -3.5 is -4, which is why -7 // 2 returns -4. When you need truncation toward zero, make that requirement explicit instead of assuming // provides it.

import math

quotient = -7 / 2
print(-7 // 2)
print(math.trunc(quotient))

Get The Quotient And Remainder Together

divmod(a, b) returns the floor-divided quotient and a remainder that satisfy a == b * quotient + remainder. This is useful for clock arithmetic, pagination, chunking, and converting a count into units plus a remainder.

total_seconds = 3675
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print(hours, minutes, seconds)

See Python’s binary arithmetic operation rules when the operands may be floats, decimals, or custom numeric types.

For nearby numeric operations, compare rounding down in Python with the division and remainder rules in this guide.

Frequently Asked Questions

What does // mean in Python?

The // operator performs floor division and returns the greatest integer less than or equal to the quotient.

Why is -7 // 2 equal to -4?

Python floors toward negative infinity, and floor(-3.5) is -4; it does not truncate toward zero.

What is the difference between / and //?

A single slash returns true division, usually a float, while // returns a floor-divided result.

How do I get both quotient and remainder?

Use divmod(a, b), which returns a pair whose quotient matches floor division and whose remainder satisfies the division identity.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted