← All missions

⚑ Super-Speed Loops

πŸ” Repetition & Automation

1) What do I need to do?

🎯 Your mission: Rocket launch countdown

Use a for loop to count down from 10 to 1, then print Blast off!

2) What do I need to know?

Why loops?

Imagine printing "Pow!" 100 times. You could type 100 lines... or use a loop!

for i in range(5): means "do this 5 times". The lines inside are indented, just like if.

Each time round, the variable i holds the count. It starts at 0, not 1!

🧰 Your toolkit for this mission

Everything you need to know to finish this lesson on your own:

for loop

Repeat some lines a set number of times.

How to write it: for i in range(5): then indent the lines to repeat. i changes each time round.

for i in range(3):
    print("Pow!")

range(stop)

Counts from 0 up to but not including stop.

How to write it: range(5) gives 0, 1, 2, 3, 4.

for i in range(5):
    print(i)

range(start, stop)

Count from start up to stop βˆ’ 1.

How to write it: range(1, 11) gives 1 to 10.

for n in range(1, 11):
    print(n)

range(start, stop, step)

Count in jumps. A negative step counts down.

How to write it: range(10, 0, -1) gives 10, 9, ... 1.

for n in range(10, 0, -1):
    print(n)
print("Blast off!")

Using the loop number

Use the loop variable in maths or text.

How to write it: Print several things with commas.

for n in range(1, 4):
    print(3, "x", n, "=", 3 * n)

String Γ— number

Multiplying text repeats it.

How to write it: "#" * 3 gives ###.

for n in range(1, 4):
    print("#" * n)

Think about it

Look at this code. What happens next, and why? Say it in your own words.

for lap in range(3):
    print("Zoom!")
print("Done")
Watch & explore β–Ά

Power-up: Why loops?

See it happen

1 / 3
for lap in range(3):
    print("Zoom!")
Zoom!
Zoom!
Zoom!

A for loop repeats the indented instruction three times.

3) Run the code & terminal output

Code Lab 🐍Playground β€” experiment freely!
Hero Terminal

Press β–Ά RUN and your program's messages appear here!

Training Arena
Training Arena
Your superhero companion

Captain Py

Our next adventure is yours to create.