9. Range step

The three-argument form adds a step:

  • start — first number. Included.
  • stop — where the sequence stops. Excluded.
  • step — how much each number changes. Default: 1. A negative step counts down.

With a negative step, start must be greater than stop:

#01

Count by twos with range

Loop over range(0, 10, 2) and print each number.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Use a step of three

Loop over range(1, 10, 3) and print each number.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Count down

Loop over range(5, 0, -1) and print each number.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Count down by twos

Loop over range(10, 0, -2) and print each number.

Print exactly:

Create a free account to save your progress and unlock chapter 2.