Skip to content
Study Hall
Lesson 2 of 3Coding with Python
Unit 4 · Lesson 2 of 3

Loops: Patterns and Tables of Values

Use a for loop with range to repeat a calculation, build a table of values for a relation such as y = 3x + 2, describe growing patterns, and keep a running total.

7 min read4 quick checks11 practice questionsInteractiveAsk AI about this →

Key things to remember

  1. 1for x in range(a, b): runs the indented body once for each integer x from a up to, but not including, b.
  2. 2range(n) gives 0, 1, …, n − 1 (that is n values, for a positive whole number n). range(a, b, step) counts by step; a negative step counts down.
  3. 3A table of values from a loop has three steps: choose the x-values with range, calculate y with the rule, print both.
  4. 4An accumulator starts at 0 before the loop, has each new amount added inside the loop, and is printed after the loop.
  5. 5When x goes up by 1 on each pass, a constant change in y means the relation is linear, and that change is the coefficient of x (the in ).

To graph you first need a table of values: substitute , then , then , and so on. The steps are identical every time; only the value of changes. Copying the same line of code once for every value of would work, but it is slow and easy to get wrong, and for 100 rows it would be silly. A loop tells Python to repeat a block of code, giving a variable a new value each time. Loops are how a program builds tables of values, extends growing patterns stage by stage, and adds up long lists of numbers, all without you substituting by hand.

for x in range(5):
    print(x)
Output: 0, 1, 2, 3 and 4, each on its own line. The number 5 is not printed.

Read the first line as "for each value of x in the list 0, 1, 2, 3, 4". The line ends with a colon, and the lines underneath that are indented (pushed in, usually by 4 spaces) form the body of the loop: the code that repeats. Each run through the body is called a pass (or an iteration). On the first pass x is 0, on the second pass x is 1, and so on. When the values run out, the loop ends and Python carries on with the first line that is not indented. The function range produces the values: range(5) starts at 0 and stops before 5, which gives exactly 5 values. With two numbers, range(1, 6) starts at 1 and stops before 6. A third number sets the step, the amount added each time.

CodeValues of xNumber of passes
range(5)0, 1, 2, 3, 45
range(1, 6)1, 2, 3, 4, 55
range(-2, 3)−2, −1, 0, 1, 25
range(0, 11, 2)0, 2, 4, 6, 8, 106
range(10, 0, -3)10, 7, 4, 14
The values begin at the start value and stop before the stop value. A negative step counts down.
a value x takesthe stop value (not included)range(5)012345678910115 valuesrange(1, 6)012345678910115 valuesrange(0, 11, 2)01234567891011+2+2+2+2+26 values
Fig 1Three ranges drawn on number lines from 0 to 11. range(5) fills the dots 0 to 4, range(1, 6) fills 1 to 5, and range(0, 11, 2) fills 0, 2, 4, 6, 8 and 10 with hops of 2. The stop value of each range is an open circle: it is never reached.
Stop before the stop value
To get the whole numbers from 1 to 10, write range(1, 11), not range(1, 10); the second one stops at 9. With a step of 1 and a start below the stop, a quick count is stop minus start: range(1, 11) gives values. Indentation matters as much as the range: only the indented lines repeat. A missing colon, or a for line with no indented line under it, stops the program with an error before anything runs.
VideoKhan Academy's Python course shows how a for loop with range repeats its body, and traces the loop variable as it changes on each pass.Watch on YouTube

Building a table of values

for x in range(0, 6):
    y = 3 * x + 2
    print(x, y)
Output, six lines: 0 2, 1 5, 2 8, 3 11, 4 14 and 5 17.
start the loopanother x inrange(0, 6)?yesy = 3 * x + 2print(x, y)back for the next xnoloop endsPassxy = 3x + 2Output103(0) + 2 = 20 2213(1) + 2 = 51 5+3323(2) + 2 = 82 8+3433(3) + 2 = 113 11+3543(4) + 2 = 144 14+3653(5) + 2 = 175 17+3Each pass, x goes up by 1 and y goes up by 3.range(0, 6) gives 0, 1, 2, 3, 4, 5.The stop value 6 is not included.
Fig 2Flowchart of the loop: while there is another x in range(0, 6), calculate y = 3x + 2, print x and y, and go back for the next x. The table shows the six passes, from x = 0, y = 2 up to x = 5, y = 17, with y going up by 3 each pass.

The program breaks the job into three small steps, which is how you plan any program: choose the inputs (the x-values, from range), apply the rule (the line that calculates y), and show the result (print). Look at the output: each time x goes up by 1, y goes up by 3. That constant change, called the first difference, is the coefficient of in , and a constant first difference is the sign of a linear relation, one whose graph is a straight line. The value at is 2, the constant term. To make a table for a different line, change only the rule, for example to , and run the loop again.

Quick checkTry it before you read on
1
What is the last line this program prints?for x in range(1, 4): print(x, 10 - 2 * x)
Quick checkTry it before you read on
2
A loop prints a table of values for , with x going up by 1 on each pass. How does y change from each line to the next?

