Loops
A loop runs the same lines of code again and again, so you write a job once and let Python repeat it. Step through a four-line program, loop.py, that adds up the list [1, 2, 3] with a for loop: watch the loop variable take each item in turn, the indented body run once per item, and the total grow from 0 to 6. Finish with range(), which counts for you, and a look at the while loop.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01How many times does the body run? for n in [5, 10, 15, 20]: print(n)
A for loop runs its body once for each item. The list has four items, so the body runs four times, with n set to 5, 10, 15 and 20 in turn.
02What does this print? total = 0 / for n in [2, 4]: / total = total + n / print(total)
total goes 0, then 0 + 2 = 2, then 2 + 4 = 6. print(total) is not indented, so it is outside the loop and runs once, after it: 6.
03Which numbers does range(1, 5) give?
range(start, stop) starts at start and stops just before stop, so range(1, 5) gives 1, 2, 3, 4.
04In the figure's program, print(total) is moved inside the loop (indented). What is printed?
An indented line belongs to the body, so it runs on every pass: after each addition total is 1, then 3, then 6.
05What is wrong with this loop? n = 1 / while n < 4: / print(n)
A while loop repeats as long as its condition is True. Nothing in the body changes n, so the condition never turns False. Adding n = n + 1 to the body fixes it; Ctrl-C stops a runaway program.
Glossary
- Loop
- Code that runs the same lines again and again, so a job is written once and repeated.
- for loop
- A loop that runs its body once for each item of a sequence, such as a list, in order.
- while loop
- A loop that runs its body again and again as long as a condition is True, checked before each pass.
- Loop variable
- The name after for (n in for n in ...). Before each pass it is given the next item.
- Body
- The indented lines under the for or while line: the part that repeats.
- Iteration
- One run through a loop's body, also called a pass.
- List
- Several values in order, inside square brackets, such as [1, 2, 3].
- range()
- A built-in that produces a run of whole numbers: range(1, 4) gives 1, 2, 3 (the stop number is left out).
Notes and sources
- Python's style guide, PEP 8, uses four spaces for each level of indentation. Every line of a loop's body must be indented by the same amount; the first line that is not indented is outside the loop.
- The colon at the end of the for line is required: leaving it out is a SyntaxError.
- After the loop the loop variable keeps the last value it was given, so n is still 3 when line 4 runs.
- range(3) starts at 0 and gives 0, 1, 2. A third number sets the step: range(0, 10, 2) gives 0, 2, 4, 6, 8. The stop number itself is never included.
- A while loop checks its condition before every pass. If nothing in the body ever makes the condition False, the loop never ends; press Ctrl-C to stop the program (Python raises KeyboardInterrupt).
- break leaves a loop at once, and continue skips straight to the next pass.
Quiz Unlocks after the last step.