📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Loops

Loops

5 min read Quiz at the end
for loops iterate over any iterable: for item in list. Use range() for number sequences and enumerate() for index and value. while loops repeat while a condition is true. break exits early, continue skips one iteration.

Loops in Python

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

# Loop with index
for i, val in enumerate(["a","b","c"]):
    print(i, val)

# while loop
n = 1
while n <= 5:
    print(n)
    n += 1

# break and continue work the same as in other languages
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which loop iterates over a sequence?
💡 Python uses for x in sequence: to iterate over sequences.
2. What does range(5) produce?
💡 range(5) produces 0, 1, 2, 3, 4 (5 integers starting at 0).
3. Which function gives both index and value?
💡 enumerate(seq) yields (index, value) tuples for each item.
4. What does break do?
💡 break exits the loop immediately.
5. What does continue do?
💡 continue skips the remaining code in the current iteration and goes to the next.