Growing patterns and running totals

total = 0
for n in range(1, 11):
    tiles = 3 * n + 2
    total = total + tiles
print("Tiles needed for stages 1 to 10:", total)
Output: Tiles needed for stages 1 to 10: 185.
Aprint inside the loop (indented)
  • Runs once on every pass.
  • Indenting the last line would print the message 10 times, once with each running total: 5, 13, 24, 38, 55, 75, 98, 124, 153 and 185.
  • Use it to see a table or to watch a value change.
Bprint after the loop (not indented)
  • Runs once, after the last pass.
  • Prints only the final total, 185.
  • Use it when you need just the answer.

The tile pattern from the algebra unit has tiles in stage . To find how many tiles you need to build all of stages 1 to 10, the program uses an accumulator: a variable that starts at 0 before the loop and has each new amount added to it inside the loop. After the last pass it holds . If total = 0 were moved to the top of the loop body, it would be reset to 0 on every pass and the program would print just 32, the last stage's tiles.

tiles added on this pass (3n + 2)total from earlier passestotal = total + tiles05010015020051+5132+8243+11384+14555+17756+20987+231248+261539+2918510+32ntiles
Fig 3The accumulator total drawn to scale after each of the 10 passes. Each bar is the previous total plus the new stage's 3n + 2 tiles (the solid top piece), so the totals climb 5, 13, 24, 38, 55, 75, 98, 124, 153 and 185.
for n in range(1, 7):
    print(n, 2 * n, n * n, 2 ** n)
Output: 1 2 1 2, then 2 4 4 4, 3 6 9 8, 4 8 16 16, 5 10 25 32 and 6 12 36 64.

One loop can compare three patterns side by side. The second column, , goes up by 2 every stage: linear. The third, (the square numbers), goes up by 3, 5, 7, 9, 11, so its first differences are not constant: non-linear. The last, (doubling), goes up by 2, 4, 8, 16, 32 and soon leaves the others far behind: at stage 10 it is 1024 while is 100. Changing the rule inside the loop is all it takes to explore a new pattern.

Loading interactive…
Choose the table-of-values or running-total program and step through the loop one pass at a time, watching the loop variable, the calculated value and the total change.
Quick checkTry it before you read on
1
What does this program print?total = 0 for n in range(2, 6): total = total + n print(total)
Quick checkTry it before you read on
2
A loop prints a rule for n = 1, 2, 3, 4, … Which rule gives a linear pattern?

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Write a loop that prints a table of values for for the integers from to , and predict the output.
Example 2
Trace this program with a table, pass by pass, and give its output.total = 0 for n in range(1, 5): total = total + n print(total)
Example 3
Lina has 120 dollars saved and adds 35 dollars every week. Write a program that prints her balance at the end of each of the next 6 weeks, then check the last line with an equation.
Example 4
Use a loop to add the first 10 odd numbers, . Then change one number in the program so that it adds the first 20 odd numbers.

Where marks get lost

  • Expecting range(1, 10) to include 10. It stops at 9; to reach 10, use range(1, 11).
  • Using range(5) when you want 1 to 5. It gives 0, 1, 2, 3, 4; write range(1, 6).
  • Putting total = 0 inside the loop. It resets the total on every pass, so a print after the loop shows only the last amount added.
  • Indenting the wrong lines: a print that should run once ends up inside the loop and prints on every pass, or a calculation that should repeat is left outside the loop and runs only once.
  • Forgetting the colon at the end of the for line, or the 4-space indent on the body. Python stops with an error before running anything.

Flashcards 7

Loop
range(1, 6)
range(0, 11, 2)
Body of a loop
+ 3 more

Practice

1
How many lines does this program print?for i in range(2, 9): print(i)
lines
2
What is the last number this program prints?for x in range(1, 10, 3): print(x)
3
What does this program print?total = 0 for n in range(1, 6): total = total + 2 * n print(total)
4
Complete the first line so the loop prints the whole numbers from 1 to 10: for n in ____:
5
What does this program print?total = 0 for n in range(1, 4): total = total + n print(total)
6
This program prints a table of values. What y-value is printed on the line where x is 4?for x in range(0, 6): print(x, 2 * x - 3)
7
Put these lines in order to make a program that adds 1 + 2 + 3 + 4 + 5 and prints the answer once.
  1. 1for n in range(1, 6):
  2. 2print(total) (not indented)
  3. 3 total = total + n (indented)
  4. 4total = 0
8
Match each range to the values it produces.
range(4)
range(1, 5)
range(0, 9, 3)
range(5, 0, -1)
range(2, 10, 2)
9
This program prints x and y for 4 values of x. By how much does y increase from each line to the next?for x in range(1, 5): print(x, 4 * x + 1)
10
What does this program print?amount = 1 for day in range(5): amount = amount * 2 print(amount)
11
A pattern has 4 squares in stage 1 and gains 3 squares each stage. Which one-line loop prints the stage number and the number of squares for stages 1 to 10?

Spot a mistake on this page? Let us know