Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The //= operator is the augmented assignment operator for floor division. It divides the left operand by the right operand, applies the floor function to the quotient (rounding down towards negative infinity), and assigns the resulting value back to the left operand.
x //= y
While conceptually similar to the expanded assignment x = x // y, augmented assignment evaluates the left-hand side expression exactly once. This is a critical distinction when the left operand involves a function call, property access, or complex indexing:

# get_index() is executed only once
items[get_index()] //= 2


# get_index() is executed twice
items[get_index()] = items[get_index()] // 2

Technical Mechanics

Type Coercion and Return Types The data type of the resulting assignment depends on the types of the operands involved:
  • If both operands are integers (int), the result assigned to the left operand is an int.
  • If either operand is a floating-point number (float), the result assigned to the left operand is a float representing a whole number.
Rounding Direction Unlike truncation (which rounds towards zero), Python’s floor division strictly rounds towards negative infinity. This distinction is critical when evaluating negative numbers. For example, -10 / 3 evaluates to -3.333.... Applying the floor function rounds this down to -4, not -3. Zero Division If the right operand evaluates to zero (e.g., x //= 0 or x //= 0.0), Python raises a ZeroDivisionError. This semantic rule applies universally to all division-based operators in Python. Object Model Implementation When the Python interpreter encounters x //= y, it resolves the operation through the following sequence:
  1. It attempts to invoke the in-place magic method __ifloordiv__(self, other) on the left operand. If implemented (typically by mutable custom types), the operation modifies the object in-place and returns it.
  2. If __ifloordiv__ is not implemented, Python falls back to the standard floor division method __floordiv__(self, other) and binds the resulting new object back to the target variable name.
Note: Built-in immutable numeric types like int and float do not implement __ifloordiv__ at all. They rely entirely on the __floordiv__ fallback, meaning a new numeric object is always allocated and reassigned to the variable.

Evaluation Examples


# Integer evaluation
a = 10
a //= 3
print(a)  # Output: 3
print(type(a))  # Output: <class 'int'>


# Floating-point evaluation
b = 10.0
b //= 3
print(b)  # Output: 3.0
print(type(b))  # Output: <class 'float'>


# Negative operand evaluation (rounding towards negative infinity)
c = -10
c //= 3
print(c)  # Output: -4


# Zero division exception
d = 5
d //= 0  # Raises ZeroDivisionError: integer division or modulo by zero
Master Python with Deep Grasping Methodology!Learn More