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

# Python Looping

***

# 2. Loops

Loops repeat code.

Python has two loops:

* `for`
* `while`

***

# for Loop

Used for iterating over sequences.

## Syntax

```python theme={null}
for variable in sequence:
    code
```

***

## Loop through List

```python theme={null}
colors = ["Red", "Blue", "Green"]

for color in colors:
    print(color)
```

Output

```text theme={null}
Red
Blue
Green
```

***

## Loop through String

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

***

## Using `range()`

### `range(stop)`

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

Output

```text theme={null}
0
1
2
3
4
```

***

### `range(start, stop)`

```python theme={null}
for i in range(2, 6):
    print(i)
```

Output

```text theme={null}
2
3
4
5
```

***

### `range(start, stop, step)`

```python theme={null}
for i in range(1, 10, 2):
    print(i)
```

Output

```text theme={null}
1
3
5
7
9
```

***

## Nested Loops

```python theme={null}
for i in range(3):
    for j in range(2):
        print(i, j)
```

***

## Loop through Dictionary

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

for key in student:
    print(key)

for value in student.values():
    print(value)

for key, value in student.items():
    print(key, value)
```

***

## Loop Control Statements

### `break`

Stops the loop immediately.

```python theme={null}
for i in range(5):
    if i == 3:
        break
    print(i)
```

Output

```text theme={null}
0
1
2
```

***

### `continue`

Skips current iteration.

```python theme={null}
for i in range(5):
    if i == 2:
        continue
    print(i)
```

Output

```text theme={null}
0
1
3
4
```

***

### `pass`

Placeholder.

```python theme={null}
for i in range(5):
    pass
```

***

## `else` with Loop

Runs only if loop finishes normally.

```python theme={null}
for i in range(3):
    print(i)
else:
    print("Done")
```

Output

```text theme={null}
0
1
2
Done
```

***

# while Loop

Repeats while condition is True.

## Syntax

```python theme={null}
while condition:
    code
```

***

## Example

```python theme={null}
count = 1

while count <= 5:
    print(count)
    count += 1
```

Output

```text theme={null}
1
2
3
4
5
```

***

## Infinite Loop

```python theme={null}
while True:
    print("Running")
```

Stop using **Ctrl + C**.

***

## break in while

```python theme={null}
i = 1

while True:
    if i == 5:
        break
    print(i)
    i += 1
```

***

## continue in while

```python theme={null}
i = 0

while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)
```

***

# Quick Revision

### Loops

* `for` → Iterate over sequences.
* `while` → Repeat while condition is True.
* `break` → Exit loop.
* `continue` → Skip current iteration.
* `pass` → Placeholder.
* Loop `else` → Executes if loop completes normally.
