Control Flow

For Loops

Use

for
loops to repeat code a specific number of times.

Looping Through a Range

for i in range(5):
    print(i)
# Prints: 0, 1, 2, 3, 4

Range Variations

range(5)        # 0, 1, 2, 3, 4
range(1, 6)     # 1, 2, 3, 4, 5
range(0, 10, 2) # 0, 2, 4, 6, 8

Looping Through Lists

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

for fruit in fruits:
    print(f"I like {fruit}")

The Power of Loops

Calculate sum of 1 to 100:

total = 0
for num in range(1, 101):
    total += num
print(total)  # 5050
Visualizer
for loop iteration
1
2
3
4
5
✓ Loop completed

Interactive Visualization

python
Loading...
Output
Run execution to see output...