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

Counting with range

The range() function is incredibly flexible. You can tell it where to start and where to stop. Remember, Python stops just before the stop number.

Worked example

How it reads

  • range(1, 4) generates numbers starting at 1 and stopping before 4 (1, 2, 3)
  • print(i) runs once for each of those numbers
Cloud tip: If you call range(stop), it starts at 0. If you call range(start, stop), it starts at start.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change the range so the loop counts down from 10 to 0 in steps of 2: 10, 8, 6, 4, 2, 0.

Press Run to check your work.