Loops
Repetition lets a program apply the same steps to many items, or repeat until a condition changes — without rewriting those steps each time.
By the end of this lesson, you can
- Trace a for loop over a sequence and a while loop controlled by a condition
- Predict how many times a loop body runs, including common off-by-one mistakes
- Explain why a while loop must eventually make its condition False, and what happens when it doesn't
Why it matters
Without repetition, a program that processes five numbers needs five near-identical copies of the same code — and one that processes five thousand numbers is impossible to write by hand at all. A loop repeats a block of steps so the same logic can apply to as many items as needed, without rewriting it.
Mental model
Python has two kinds of loop, and the difference is what controls how many times they run:
- A
forloop repeats once per item in a sequence — you know in advance (or Python can figure out) how many repetitions there will be. - A
whileloop repeats for as long as a condition staysTrue— the number of repetitions depends on what happens during the loop, and isn’t known in advance.
total = 0
for number in [4, 8, 15]:
total = total + number
print(total)27range() is a common way to loop a fixed number of times rather than
over an existing list:
for i in range(3):
print(i)0
1
2range(3) produces 0, 1, 2 — three values, not including 3.
This is one of the most common sources of off-by-one mistakes for
beginners.
Trace it
Tracing a loop means writing down every variable’s value after each pass through the body:
| Pass | number | total |
|---|---|---|
| start | (none yet) | 0 |
| 1 | 4 | 4 |
| 2 | 8 | 12 |
| 3 | 15 | 27 |
A while loop’s condition is checked before every pass, including the
first — if it’s False immediately, the body never runs at all:
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1
print("Liftoff!")3
2
1
Liftoff!Check your understanding
Or reveal the answer without checking
Answer:5
range(5) produces 0, 1, 2, 3, 4 — five values, not including 5. The loop body runs once per value, so five times total.
Or reveal the answer without checking
Answer:The loop body never changes anything the condition depends on, so the condition never becomes False
A while loop stops exactly when its condition becomes False. If nothing in the body changes a variable the condition depends on, the condition's truth value never changes, and the loop never stops.
Practice: warm-up
Trace this loop by hand — write down total after each pass — before
revealing the answer.
total = 100
for amount in [10, 25, 5]:
total = total - amount
Stuck? Reveal one hint at a time.
Hint 1
There are three items in the list, so the body runs three times.
Hint 2
Each pass subtracts that pass's amount from whatever total currently is — use the running value, not the original 100, after the first pass.
Reveal the trace
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
start: total = 100
pass 1 (amount = 10): total = 90
pass 2 (amount = 25): total = 65
pass 3 (amount = 5): total = 60Practice: apply it
This loop is supposed to print 1 through 5, then stop — but it
never stops:
count = 1
while count <= 5:
print(count)
Or reveal the answer without checking
Answer:Nothing inside the loop body ever changes count, so count <= 5 stays True forever
count starts at 1 and count <= 5 is True, but nothing in the body updates count — so it's still 1 on every pass, and the condition never becomes False.
Modification challenge: add the one missing line that makes this
loop print 1 through 5 and then correctly stop.
Summary
- A loop repeats a block of code so the same steps don’t need to be rewritten for every item.
- A
forloop repeats once per item in a sequence; awhileloop repeats for as long as its condition staysTrue. range(n)produces0up to, but not including,n— a common source of off-by-one mistakes.- A
whileloop’s body must eventually make its conditionFalse, or it becomes an infinite loop. - Tracing a loop means writing down every relevant variable’s value after each pass through the body.