🐍 Python 2026 › Lesson 10: Loops – for and while
Lesson 10 of 30

Loops – for and while

Repeating actions with for loops, while loops, break, continue, and else.

The for Loop

The for loop iterates over any iterable (list, string, range, dict…):

for i in range(1, 6):
    print(i)
# prints 1 2 3 4 5

animals = ["cat", "dog", "fish"]
for animal in animals:
    print(animal.capitalize())

range() Function

range(5)          # 0,1,2,3,4
range(2, 8)       # 2,3,4,5,6,7
range(0, 10, 2)  # 0,2,4,6,8
range(10, 0, -1) # 10,9,...,1

The while Loop

count = 1
while count <= 5:
    print(f"Count: {count}")
    count += 1

break and continue

# break exits the loop early
for n in range(10):
    if n == 5:
        break
    print(n)  # 0 1 2 3 4

# continue skips the rest of this iteration
for n in range(6):
    if n % 2 == 0:
        continue
    print(n)  # 1 3 5

Nested Loops

for row in range(1, 4):
    for col in range(1, 4):
        print(f"{row}x{col}={row*col}", end="  ")
    print()