Skip to content
dreamcode
dreamcode
Map
Loops
Lesson 13 of 77
+15 XP on finish
PYTHON BASICSChapter 3 · Loops and iteration

The for loop

Sometimes you want to do the same thing many times, say hello to every cloud in the sky. Instead of copying a line over and over, a for loop repeats it for you, once per item.

Worked example

How it reads

  • for cloud in range(3) means "for each of 3 turns, call the current turn cloud"
  • The indented line is the part that repeats. Python knows it belongs to the loop because of the spaces
  • range(3) counts 0, 1, 2, three numbers, starting at zero
Cloud tip: Loops start counting at 0, not 1. Nearly every programmer has tripped on this, now you won't.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add "nimbus" to the list, and after the loop print how many clouds there are in the form 4 clouds, using len(sky).

Press Run to check your work.