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.
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)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: 85break, 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 printswhile 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":
breakExam 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