# For Loops

Use `for` loops to iterate over sequences (lists, tuples, strings) and other iterables.

## Syntax

```python
for item in sequence:
    print(item)
```

## Basic Example

```python
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
```

## Using `range()`

`range()` generates numbers on demand, which is memory-efficient.

```python
for i in range(5):
    print(i)
```

`range(start, stop, step)` is also supported:

```python
print(list(range(2, 10, 2)))  # [2, 4, 6, 8]
```

## Index + Value with `enumerate()`

```python
fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print(index, fruit)
```

## `for` ... `else`

The `else` block runs only if the loop completes without a `break`.

```python
targets = ["Ada", "Grace", "Linus"]
name = "Guido"

for target in targets:
    if target == name:
        print("Found")
        break
else:
    print("Not found")
```

## Common Pitfalls

* Modifying a list while iterating over it.
* Using `range(len(...))` when `enumerate()` is clearer.

[Next](/cit-python-cohort-three/week2/control_flow/while_loops.md) | [Previous](/cit-python-cohort-three/week2/control_flow/if_statements.md)


---

# 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://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/control_flow/for_loop.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.
