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

# Python Floor Division

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.

```python theme={"dark"}
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`.

```python theme={"dark"}

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

```python theme={"dark"}
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 ($\lfloor x \rfloor$). When evaluating negative quotients, the result is rounded down to the next lowest integer, moving further away from zero.

```python theme={"dark"}

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

```python theme={"dark"}

# 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`.

```python theme={"dark"}
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'
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
