Python Floor Division Operator Explained
Understand how Python's floor division operator (//) handles negative numbers and floats, differs from true division, and appears in practical code.

Python's floor division operator (//) divides two numbers and rounds the result down to the nearest integer. It is a core arithmetic operator, but its behavior with negative numbers and floats often surprises developers coming from languages that truncate toward zero. This article explains how it works, how it differs from true division, and how to use it correctly in real code.
How the Floor Division Operator Works
Floor division returns the largest integer less than or equal to the exact quotient. For positive operands, this behaves like integer division in many languages:
print(7 // 2) # 3
print(8 // 2) # 4
print(9 // 3) # 3The operator works on integers and floats. When both operands are integers, the result is an integer. If either operand is a float, the result is a float, but still rounded down to an integer value:
print(7.0 // 2) # 3.0
print(7 // 2.0) # 3.0
print(7.5 // 2) # 3.0The key point is that the result is always rounded down, not truncated. This distinction becomes critical with negative numbers.
Floor Division with Negative Numbers
The most common source of confusion is how // handles negative operands. Consider:
print(-7 // 2) # -4
print(7 // -2) # -4
print(-7 // -2) # 3Many developers expect -7 // 2 to return -3 because truncation toward zero would give -3. Instead, floor division rounds down to the nearest lower integer, so -3.5 becomes -4. This is consistent with the mathematical definition of floor: the largest integer less than or equal to the value.
This behavior is not a bug; it is intentional and matches the semantics of math.floor(). For example:
import math
print(math.floor(-7 / 2)) # -4
print(-7 // 2) # -4If you need truncation toward zero, use int() on the true division result:
print(int(-7 / 2)) # -3But note that int() truncates toward zero for floats, which is different from floor for negative numbers.
Difference Between Floor Division and True Division
In Python 3, true division (/) always returns a float, even when the operands are integers. Floor division (//) returns an integer or float depending on the operands, but always rounds down. Here is a side-by-side comparison:
| Expression | True Division (/) | Floor Division (//) |
|---|---|---|
7 / 2 | 3.5 | 3 |
-7 / 2 | -3.5 | -4 |
7 / -2 | -3.5 | -4 |
8 / 2 | 4.0 | 4 |
True division is the standard mathematical division. Floor division is useful when you need an integer result and are certain that rounding down is the desired behavior. For example, when computing indices, counts, or steps where you want to discard the remainder.
Floor Division with Floats and Large Numbers
When either operand is a float, the result is a float, but the value is still the floor of the division. Floating-point numbers are binary approximations, so the floor can differ from a decimal calculation. For example:
print(10.0 // 3) # 3.0
print(0.1 // 0.01) # 9.0 (not 10.0)Because 0.1 and 0.01 are not exact binary fractions, the division yields a value slightly less than 10, and floor rounds down to 9. If you need exact decimal arithmetic, use the decimal module or work with integers.
Large integers do not have this problem. Python's integers are arbitrary precision, so // computes exact results:
print(10**30 // 7) # 142857142857142857142857142857Using Floor Division with math.floor and int()
You might wonder when to use // versus math.floor() or int(). The operator is more concise and avoids an extra function call. However, math.floor() works on any real number and returns an integer, while // returns a float if any operand is a float. Consider:
import math
print(math.floor(7.5)) # 7
print(7.5 // 1) # 7.0If you need an integer type, use int() around // when floats are involved:
print(int(7.5 // 1)) # 7But for most cases, // is the idiomatic choice for integer floor division. math.floor() is more appropriate when you already have a float and want the mathematical floor, not necessarily division.
Common Pitfalls and Practical Considerations
One common mistake is assuming // is the same as int(a / b). As shown, they differ for negative numbers. Another pitfall is mixing // with the modulo operator %. For integer operands, a // b and a % b satisfy the identity a == (a // b) * b + (a % b), which means the sign of the remainder follows the divisor. This is useful for algorithms that rely on consistent rounding, such as circular indexing.
For example, to get a positive index for negative values:
def wrap_index(i, n):
return i % n
print(wrap_index(-1, 5)) # 4Here, % uses floor division semantics, so the result is always in [0, n-1].
Performance-wise, // is implemented at the C level and is as fast as any arithmetic operation. There is no need to micro-optimize; the main concern is correctness with negative numbers and floats.
Floor Division in Real-World Code
Floor division appears in many practical scenarios. Pagination is a classic example: given a list and a page size, you compute the page number for an index:
def page_number(index, page_size):
return index // page_size
print(page_number(15, 10)) # 1
print(page_number(20, 10)) # 2Another use is converting seconds to minutes and hours:
total_seconds = 3675
minutes = total_seconds // 60
seconds = total_seconds % 60
print(f"{minutes} minutes and {seconds} seconds") # 61 minutes and 15 secondsIn algorithms like binary search, // is used to compute an integer midpoint:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1These examples show that // is not just a syntax quirk; it is a tool for writing clear, correct code when you need to round down.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.