For loops, range, and repeating over data
Chapter 18 of 132 · Level 1 · Beginner Python textbook · 7 real coding demonstrations + 2 worked code answers. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Start here: simple explanation
When you know which items to process, a for loop is often clearer than a while loop.
A for loop takes one item at a time from an iterable. An iterable is something that can provide a sequence of items, such as a string or a list.
range(5) generates 0, 1, 2, 3, 4. The stop value is excluded. range(2, 8, 2) gives 2, 4, 6, which is a start, stop, and step.
The loop variable is assigned a new value on each pass. A body can contain several instructions, which must be indented consistently.
Use enumerate when you need both an item and its position. Prefer direct iteration over a collection instead of manually managing indexes when you do not need them.
02 · Learning objectives and technical words
By the end you can…
- Use range() to count
- Loop over strings and lists
- Combine loops with conditions and running totals
Words you should know
- iterable
- a value that can provide items one at a time
- range
- a tool for producing a sequence of integers
- enumerate
- a tool yielding positions and items
- nested loop
- a loop inside another loop
03 · Point-by-point: understand the idea
Locate the input, identify the Python instruction that changes it, and compare the printed result. Make a prediction before each run so you understand what the program does—not only how to copy it.
range(1, 5) does not include 5. This is a common off-by-one mistake. State the first and last value you expect before running a loop.
04 · Coding examples with real outputs
Example 1: Count from zero
Python · runnablefor number in range(4):
print(number)
0 1 2 3
Why it works: The stop value 4 is excluded.
Example 2: Count in steps
Python · runnablefor number in range(2, 9, 2):
print(number)
2 4 6 8
Why it works: Start at 2, stop before 9, and step by 2.
Example 3: Process each character
Python · runnablefor letter in "cat":
print(letter)
c a t
Why it works: Each character is processed in its own iteration.
Example 4: Total a group of numbers
Python · runnableprices = [5, 8, 12]
total = 0
for price in prices:
total += price
print("Total:", total)
Total: 25
Why it works: The loop variable price takes each list value.
Example 5: Use enumerate for position
Python · runnablepets = ["cat", "dog", "fish"]
for position, pet in enumerate(pets, start=1):
print(position, pet)
1 cat 2 dog 3 fish
Why it works: start=1 makes the displayed numbering friendly to people.
Example 6: Filter values with a condition
Python · runnablescores = [40, 85, 61, 93]
for score in scores:
if score >= 60:
print("Pass:", score)
Pass: 85 Pass: 61 Pass: 93
Why it works: The if is inside the for body; only passing scores are printed.
Example 7: A tiny multiplication table
Python · runnablefor n in range(1, 4):
print(f"3 x {n} = {3 * n}")
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9
Why it works: An f-string reports the arithmetic in a readable form.
05 · Exercises, debugging and worked answers
Try without looking
- Print 5, 4, 3, 2, 1 using range.
- Find the total of 2, 4, and 6 using a for loop.
- Print each letter in the text "AI".
Worked answers
Worked solution 1: Worked answer: count backward
Python · runnablefor n in range(5, 0, -1):
print(n)
5 4 3 2 1
Why it works: A negative step moves down toward the excluded stop 0.
Worked solution 2: Worked answer: list total
Python · runnablenumbers = [2, 4, 6]
total = 0
for n in numbers:
total += n
print(total)
12
Why it works: Each iteration adds one number to the accumulator.
06 · Build a mini-project and review
Mini-build: A practice quiz scorer that loops over an answer list and counts correct answers.
Can you explain the output before running the code? Can you change the inputs without breaking the program? Revisit the examples until you can do both.
