Loops
Python · Reference cheat sheet
Loops
Python · Reference cheat sheet
📋 Overview
Python iterates with for over any iterable and while for condition-driven loops. Prefer for + comprehensions over index arithmetic. Control flow: break, continue, else on loops (runs if no break), and enumerate / zip for paired iteration.
🔧 Core concepts
| Construct | Use |
|---|---|
for x in iterable | Iterate values |
for i, x in enumerate(xs) | Index + value |
for a, b in zip(xs, ys) | Parallel iterables |
while cond | Condition loop |
break / continue | Exit / next iteration |
for/while else | Runs if loop did not break |
| Comprehensions | [...], \{...\}, (... for ...) generators |
range(stop), range(start, stop[, step]) for integer sequences.
💡 Examples
Enumerate, zip, unpacking:
names = ["Ada", "Bob"]
scores = [10, 7]
for i, name in enumerate(names, start=1):
print(i, name)
for name, score in zip(names, scores, strict=True): # 3.10+
print(f"{name}: {score}")Loop else (search):
needle = 4
for n in [1, 2, 3]:
if n == needle:
print("found")
break
else:
print("not found")Comprehensions and generators:
squares = [n * n for n in range(5) if n % 2 == 0]
gen = (n * n for n in range(1_000_000)) # lazy
total = sum(gen)While with sentinel:
from collections.abc import Iterator
def read_chunks() -> Iterator[str]:
yield "a"
yield "b"
it = iter(read_chunks())
while True:
try:
chunk = next(it)
except StopIteration:
break
print(chunk)⚠️ Pitfalls
- Do not mutate a list you are iterating; iterate a copy or build a new list.
for i in range(len(xs))is usually worse thanfor x in xs/enumerate.- Infinite
while Trueneeds a clear exit; prefer iterators when possible. ziptruncates to the shortest iterable unlessstrict=True.- Creating huge lists with comprehensions can exhaust memory—use generators.