Back to Blog
Python

Python //= Operator: In-Place Floor Division

python //= operator: Learn how the Python //= operator performs floor division and assigns the result in one step, with examples and edge cases.

Python operatorsfloor divisionaugmented assignmentinteger arithmeticPython syntax
Illustration of a Python code snippet showing the //= operator dividing a number and assigning the result back to the variable.

The python //= operator is an augmented assignment 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 in-place arithmetic 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) # 3

When 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.0

In 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) # -4

Because 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.

OperatorBehaviorExample (x = 7)Result Type
//=Floor divisionx //= 2Integer (if both int)
/=True divisionx /= 2Float
%=Modulox %= 2Integer (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 //= 2

Another 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. Using //= 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 - 1

Here, 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 //= 2

This loop processes 1000, 500, 250, and so on until the value becomes 0. The //= operator ensures the variable is updated in place, making the loop concise and readable.

Performance and Maintainability Considerations

From a performance perspective, x //= y is equivalent to x = x // y; Python does not perform any special optimization that makes one faster than the other. The choice between them is purely stylistic. However, //= 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, but for built-in numeric types there is no performance advantage over explicit assignment.

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.

python //= operator: Practical Usage and Code Examples | RYUSLOG DEV