>> Module 01 / Day 07 / Lecture

for Loops & range()

Day 7 of a 40-day journey
You've copied and pasted the same line ten times. Today you stop. A loop says "do this N times" — once.
Lecture
75 min · 09:00–10:15
Lab
Sum 1..N + Times Tables
Module
M1 · Python Foundations
DAY 07 / 40
>> Today's roadmap

Five stops, then a lab

  1. The for loop shape — for i in ...:
  2. range() — counting from here to there, with a step
  3. The accumulator pattern — a running total
  4. Looping with a counter + building output line-by-line
  5. Lab — Sum 1..N + Times Tables
>> The hook

Computers are brilliant at boring, repeated work.

You would never hand-write "1, 2, 3, ... 100". Neither should your code. A for loop runs the same block once for every item in a sequence.

first_loop.py
for i in range(5):
    print("Hello!")
# Prints "Hello!" five times — no copy-paste.
>> 01 / 05 · The shape

The for loop, part by part

Four pieces: the keyword for, a loop variable, the keyword in, and something to loop over. The colon and indentation mark the body.

shape.py
for i in range(3):
    print(i)

# Output:
# 0
# 1
# 2
The loop variable.
i takes each value in turn — first 0, then 1, then 2. You pick the name; i is just convention.
>> 02 / 05 · range()

Three ways to call range()

range(stop)
range(5)
0 1 2 3 4

Starts at 0, stops before 5.

range(start, stop)
range(2, 6)
2 3 4 5

From 2, stops before 6.

range(start, stop, step)
range(0, 10, 2)
0 2 4 6 8

Counts up by 2 each time.

>> 03 / 05 · The accumulator

The running-total pattern

Set up a variable before the loop, then update it inside the loop. After the loop ends, it holds the answer.

accumulator.py
total = 0              # 1. set up BEFORE the loop
for i in range(1, 6):  # i = 1, 2, 3, 4, 5
    total = total + i  # 2. update INSIDE the loop
print(total)          # 3. use it AFTER → 15
Step by step.
total starts 0 → 1 → 3 → 6 → 10 → 15.
total += i is a shortcut for the same thing.
>> 04 / 05 · Looping with a counter

The counter does double duty

The loop variable isn't just for show — you can do math with it on every pass.

counter.py
n = 7
for i in range(1, 4):     # i = 1, 2, 3
    print(f"{n} x {i} = {n * i}")

# Output:
# 7 x 1 = 7
# 7 x 2 = 14
# 7 x 3 = 21
>> 05 / 05 · One line per pass

Building output line-by-line

Each trip through the loop produces exactly one line. Ten trips, ten lines. This is how you print a table, a menu, or a countdown.

countdown.py
for i in range(3, 0, -1):  # 3, 2, 1 — step DOWN
    print(f"{i}...")
print("Lift off!")

# 3...
# 2...
# 1...
# Lift off!
>> Putting it together

A complete tiny program

A loop, a range, and an accumulator — the exact recipe for today's Problem 1.

sum_to_n.py
def sum_to_n(n):
    total = 0                 # initialise the accumulator
    for i in range(1, n + 1):  # 1, 2, ..., n
        total += i             # add each number
    return total

print(sum_to_n(5))           # → 15
>> Common failures

Three mistakes you'll make today

  1. Off-by-one. range(1, n) stops at n-1. To include n, write range(1, n + 1).
  2. Forgetting range excludes stop. range(5) is 0,1,2,3,4 — there is no 5.
  3. Forgetting to initialise the accumulator. No total = 0 before the loop → NameError. (For products, start at 1, not 0!)
>> 11:00 – 12:00 · Lab

Today's lab — Sum 1..N + Times Tables

PROBLEM 1
Sum 1 to N

sum_to_n(n) returns 1+2+...+n using a for loop + accumulator. No formula — that's the whole point.

PROBLEM 2
Times Table

times_table(n) prints lines 1–10 like 7 x 3 = 21.

>> End of Day 07

What you can now do

  • ✓ Write a for loop and count with range() in all three forms.
  • ✓ Build a running total with the accumulator pattern.
  • ✓ Use the loop counter to print output one line at a time.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF