Python / Loops

Repeated calculations

Add and multiply many numbers with a for loop, use the loop variable in the calculation, shorten with +=, and check with if inside the loop. Baccalaureate

A loop can repeat more than printing; it can repeat a calculation on the same variable. To work out 3×3×3×3, start with num = 1 and multiply by 3 four times:

Python
num = 1
for i in range(4):
    num = num * 3
print(num)
Console
81
For multiplying, start at 1; for adding, start at 0. Start a product at 0 and the answer is always 0.

Where you put print matters: indented, it prints on every turn; not indented, it prints once at the end:

Python
num = 1
for i in range(4):
    num = num * 3
    print(num)
Console
3
9
27
81

Using i in the calculation

The loop variable changes on each turn, so you can use it in the calculation. range(2, 7) gives 2, 3, 4, 5, 6, so this works out 2×3×4×5×6:

Python
num = 1
for i in range(2, 7):
    num = num * i
print(num)
Console
720

Running totals and +=

Same idea with adding: a variable starts at 0 and grows on each turn. total += i is short for total = total + i:

Python
total = 0
for i in range(1, 11):
    total += i
print("1 + 2 + ... + 10 = " + str(total))
Console
1 + 2 + ... + 10 = 55

if inside the loop

Put an if inside the loop to count or add only the numbers that pass a test. Here we count the multiples of 3 from 1 to 20:

Python
count = 0
for i in range(1, 21):
    if i % 3 == 0:
        count += 1
print(count)
Console
6
Put total = 0 or count = 0 before the loop. Inside the loop it would reset to 0 on every turn.

Practice

1Write the programCurriculum practice

Work out 5×5×5: set num to 1, repeat num = num * 5 three times with a for loop, then print num once.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Multiply the numbers from 2 to 9 in order using the loop variable i, then print the result.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

Start with num = 5040, divide it by i for i from 1 to 5 (range(1, 6)), then print num.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Add 5 to num using += so it prints 5.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
num = 0
___
print(num)
Console
Press Run to see the result.
5Write the programExtra practice

Print the 7 times table from 7 x 1 = 7 to 7 x 5 = 35, one line each, letting the computer calculate the result with the loop variable i.

HintUse range(1, 6) and str().
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

You save 15 pounds every day for 7 days. Use a loop and +=, and print the total each day as Day 1: 15, Day 2: 30 ... up to Day 7: 105.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Add up only the even numbers from 1 to 20 using an if inside the loop, then print Sum of evens: 110.

HintA number is even when i % 2 == 0.
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • For repeated multiplying start with num = 1; for adding start with total = 0, before the loop.
  • You can use i in the calculation: num = num * i.
  • x += 3 means x = x + 3; an if inside the loop picks which values to add or count.