Loops: doing things again
Programs are good at doing the same thing many times without getting bored. A loop runs a block of lines again and again. Step through a short Python program: a for loop adds up 1 + 2 + 3 + 4, the highlight jumps back to the loop's first line after every pass, and the numbers in memory change each time. Then see a while loop stop when its condition turns False, and how break gets you out of a loop that would never end.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Which numbers does for i in range(1, 4): give to i?
range(start, stop) counts from start and stops just before stop, so 4 itself is left out: 1, 2, 3.
02What does this print? total = 0 then for n in range(1, 4): total = total + n then print(total)
The loop adds 1, then 2, then 3 to total: 0 + 1 + 2 + 3 = 6. range(1, 4) never gives 4.
03A while loop checks its condition...
Python tests the condition each time it reaches the while line. True runs the body again; False ends the loop.
04Why does x = 5 then while x > 0: print(x) never stop?
It is an infinite loop. The body has to change something the condition checks, such as x = x - 1, or use break.
05What does break do inside a loop?
break leaves the loop it is in straight away, whatever the condition says. The lines after the loop then run.
Glossary
- Loop
- A part of a program that runs the same lines again and again.
- for loop
- A loop that takes the items of a sequence, such as a list or range(1, 5), one at a time.
- range(start, stop)
- Gives the whole numbers from start up to, but not including, stop: range(1, 4) gives 1, 2, 3.
- Loop variable
- The name after for (n in for n in ...). It takes the next item on every pass.
- Loop body
- The indented lines under the loop's header line. They run once per pass.
- Pass (iteration)
- One run through the loop body.
- while loop
- A loop that runs its body again as long as its condition is True.
- break
- A statement that ends the loop it is in at once.
Notes and sources
- range(start, stop) counts up from start and stops just before stop, so range(1, 4) gives 1, 2, 3 and range(1, 5) gives 1, 2, 3, 4. range(5) on its own starts at 0: 0, 1, 2, 3, 4.
- Python knows which lines are the loop body from the indentation: the lines under the header that are indented further. The header line ends with a colon.
- After a for loop ends, the loop variable keeps its last value: here n is still 4.
- Ctrl+C makes Python raise KeyboardInterrupt, which stops a program stuck in an infinite loop.
- Python also has a built-in sum(), so sum(range(1, 5)) gives 10 without writing the loop yourself.
- Python tutorial: for statements
- Python tutorial: The range() function
- Python tutorial: break and continue statements
- Python tutorial: First steps towards programming (while)
- Python reference: The for statement
- Python reference: The while statement
- Python reference: The break statement
- Python library: range
- Python library: KeyboardInterrupt
Quiz Unlocks after the last step.