> ## 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 Return Statement

The `return` statement is a control flow mechanism used within a function to immediately terminate its execution, pop its local frame from the call stack, and pass an object reference back to the caller's scope.

```python theme={"dark"}
return [expression_list]
```

## Execution Mechanics

When the Python interpreter encounters a `return` statement, it performs the following sequence of operations:

1. **Expression Evaluation:** It evaluates the `expression_list`. If no expression is provided, it evaluates to the `None` singleton.
2. **Stack Unwinding:** It destroys the local namespace associated with the function's execution frame.
3. **Control Transfer:** It passes the evaluated object reference back to the exact point in the call stack where the function was invoked.

## Syntactic Behaviors

**Implicit vs. Explicit Returns**
If a function reaches the end of its code block without encountering a `return` statement, Python implicitly executes `return None`. A bare `return` statement without an expression also yields `None`.

```python theme={"dark"}
def implicit_none():
    x = 10
    # Implicitly executes: return None

def explicit_none():
    return  # Evaluates to None
```

**Tuple Packing**
If the `expression_list` contains multiple comma-separated expressions, Python automatically packs them into a single `tuple` object. The `return` statement always returns exactly one object; multiple values are simply a single tuple containing multiple references.

```python theme={"dark"}
def return_multiple():
    return 1, "string", [3, 4]  # Returns a single tuple: (1, 'string', [3, 4])
```

## Interaction with Exception Handling

The `return` statement exhibits specific behavior when used in conjunction with `try...finally` blocks. If a `return` is triggered inside a `try` block, the interpreter guarantees that the `finally` block will execute *before* the function actually yields control back to the caller.

If the `finally` block also contains a `return` statement, the `return` value from the `finally` block overrides the `return` value from the `try` block.

```python theme={"dark"}
def exception_override():
    try:
        return "Try Return"
    finally:
        return "Finally Return" # This value overrides "Try Return"
```

## Interaction with Generators

In a generator function (a function containing `yield` statements), the `return` statement serves a different internal purpose. Executing `return expression` inside a generator does not yield a value to the iteration sequence; instead, it raises a `StopIteration` exception to terminate the generator. The evaluated `expression` is attached to the `StopIteration` exception as its `value` attribute.

```python theme={"dark"}
def generator_return():
    yield 1
    return "Termination Value" # Raises StopIteration("Termination Value")
```

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