Chapter 018 / 132 · Level 1 · Beginner Python textbook

For loops, range, and repeating over data

Complete Python PDF
Checking PDF availability…

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.

Code + real outputPractice + explanationsOffline Python exercises
Course disclaimer · independent educational content
EasyTutorGuide creates independent educational tutorials. Python and other product names remain the property of their owners; this course is not endorsed by the Python Software Foundation or third-party providers. Examples are for learning and should be reviewed and tested before production use. Do not expose real API keys or private data. Translation is automated and may introduce errors; code examples remain untranslated. Terms · Privacy · Legal.
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
PINPOINT LEARNING

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 · runnable
PYTHON CODE
for number in range(4):
    print(number)
Verified execution ▼
0
1
2
3

Why it works: The stop value 4 is excluded.

Example 2: Count in steps

Python · runnable
PYTHON CODE
for number in range(2, 9, 2):
    print(number)
Verified execution ▼
2
4
6
8

Why it works: Start at 2, stop before 9, and step by 2.

Example 3: Process each character

Python · runnable
PYTHON CODE
for letter in "cat":
    print(letter)
Verified execution ▼
c
a
t

Why it works: Each character is processed in its own iteration.

Example 4: Total a group of numbers

Python · runnable
PYTHON CODE
prices = [5, 8, 12]
total = 0
for price in prices:
    total += price
print("Total:", total)
Verified execution ▼
Total: 25

Why it works: The loop variable price takes each list value.

Example 5: Use enumerate for position

Python · runnable
PYTHON CODE
pets = ["cat", "dog", "fish"]
for position, pet in enumerate(pets, start=1):
    print(position, pet)
Verified execution ▼
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 · runnable
PYTHON CODE
scores = [40, 85, 61, 93]
for score in scores:
    if score >= 60:
        print("Pass:", score)
Verified execution ▼
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 · runnable
PYTHON CODE
for n in range(1, 4):
    print(f"3 x {n} = {3 * n}")
Verified execution ▼
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

  1. Print 5, 4, 3, 2, 1 using range.
  2. Find the total of 2, 4, and 6 using a for loop.
  3. Print each letter in the text "AI".

Worked answers

Worked solution 1: Worked answer: count backward

Python · runnable
PYTHON CODE
for n in range(5, 0, -1):
    print(n)
Verified execution ▼
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 · runnable
PYTHON CODE
numbers = [2, 4, 6]
total = 0
for n in numbers:
    total += n
print(total)
Verified execution ▼
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.

Progress check

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.