Python In-Place Integer Division Operator

Rate this post

Python’s in-place integer division operator x //= y divides two objects in-place by calculating x // y and assigning the result to the first operands variable name x. Set up in-place integer (or floor) division for your own class by overriding the magic “dunder” method __floordiv__(self, other) in your class definition.

>>> x = 5
>>> x //= 2
>>> x
2

The expression x /= y is syntactical sugar for the longer-form x = x / y:

>>> x = 5
>>> x = x // 2
>>> x
2

Let’s explore some examples on different data types of the operands.

Integer Example

The //= operator on integer operands stores the result of the mathematical floor division of both operands in the left-hand operands’ variable.

>>> x = 9
>>> x //= 2
>>> x
4

Float Example

If at least one of the operands is a float value, the result is also a float—float is infectious!

>>> x = 9.0
>>> x //= 2
>>> x
4.0

Incompatible Data Type

What if two operands have an incompatible data type—unlike floats and integers? For example, if you try to divide a list by an integer variable?

>>> x = [1, 2, 3]
>>> x //= 3
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    x //= 3
TypeError: unsupported operand type(s) for //=: 'list' and 'int'

The result of incompatible division is a TypeError: unsupported operand type(s). You can fix it by using only compatible data types for the in-place integer division operation.

Can you use the division operator on custom objects? Yes!

Python In-Place Floor Division Magic Method

To use the in-place division operator //= on custom objects, you need to define the __floordiv__() method (“dunder method”, “magic method”) that takes two arguments self and other, updates the first argument self with the result of the integer division, and returns the updated object.

In the following code, you divide two Data objects using integer division on their contents contents:

class Data:

    def __init__(self, data):
        self.data = data

    def __floordiv__(self, other):
        self.data //= other.data
        return self

x = Data(9)
y = Data(2)

x //= y
print(x.data)
# 4

You can see that the content of the first operand is updated as a result of the in-place integer division operation.

Note that if you want to override in-place division rather than in-place integer division, you need to define the __truediv__(self, other) method in your class.

Here’s an analogous example:

class Data:

    def __init__(self, data):
        self.data = data

    def __truediv__(self, other):
        self.data /= other.data
        return self

x = Data(9)
y = Data(2)

x /= y
print(x.data)
# 4.5

Now Python internally performs the true division 9 / 2 == 4.5 and not the integer division 9 // 2 == 4.

Python In-Place Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of first calculating the result of x +3 and then assigning it to the variable x.

OperatorNameShort ExampleEquivalent Long Example
=In-place Assignmentx = 3
+=In-place Additionx += 3x = x + 3
-=In-place Subtractionx -= 3x = x - 3
*=In-place Multiplicationx *= 3x = x * 3
/=In-place Divisionx /= 3x = x / 3
%=In-place Modulox %= 3x = x % 3
//=In-place Integer Divisionx //= 3x = x // 3
**=In-place Powerx **= 3x = x ** 3
&=In-place Bitwise Andx &= 3x = x & 3
|=In-place Bitwise Orx |= 3x = x | 3
^=In-place Bitwise XORx ^= 3x = x ^ 3
>>=In-place Bitwise Shift Rightx >>= 3x = x >> 3
<<=In-place Bitwise Shift Leftx <<= 5x = x << 5