HomeGuidesPythonPython Loops Explained — for, while, enumerate, zip & break/continue
🐍 Python

Python Loops: for, while, enumerate, zip, and break/continue

Loops are fundamental and heavily tested. Here's what Python exams actually ask — from range() to enumerate() to the loop else clause.

Examifyr·2026·5 min read

for loops and range()

Python's for loop iterates over any iterable. range() generates a sequence of numbers.

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

for i in range(2, 8):      # 2, 3, 4, 5, 6, 7
    print(i)

for i in range(0, 10, 2):  # 0, 2, 4, 6, 8
    print(i)

for i in range(5, 0, -1):  # 5, 4, 3, 2, 1
    print(i)
Note: range(stop) — range(start, stop) — range(start, stop, step). The stop value is always excluded.

enumerate() — getting index and value together

enumerate() returns an index alongside each element. This is the idiomatic way to loop when you need both.

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

for i, fruit in enumerate(fruits):
    print(i, fruit)
# 0 apple
# 1 banana
# 2 cherry

for i, fruit in enumerate(fruits, start=1):
    print(i, fruit)
# 1 apple ...

zip() — looping over multiple iterables

zip() pairs up elements from multiple iterables. It stops at the shortest one.

names = ["Alice", "Bob", "Carol"]
scores = [90, 78, 85]

for name, score in zip(names, scores):
    print(f"{name}: {score}")
# Alice: 90
# Bob: 78
# Carol: 85
Note: If iterables have different lengths, zip() silently stops at the shortest. Use itertools.zip_longest() to fill missing values.

break, continue, and the loop else clause

break exits the loop entirely. continue skips to the next iteration. The else clause on a loop runs only if the loop completed without hitting a break.

# break
for n in range(10):
    if n == 5:
        break
    print(n)    # prints 0-4

# continue
for n in range(5):
    if n == 2:
        continue
    print(n)    # prints 0, 1, 3, 4

# loop else — runs only if no break occurred
for n in range(5):
    if n == 10:
        break
else:
    print("no break")  # this prints
Note: The loop else clause is one of Python's most surprising features. It only runs if the loop finished without a break — not on exceptions.

while loops

while loops repeat as long as a condition is True. Always ensure the condition can become False, or you'll get an infinite loop.

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

# Common pattern: while True with break
while True:
    user_input = input("Enter 'quit' to exit: ")
    if user_input == "quit":
        break

Exam tip

The loop else clause is a guaranteed exam question because it surprises most people. Remember: else on a loop means "no break happened" — not "loop condition was False".

🎯

Think you're ready? Prove it.

Take the free Python readiness test. Get a score, topic breakdown, and your exact weak areas.

Take the free Python test →

Free · No sign-up · Instant results

← Previous
Python Dictionaries Explained — Methods, Iteration & Common Traps
Next →
Python Classes and OOP Explained — __init__, Inheritance & Methods
← All Python guides