Lesson 3: For Loops & Counted Repetition
Counted repetition without the bookkeeping • Section 5.2 (The for Loop), 5.3 (Nested Loops), 5.4 (break, continue)
Save or Print Notes
Export this study guide as a clean PDF for exam preparation and quick revision.
1. Why Counted Loops Matter & while vs for
In production systems (e.g. Instagram's 100,000 lines of Python), 60% to 80% of all loops are counted loops.
A counted loop repeats a known number of times. A for loop automates the counter creation, testing, and incrementing.
| The Bookkeeping Job | With while Loop |
With for Loop |
|---|---|---|
| Create counter | You write i = 0 |
Python does it automatically |
| Test counter | You write while i < 5: |
Python tests automatically |
| Move counter on | You write i += 1 |
Python increments on every pass |
| Can it loop forever? | Yes, if you forget to increment | Never: range is fixed before first pass! |
| Output | 0 to 4, one per line | 0 to 4, one per line |
2. Anatomy of a for-loop & The range() Function
The Loop Header
for i in range(5):
print("Loop #", i)
for: Keyword that starts a counted loop.i: Loop variable holding the current value.in: Connects loop variable to the sequence.range(5): Generates sequence0, 1, 2, 3, 4.:(colon): Ends header line; body must be indented.
range(5) produces 5 values: 0, 1, 2, 3, 4. It never produces 5!
Three Ways to Call range()
| Call | Values Produced | Count |
|---|---|---|
range(5) |
0, 1, 2, 3, 4 | 5 |
range(2, 6) |
2, 3, 4, 5 | 4 |
range(2, 10, 2) |
2, 4, 6, 8 | 4 |
range(5, 0, -1) |
5, 4, 3, 2, 1 | 5 |
range(1, 5, -1) |
(empty range) | 0 |
print(i, end=" "), end=" " replaces the default newline with a space so values print on one line.
3. Walking Strings & The Accumulator Pattern
Looping Over a String
Strings are sequences of characters. for ch in word: visits characters from left to right.
word = "Qatar"
for ch in word:
print(ch)
# Output: Q, a, t, a, r on separate lines
Counting Vowels in a Name:
name = "Fatima"
vowels = 0 # Created BEFORE the loop
for ch in name:
if ch in "aeiou":
vowels += 1
print(vowels) # Output: 3
The Accumulator Pattern
The running total must live OUTSIDE the loop so it survives from pass to pass.
total = 0
for bill in range(5):
amount = int(input("Amount: "))
if amount < 0:
continue # skip negative values
total = total + amount
print(f"Total is {total} QR")
4. Nested Loops: A Loop Inside a Loop
The outer loop controls how many complete runs the inner loop makes. The inner loop finishes completely for every single step of the outer loop.
for i in range(3): # outer loop runs 3 times (i = 0, 1, 2)
for j in range(2): # inner loop runs 2 times (j = 0, 1)
print("i =", i, "j =", j)
# 3 outer * 2 inner = 6 passes total!
To print each row on a new line, place a bare
print() in the OUTER loop body right after the inner loop finishes!
for i in range(1, 6):
for j in range(1, 6):
print(i * j, end=" ")
print() # newline for each row!
5. Controlling Flow: continue vs break
continue (Skip This Pass)
Skips the rest of the body for that one pass and immediately jumps to the next value in the range.
for i in range(1, 6):
if i == 3:
continue
print(i, end=" ")
# Output: 1 2 4 5 (3 is skipped)
break (Leave Loop Completely)
Terminates the loop immediately and executes the code below it.
for i in range(1, 6):
if i == 3:
break
print(i, end=" ")
# Output: 1 2 (stops at 3; 4, 5 never visited)