LOOPS · 10 MIN
For and While Loops
Loops repeat a block — for over a sequence, while until a condition turns False.
PYRA explains
- for i in range(3) runs with i = 0, 1, 2.
- while keeps running as long as its condition is True — make sure it eventually becomes False.
- break exits a loop early; continue skips to the next iteration.
Real world: Checking each ticket at a gate (for), or stirring until the tea boils (while).
Watch it move
i = 1, total = 1
1/5
Code
total = 0
for i in range(1, 5):
total += i
print(total)Output ›
10Try it in the online compiler →Checkpoint quiz
How many times does range(2, 6) iterate?