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

The `%` operator in Python serves two distinct syntactic roles depending on the operand types: it acts as the modulo operator for numeric types and as the string interpolation operator for string and byte types.

## Numeric Modulo

When applied to numeric types (`int`, `float`), the `%` operator calculates the remainder of the floor division between a dividend (left operand) and a divisor (right operand).

```python theme={"dark"}
result = a % b
```

**Mathematical Behavior:**
Unlike C or Java, Python's modulo operator guarantees that the sign of the result always matches the sign of the *divisor* (`b`), not the dividend (`a`). The operation is mathematically defined by the following equation:

`r = a - (b * floor(a / b))`

```python theme={"dark"}

# Positive divisor: result is always positive or zero
10 % 3    # Evaluates to 1
-10 % 3   # Evaluates to 2


# Negative divisor: result is always negative or zero
10 % -3   # Evaluates to -2
-10 % -3  # Evaluates to -1
```

**Floating-Point Operands:**
The `%` operator accepts `float` operands, returning a floating-point remainder based on the same floor division logic.

```python theme={"dark"}
5.5 % 2.0  # Evaluates to 1.5
```

*Note: Due to IEEE 754 precision limitations and the floor division behavior, the standard library `math.fmod()` is strictly recommended over the `%` operator when working with floats, as `math.fmod()` implements C-style modulo (matching the sign of the dividend).*

## String and Bytes Formatting

When the left operand is a `str` or `bytes` object, the `%` operator is overloaded to perform legacy printf-style formatting.

```python theme={"dark"}
formatted_string = format_string % values
```

**Evaluation Mechanics:**
The left operand is parsed for conversion specifiers (e.g., `%s` for string, `%d` for decimal integer, `%f` for float). The right operand must be a single value, a tuple of values, or a mapping object (such as a `dict`) that provides the data to substitute into the specifiers.

```python theme={"dark"}

# Tuple substitution (positional)
"%s is %d years old." % ("Alice", 30)

# Evaluates to: 'Alice is 30 years old.'


# Dictionary substitution (named mapping)
"%(name)s is %(age)d years old." % {"name": "Bob", "age": 25}

# Evaluates to: 'Bob is 25 years old.'
```

When using a tuple as the right operand, the number of elements in the tuple must exactly match the number of positional conversion specifiers in the format string, otherwise a `TypeError` is raised.

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