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)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.
| Code | Values of x | Number of passes |
|---|---|---|
range(5) | 0, 1, 2, 3, 4 | 5 |
range(1, 6) | 1, 2, 3, 4, 5 | 5 |
range(-2, 3) | −2, −1, 0, 1, 2 | 5 |
range(0, 11, 2) | 0, 2, 4, 6, 8, 10 | 6 |
range(10, 0, -3) | 10, 7, 4, 1 | 4 |
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.Building a table of values
for x in range(0, 6):
y = 3 * x + 2
print(x, y)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.
for x in range(1, 4):
print(x, 10 - 2 * x)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)- 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.
- 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.
for n in range(1, 7):
print(n, 2 * n, n * n, 2 ** n)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.
total = 0
for n in range(2, 6):
total = total + n
print(total)