Python theorytheory 0/50 · 0%
Control flow · easy

10. for loops and range

Iterating over sequences and iterables.

A `for` loop iterates over any iterable — lists, strings, ranges, dict keys, files. `range(n)` produces integers `0..n-1` lazily.

python
total = 0
for i in range(1, 6):
    total += i
# total == 15

for i, tx in enumerate(["mint", "burn"]):
    print(i, tx)

`enumerate` yields (index, value) pairs, and `zip` pairs up multiple iterables:

python
for name, price in zip(names, prices):
    print(name, price)

Check your understanding

  1. 1. What does `range(1, 6)` produce?

  2. 2. What does `enumerate(items)` yield?

  3. 3. What does `zip(a, b)` do?