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

The `=` operator in Python is the fundamental assignment operator used to bind names to objects within a namespace or to mutate existing objects by updating their attributes, items, or slices. Unlike languages with value semantics (such as C++) where assignment copies a value into a pre-allocated memory space, Python utilizes pure reference semantics. The `=` operator acts as a reference binder or a method trigger, depending entirely on the nature of the left-hand side target.

## Statement vs. Expression

Crucially, the `=` operator in Python strictly forms an assignment *statement* and does not evaluate to a value (it is not an expression). This is a deliberate syntactic design choice meant to prevent accidental assignments inside conditionals (e.g., writing `if a = b:` instead of `if a == b:`). Because the `=` operator does not return the assigned value, it cannot be embedded inline within other operations. This strict separation between statements and expressions is the primary reason the assignment expression or "walrus" operator (`:=`) was introduced in Python 3.8.

## Syntax and Target Types

```python theme={"dark"}
target = expression
```

The execution of the `=` operator depends on the specific type of the `target`:

* **Identifier (Name):** Binds or rebinds a symbolic name in the local or global namespace to the memory address of the evaluated object.
* **Attribute Reference (`obj.attr`):** Mutates the object by invoking its `__setattr__` method.
* **Subscription (`obj[key]`):** Mutates the object by invoking its `__setitem__` method.
* **Slicing (`obj[start:stop]`):** Mutates the object by invoking its `__setitem__` method with a `slice` object.

## Execution Flow

When the Python interpreter encounters the `=` operator, it processes the statement in two distinct phases:

1. **Right-Hand Side (RHS) Evaluation:** The `expression` is evaluated completely to yield a single object in memory.
2. **Left-Hand Side (LHS) Assignment:** The resulting object reference is assigned to the `target`.

## Reference Binding Mechanics (Names)

When the target is an identifier, `=` creates a reference rather than copying data. Assigning an existing variable to a new variable results in two names pointing to the exact same object in memory.

```python theme={"dark"}
a = [1, 2, 3]
b = a          # 'b' is bound to the same list object as 'a'
```

Reassigning a name simply points it to a new object, leaving other references intact. It does not overwrite the original object in memory.

```python theme={"dark"}
a = [9, 8, 7]  # 'a' is bound to a new list; 'b' remains [1, 2, 3]
```

## Object Mutation (Complex Targets)

When the target is an attribute reference, subscription, or slice, the `=` operator mutates the underlying object rather than updating the namespace reference.

```python theme={"dark"}
lst = [1, 2, 3]
lst[0] = 99      # Mutates 'lst' via lst.__setitem__(0, 99)

class Node: pass
n = Node()
n.value = 10     # Mutates 'n' via n.__setattr__('value', 10)
```

In these cases, the name `lst` or `n` remains bound to the exact same object in memory; only the internal state of that object is modified.

## Chained Assignment

The `=` operator supports chaining, allowing multiple targets to be assigned the same object reference in a single statement.

```python theme={"dark"}
x = y = z = 100
```

The interpreter evaluates the RHS (`100`) to create an integer object, then assigns that single object reference to the chained targets strictly from **left to right**. In the statement above, Python assigns the reference to `x`, then `y`, and finally `z`.

## Iterable Unpacking

The `=` operator natively supports unpacking when the LHS consists of a comma-separated sequence of targets (implicitly a tuple or list) and the RHS is an iterable.

```python theme={"dark"}

# Basic unpacking
first, second, third = [10, 20, 30]


# Extended unpacking using the asterisk (*)
head, *tail = (1, 2, 3, 4)
```

For unpacking to succeed, the number of targets on the LHS must exactly match the number of items yielded by the RHS iterable, unless the `*` (gather) syntax is used to capture remaining items into a list.

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