Python Integer Division: How // Works and When to Use It
Understand Python's floor division with //, including negative operands, floats, and when to use divmod() for quotient and remainder.

In Python, // is the floor division operator. It divides two numbers and rounds the result down to the nearest integer, toward negative infinity. This differs from truncation toward zero, which many other languages use, and it matters most when negative numbers are involved.
How Integer Division Works in Python
For integer operands, // returns an integer result. Positive operands behave like standard integer division in languages such as C or Java:
print(7 // 2) # Output: 3
print(8 // 3) # Output: 2The result is the largest integer less than or equal to the exact quotient. Python applies this same floor rule consistently, including when operands are negative.
The Difference Between / and //
In Python 3, / is true division and always returns a float, even when both operands are integers:
print(7 / 2) # Output: 3.5
print(7 // 2) # Output: 3Use / when fractional precision matters and // when you need an integer result, such as for indexing, counting, or splitting data into chunks.
Negative Numbers and Floor Division
The most common source of confusion is how // handles negative numbers. Because floor division rounds toward negative infinity, the result is the nearest integer less than or equal to the quotient. For example:
print(-7 // 2) # Output: -4
print(7 // -2) # Output: -4In both cases, the exact quotient is -3.5, and the floor is -4. This differs from truncation toward zero, which would give -3. For small integer or float operands, you can get truncation behavior with int() on the true division result:
print(int(-7 / 2)) # Output: -3This distinction matters in algorithms that rely on consistent rounding, such as binary search or cyclic indexing.
Integer Division with Floats
// also works with float operands. In that case it returns a float, even though the value is mathematically an integer:
print(7.5 // 2) # Output: 3.0
print(-7.5 // 2) # Output: -4.0The result is the floor of the quotient, but it is represented as a float. This can be useful when you need to preserve the float type, but the value cannot be used directly where an int is required, such as a list index.
Using divmod() for Quotient and Remainder
When you need both the quotient and the remainder, Python's built-in divmod() function can compute them together. It returns a tuple (quotient, remainder) and follows the same floor semantics:
q, r = divmod(17, 5)
print(q) # Output: 3
print(r) # Output: 2For negative numbers, divmod() preserves the invariant q * divisor + remainder == dividend, and the remainder has the same sign as the divisor. This is consistent with Python's % operator.
Runtime Behavior
The // operator is built into Python and needs no import. It works with Python's arbitrary-precision integers without overflow, unlike fixed-width integer languages. If you need both the quotient and the remainder, prefer divmod() over separate // and % calls; it is a single call and can avoid computing the division twice.
Common Pitfalls and How to Avoid Them
A frequent mistake is assuming // truncates toward zero. This can lead to off-by-one errors with negative numbers, such as when calculating array indices for negative steps. Always confirm the rounding direction when negative values are possible.
Another pitfall is mixing // with floats when you need an integer type. Because // on floats returns a float, it may not be suitable for operations that require an int, such as list indexing. Convert explicitly with int() if you need an integer type.
When to Use Integer Division vs Other Approaches
Integer division is useful for splitting items into groups, calculating page numbers, or deriving repeat counts from a total. For example, to determine how many full groups of size n fit into a total m, use m // n. If you need to round up instead of down, use (m + n - 1) // n for positive integers.
For truncation toward zero, as in many C-style languages, int(m / n) works for values that can be represented exactly as floats. For very large integers, avoid converting through a float; use a small helper based on integer arithmetic instead:
def trunc_div(a, b):
sign = -1 if (a < 0) != (b < 0) else 1
return sign * (abs(a) // abs(b))This helper preserves integer precision while giving the same truncating behavior you would expect from C-style division.
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.