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 in Python performs floor division. It evaluates the quotient of two real numerical operands and mathematically floors the result, rounding down to the nearest whole number towards negative infinity.
result = dividend // divisor

Type Coercion and Return Values

The data type of the returned value depends on the data types of the operands evaluated. Python applies numeric promotion (type coercion) before performing the operation:
  • Integer Return: If both the dividend and the divisor are integers (int), the operator returns an int.
  • Float Return: If either the dividend or the divisor is a floating-point number (float), Python promotes the integer operand to a float prior to evaluation. The operation then performs float floor division, returning a standard IEEE 754 floating-point number (float). The resulting float represents a whole number mathematically, which is why its string representation displays with a .0.

# Both operands are integers
10 // 3      # Evaluates to 3 (type: int)


# Numeric promotion to float occurs before division
10.0 // 3    # Evaluates to 3.0 (type: float)
10 // 3.0    # Evaluates to 3.0 (type: float)
10.0 // 3.0  # Evaluates to 3.0 (type: float)

Floating-Point Precision Limitations

When working with float operands, the // operator is subject to IEEE 754 floating-point representation limits. Consequently, a // b is not always strictly equivalent to math.floor(a / b). Because floats cannot precisely represent all base-10 fractions, the intermediate exact quotient may be slightly lower than expected, causing the floor operation to drop to the next lower integer.
import math


# Standard true division yields 10.0
1.0 / 0.1             # Evaluates to 10.0
math.floor(1.0 / 0.1) # Evaluates to 10


# Floor division yields 9.0 due to precision limits
1.0 // 0.1            # Evaluates to 9.0

Behavior with Negative Operands

Unlike languages such as C or Java, which truncate division results towards zero, Python’s // operator strictly follows the mathematical floor function (x\lfloor x \rfloor). When evaluating negative quotients, the result is rounded down to the next lowest integer, moving further away from zero.

# Positive quotient: rounds down towards zero
10 // 3      # Evaluates to 3


# Negative quotient: rounds down towards negative infinity
-10 // 3     # Evaluates to -4
10 // -3     # Evaluates to -4
-10.0 // 3   # Evaluates to -4.0

Underlying Implementation

At the object level, the // operator relies on the __floordiv__(self, other) and __rfloordiv__(self, other) magic (dunder) methods. Python’s data model dictates a strict resolution order for these methods:
  1. Subclass Priority: If the right operand is an instance of a strict subclass of the left operand’s type, Python prioritizes the right operand and invokes its __rfloordiv__ method first.
  2. Standard Invocation: If the right operand is not a strict subclass, Python invokes the left operand’s __floordiv__ method.
  3. Fallback Mechanism: If the left operand’s __floordiv__ method returns NotImplemented, Python falls back to invoking the right operand’s __rfloordiv__ method.

# The operator syntax
a // b


# Standard underlying method call
a.__floordiv__(b)


# Prioritized or fallback method call (depending on subclass status or NotImplemented)
b.__rfloordiv__(a)
Because it relies on dunder methods, the // operator can be overloaded in custom classes to define specific floor division behavior for user-defined objects.

Exceptions and Unsupported Types

The // operator enforces strict mathematical and type-based rules, raising exceptions for invalid operations:
  • ZeroDivisionError: If the right operand (divisor) evaluates to 0 or 0.0, the operator raises a ZeroDivisionError, halting execution unless explicitly caught.
  • TypeError (Complex Numbers): Floor division is not mathematically defined for complex numbers in Python. Attempting to use the // operator with a complex type raises a TypeError.
5 // 0         # Raises ZeroDivisionError: integer division or modulo by zero
5.0 // 0.0     # Raises ZeroDivisionError: float floor division by zero

(5 + 2j) // 2  # Raises TypeError: unsupported operand type(s) for //: 'complex' and 'int'
Master Python with Deep Grasping Methodology!Learn More