> ## Documentation Index
> Fetch the complete documentation index at: https://python4ai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Loops

> Repeat code multiple times

## What are loops?

Loops let you repeat code without writing it multiple times. Instead of copying and pasting, you tell Python to repeat the code for you.

Without loops:

```python theme={null}
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
```

With loops:

```python theme={null}
for i in range(5):
    print("Hello!")
```

Both do the same thing, but the loop is much cleaner!

## For loops

The `for` loop is the most common loop in Python. Let's start simple:

### Repeat a specific number of times

```python theme={null}
# Print numbers 0 through 4
for i in range(5):
    print(i)

# Output:
# 0
# 1
# 2
# 3
# 4
```

<Note>
  Python starts counting at 0, not 1. This is called "zero-indexing". So `range(5)` gives you 0, 1, 2, 3, 4 (five numbers total).
</Note>

### Count from different starting points

```python theme={null}
# Count from 1 to 5
for i in range(1, 6):
    print(i)
# Output: 1, 2, 3, 4, 5

# Count by 2s
for i in range(0, 10, 2):
    print(i)
# Output: 0, 2, 4, 6, 8
```

### Loop through text

You can loop through each character in a string:

```python theme={null}
name = "Python"
for letter in name:
    print(letter)

# Output:
# P
# y
# t
# h
# o
# n
```

### Loop through a list (preview)

We'll learn more about lists later, but here's a preview:

```python theme={null}
colors = ["red", "blue", "green"]
for color in colors:
    print(f"I like {color}")

# Output:
# I like red
# I like blue
# I like green
```

## While loops

A `while` loop continues as long as a condition is true:

```python theme={null}
count = 0
while count < 5:
    print(f"Count is {count}")
    count = count + 1  # Increase count by 1

# Output:
# Count is 0
# Count is 1
# Count is 2
# Count is 3
# Count is 4
```

<Warning>
  Always make sure your while loop will eventually stop! If you forget to update the variable, it will run forever.
</Warning>

## Common mistakes

<AccordionGroup>
  <Accordion title="Forgetting the colon">
    ```python theme={null}
    # Wrong - missing colon
    for i in range(5)
        print(i)

    # Right - colon after the loop line
    for i in range(5):
        print(i)
    ```
  </Accordion>

  <Accordion title="Wrong indentation">
    ```python theme={null}
    # Wrong - not indented
    for i in range(3):
    print(i)

    # Right - indented inside the loop
    for i in range(3):
        print(i)
    ```
  </Accordion>

  <Accordion title="Off-by-one errors">
    ```python theme={null}
    # Wrong - only goes to 4
    for i in range(5):
        print(f"Item {i}")  # 0, 1, 2, 3, 4

    # Right - if you want 1-5
    for i in range(1, 6):
        print(f"Item {i}")  # 1, 2, 3, 4, 5
    ```
  </Accordion>
</AccordionGroup>

<Card title="Data structures" icon="arrow-right" href="/data-structures">
  Learn about lists, dictionaries, and more
</Card>
