dreamcode
Back to map
Loops · Lesson 3 of 6
+15 XP on finish
PYTHON BASICS

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.

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 output -