# Program Structure and Control Flow

## Loops and Iterations

### Throw-away Variable

The `_` symbol is useful when the variable in that position is not important:

```python
s = [(1, 2, 3), (4, 5, 6)]
for x, _, z in s:
    print(x, z)
```

### Wildcard Unpacking

Use `*var_name` to unpack multiple elements:

```python
s = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
for x, y, *extra in s:
    print(f"{x = }", f"{y = }", f"{extra = }")
```

Output:

![Wildcard Unpacking](/files/PtiiwExL2fefRsmeeacm)

### Enumerate

Let `s` be an iterable, then `enumerate(s)` creates an iterator that produces tuples `(0, s[0])`, `(1, s[1])`, `(2, s[2])`, and so on:

```python
s = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
for i, x in enumerate(s):
    print(i, x)
```

Output:

![Enumerate](/files/GnlgEvEKnKXN5yb7hFN7)

### `for-else` Loop

The `else` clause of a loop executes only if the loop runs to completion. This either occurs immediately (if the loop wouldn't execute at all) or after the last iteration. If the loop is terminated early using the `break` statement, the `else` clause is skipped.

```python
with open('foo.txt') as file:
    for line in file:
        stripped = line.strip()
        if not stripped:
            break
        # Do some processing work
    else:
        print('Processing completed.')
```

## Iterators

{% hint style="info" %}
An **iterable** is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for loop.
{% endhint %}

**Lists, tuples, dictionaries, sets, strings, and file objects are all iterable objects.** They are iterable containers which you can get an iterator from. All these objects have a `iter()` method which is used to get an iterator:

```python
# Create an example tuple
>>> example_tuple = ('apple', 'banana', 'cherry')

# Create an iterator using the iter() function
>>> iterator = iter(example_tuple)

# Get next item in the iterator
>>> next(iterator)
'apple'

# Get next item in the iterator
>>> next(iterator)
'banana'

# Get next item in the iterator
>>> next(iterator)
'cherry'

# No more iterm in the iterator
>>> next(iterator)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/computer-science/python/program-structure-and-control-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
