Python //= Operator: In-Place Floor Division
Learn how Python's //= operator performs floor division and assigns the result in one step, with examples and edge cases.

The //= operator is an augmented assignment in Python that combines floor division with variable assignment. When you write x //= y, Python evaluates x // y and assigns the result back to x. This operator is part of the family of augmented assignment operators, which also includes +=, -=, *=, and /=. It is particularly useful when you need to repeatedly halve a value, implement integer division in a loop, or keep code concise without losing clarity.
How Floor Division Works in Python
Floor division, denoted by //, divides two numbers and rounds the result down to the nearest integer. It differs from true division (/), which always returns a float, and from truncation, which rounds toward zero. For positive numbers, floor division and truncation produce the same result, but for negative numbers they differ because floor division rounds toward negative infinity.
# True division always returns a float
print(7 / 2) # 3.5
# Floor division returns an integer (or float if any operand is float)
print(7 // 2) # 3When both operands are integers, // returns an integer. If either operand is a float, the result is a float that represents the floor value. For example, 7.0 // 2 returns 3.0, not 3. This distinction matters when you rely on the result type for subsequent operations.
Using //= with Integers and Floats
The //= operator works with both integers and floats, and the result type follows the same rules as the // operator. The variable on the left is rebound to the computed value.
count = 10
count //= 3
print(count) # 3
value = 10.0
value //= 3
print(value) # 3.0In the first example, count becomes an integer because both operands are integers. In the second, value remains a float because the original variable was a float. This behavior is consistent with Python's numeric type promotion rules.
Behavior with Negative Numbers
Floor division with negative numbers can produce results that surprise developers who expect truncation. The //= operator inherits this behavior, so you need to be aware of it when working with negative values.
a = -7
a //= 2
print(a) # -4, not -3
b = 7
b //= -2
print(b) # -4Because floor division rounds toward negative infinity, -7 // 2 yields -4 (since -4.0 is the floor of -3.5). Truncation would give -3, but Python's // is explicitly floor division. If you need truncation toward zero, use int(x / y) or math.trunc instead of //=.
//= vs. /= and Other Augmented Assignments
Augmented assignment operators are shorthand for x = x op y. The //= operator is distinct from /= because /= performs true division and always assigns a float result. Choosing the right operator depends on whether you need an integer quotient or a floating-point quotient.
| Operator | Behavior | Example (x = 7) | Result Type |
|---|---|---|---|
//= | Floor division | x //= 2 | Integer (if both int) |
/= | True division | x /= 2 | Float |
%= | Modulo | x %= 2 | Integer (if both int) |
In practice, //= is used when the quotient must be an integer, such as when splitting a list into chunks or implementing a binary search. The /= operator is used when you need a precise floating-point result, such as averaging values.
Common Mistakes and Edge Cases
A frequent mistake is assuming //= works with non-numeric types. Like all arithmetic operators, it requires operands that support floor division. Attempting to use //= on strings or lists raises a TypeError.
# This raises TypeError: unsupported operand type(s) for //=: 'str' and 'int'
text = "hello"
text //= 2Another edge case is division by zero. x //= 0 raises ZeroDivisionError, just like any division operation. There is no special handling in the augmented assignment form.
Floating-point floor division can also produce unexpected results due to precision. For example, 0.3 // 0.1 returns 2.0 because 0.3 / 0.1 is slightly less than 3 due to binary representation. This is not a bug in //= but a property of floating-point arithmetic. When exact integer division is required, use integers or the decimal module.
When to Use //= in Real Code
The //= operator shines in algorithms that repeatedly halve a value. For example, when implementing binary search on an integer range, you often compute the midpoint and update the bounds. Floor division (//) makes the intent clear and reduces repetition.
left, right = 0, len(items) - 1
while left <= right:
mid = (left + right) // 2
if items[mid] == target:
return mid
elif items[mid] < target:
left = mid + 1
else:
right = mid - 1Here, mid is computed with // but not assigned back to left or right. In other scenarios, such as repeatedly dividing a number by a factor until it falls below a threshold, //= is more direct.
remaining = 1000
while remaining > 0:
process(remaining)
remaining //= 2This loop processes 1000, 500, 250, and so on until the value becomes 0. The //= operator keeps the updated value in the same variable, making the loop concise and readable.
Behavior and Maintainability Considerations
For a simple variable target, x //= y is equivalent to x = x // y, so the choice between the two forms is mostly stylistic. The augmented form can improve maintainability by signaling that the variable is being updated with the result of the operation, which is useful in loops and stateful functions.
One subtlety is that //= does not necessarily operate in-place on mutable objects. For immutable types like integers and floats, the variable is rebound to a new object. For custom classes that implement __ifloordiv__, the behavior can be overridden.
When working with large loops, using //= can reduce the chance of accidentally reassigning to a different variable. It also makes the code more compact without sacrificing readability, as long as the operation is familiar to the reader. For teams new to Python, the explicit form x = x // y may be clearer, but //= is a standard idiom that experienced developers expect to see.
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.