> ## 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 Multiplication Assignment

The `*=` operator is an augmented assignment operator that performs in-place multiplication or sequence repetition. It evaluates the product of the left and right operands and binds the resulting value back to the left operand's identifier.

## Syntax

```python theme={"dark"}
x *= y
```

While logically equivalent to `x = x * y`, the `*=` operator evaluates the left operand only once and handles memory allocation differently depending on the mutability of the left operand.

## Underlying Mechanics

When the Python interpreter encounters `x *= y`, it determines the execution path based on the data type of `x`:

1. **`__imul__(self, other)`**: Python first checks if the left operand implements the "in-place multiply" magic method. If it does (which is typical for mutable objects), the object modifies its own data in memory and returns `self`. The variable `x` remains bound to the exact same memory address.
2. **`__mul__(self, other)`**: If `__imul__` is not implemented (which is strictly true for immutable objects), Python falls back to the standard multiplication method. It evaluates `x * y`, allocates a completely new object in memory for the result, and rebinds the identifier `x` to this new object.

## Behavior by Type

### Immutable Types (Integers, Floats, Strings, Tuples)

Because these types cannot be changed after creation, `*=` acts as syntactic sugar for `x = x * y`. A new object is created, and the reference is updated.

```python theme={"dark"}

# Numeric arithmetic
x = 10
x_id_before = id(x)
x *= 2  # x becomes 20
x_id_after = id(x)

# x_id_before != x_id_after (Reference rebound to new integer object)


# String repetition
s = "A"
s *= 3  # s becomes "AAA"

# Reference rebound to new string object
```

### Mutable Types (Lists, Bytearrays)

For mutable sequences, `*=` performs in-place repetition. The existing object is extended with copies of its own contents, preserving the original memory address and affecting any other variables referencing that same object.

```python theme={"dark"}

# List repetition
lst = [1, 2]
lst_id_before = id(lst)

lst *= 3  # lst becomes [1, 2, 1, 2, 1, 2]
lst_id_after = id(lst)


# lst_id_before == lst_id_after (Modified in-place via __imul__)
```

## Type Restrictions

The right operand must be an integer when the left operand is a sequence type (string, list, tuple). Attempting to use `*=` with a sequence and a float will raise a `TypeError`. For numeric types, standard implicit type coercion applies (e.g., an `int` multiplied by a `float` using `*=` will rebind the variable to a new `float` object).

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