Python For Loop Tracing: 5 Exercises with Answers
For loops combine range(), enumerate(), and nested iteration. These five exercises increase in difficulty. Cover your answers, trace on paper, then compare.
Exercise 1
total = 0
for n in range(1, 4):
total += n
print(total)Answer: 6. range(1,4) yields 1, 2, 3.
Exercise 2–5
Exercise 2 uses enumerate on a string. Exercise 3 nests loops printing a multiplication pattern. Exercise 4 mutates a list while iterating. Exercise 5 uses break and else on a for loop - a Python feature many miss. Visit our for-loop practice landing page for more static examples, then generate 100+ unique problems instantly.