LESSON 6

Loops

Learning objective: Repeat actions with for and while loops.

Understand

Loops repeat work without duplicating code.

A for loop iterates over a sequence like a list or range(); a while loop repeats while a condition is true. Loops are essential for processing collections of data.

Analogy: A loop is like stamping copies: repeat the same action across many items.

See It in Action

Looping with range:

for i in range(3):
    print(i)   # 0, 1, 2
How it works: range(3) produces 0, 1, 2, and the loop prints each value in turn.

Try It Yourself

  1. Loop over range(5) and print each number.
  2. Loop over a list of words.
  3. Write a while loop that counts down.

Quick Quiz

What does range(3) produce?

Challenge

Print the numbers 1 to 10 using a loop.

Success condition: Your loop prints the full sequence correctly.