A loop is one piece of code, run again and again, with a counter holding a different value each time.
You do not write the work out ten times. You write it once and tell Python how many times to run it. The counter is an ordinary variable: print it, add it up, or use it to pick an item out of a list.
Trace it before you type. Most loop mistakes are obvious in ten seconds on paper and invisible on screen for an hour. So trace this one first.
i is, and count
how many times the print line actually runs.Three passes, not four. range(1, 4)
stops before the 4.
Every form of range, and what it gives you
| You write | The counter takes | Passes |
|---|---|---|
| range(1, 4) | 1, 2, 3 | 3 |
| range(1, 11) | 1 … 10 | 10 |
| range(3) | 0, 1, 2 | 3 |
| range(10, 0, -1) | 10 … 1 | 10 |
Counting down in pseudocode is FOR i ← 10 TO 1 STEP -1 ... NEXT i. In Python it is range(10, 0, -1).
The pseudocode you already know includes its last value.
FOR i ← 1 TO 10 runs with i = 10. Python's second number is where it
stops, so the same loop is range(1, 11). One character, and it is the
most expensive one in this block.
One number on its own starts at zero. range(3) gives 0, 1, 2. That
feels wrong today and it will feel normal next month, because every list you touch starts
at 0 as well.
The indentation is the NEXT
Pseudocode ends a FOR loop with the line NEXT i. Python has
no NEXT line. The indentation does that job. Same words, four spaces moved, different
program:
No word changed between those two programs. Four spaces did.
The outer loop is the row. The inner loop is the column.
The inner loop runs all the way through on every single pass of the outer one, so a 3 by 4 pair of loops runs the middle twelve times.
For columns that line up, join a tab, "\t", instead
of a space. Step 23 uses tabs.
Build the line, then print it. Printing inside the inner loop puts every value on its own line and the grid disappears.
When the inner loop's count depends on the outer counter, for Col in range(Row),
you get a staircase instead of a rectangle. That is the whole trick behind every pattern
question in steps 24 to 28.
How the marks are given
Paper 4 awards nothing for describing a loop. It awards marks for a program that runs and prints exactly what was asked for.
A total starts at 0. Add to it inside the loop. Print it after.
A count works the same way, but it only adds 1 when an
if is true.
Print after the loop, not inside it, or you get a line for every pass.
Six ways this block goes wrong
TRAP“It printed something, so it worked.”
range(1, 20)gives nineteen lines and every one of them is correct. Nothing is red. Count the lines against what the question asked for.Writing
i ^ 2for squared.It does not crash and it does not warn you. It gives a wrong number. Powers are
**.Total = iinstead ofTotal = Total + i.The answer looks almost plausible, which is what makes it dangerous.
Forgetting the colon, or forgetting to indent the next line.
Printing inside the inner loop when you wanted a grid.
Expecting
range(3)to start at 1. It starts at 0, every time.
What you do and don’t need
You do need
- a
forloop with every form of range, forwards and backwards - a running total and a count built inside a loop
- an
ifinside a loop, indented correctly - a loop inside a loop, and why the inner one finishes first
Not yet, or not at all
whileloops: that is block Dbreakandcontinue: block D- list comprehensions,
enumerate,zip: you will not need them for any mark in this course - looping over a list directly; you will use an index instead
One word picks the loop for you
When a question says how many times, the count is known before the loop starts,
so it is a for loop. When it says until, it is not, and you need block D.
Reading that one word correctly chooses the right loop before you write a line.
Remember these three
- Python's range stops before its second number.
FOR i ← 1 TO 10becomesrange(1, 11). - The indentation decides what is inside the loop. There is no NEXT.
- The inner loop finishes completely on every pass of the outer loop. Build the line inside, print it outside.
Steps 17 to 28
Step 22, Squares, uses the operators from Block A. Read it there.
See all 12 stepsBlock C · Loops that count, and loops inside loopsProgress is saved on this device.