> ## 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 For Loop

A Python `for` loop is a control flow statement that iterates over the items of any iterable object in the order they are returned by the iterable. Unlike traditional C-style `for` loops that rely on initialization, condition evaluation, and incrementing a counter, Python's implementation is fundamentally an iterator-based traversal mechanism.

## Syntax

The standard syntax utilizes the `for` and `in` keywords, followed by an indented block of statements:

```python theme={"dark"}
for target_list in iterable:
    # statement(s) executed for each item
```

Python also supports an optional `else` clause:

```python theme={"dark"}
for target_list in iterable:
    # statement(s) executed for each item
else:
    # statement(s) executed if the loop exhausts the iterable naturally
```

## Internal Mechanics

When a `for` loop is executed, Python performs the following operations under the hood:

1. **Evaluation:** The `iterable` expression is evaluated exactly once.
2. **Iterator Instantiation:** Python calls the built-in `iter()` function on the iterable. This function first attempts to invoke the object's `__iter__()` method to return an iterator object. If `__iter__()` is not defined, `iter()` falls back to the sequence protocol, attempting to call the object's `__getitem__()` method with integer arguments starting from `0`.
3. **Iteration:** Python repeatedly calls the built-in `next()` function on the iterator, which invokes the iterator's `__next__()` method.
4. **Assignment:** The value returned by `next()` is bound to the variable(s) defined in the `target_list`.
5. **Execution:** The indented block of code is executed.
6. **Termination:** Steps 3–5 repeat until the iterator raises a `StopIteration` exception (or an `IndexError` in the case of the `__getitem__()` fallback). The `for` loop catches this exception internally and terminates gracefully.

## Variable Scope

Variables assigned in the `target_list` are not block-scoped. They exist within the enclosing scope where the `for` loop is defined. After the loop terminates, these variables "leak" into the surrounding scope and remain bound to the last value yielded by the iterable. If the iterable evaluates to empty, the loop body is bypassed entirely, and the `target_list` variables are never created or bound.

## Target Unpacking

If the iterable yields sequences (like tuples or lists), the `target_list` can be structured to unpack those sequences directly into multiple variables during assignment.

```python theme={"dark"}
for var1, var2 in iterable_of_sequences:
    # var1 and var2 are bound to the unpacked elements of the current sequence
```

## Control Flow Modifiers

The execution flow within the loop body can be altered using specific keywords:

* **`break`**: Immediately terminates the loop. If an `else` clause is present, it is bypassed.
* **`continue`**: Immediately halts the current iteration, skips any remaining statements in the loop body, and proceeds to the next item yielded by the iterator.
* **`else`**: The block associated with this clause executes strictly when the loop terminates via the exhaustion of the iterable (i.e., when `StopIteration` or `IndexError` is raised). It will *not* execute if the loop is terminated prematurely by a `break` statement or an unhandled exception.

## Modifying Iterables During Iteration

Modifying a mutable iterable (such as a `list`) while actively iterating over it is a critical anti-pattern. Because the internal iterator maintains its state based on an index or internal pointer, adding or removing items during iteration shifts the underlying elements. This behavior predictably leads to skipped items, duplicate processing, or infinite loops.

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