Module 3

range()

Looping a specific number of times.

Looping with numbers

The range() function generates a sequence of numbers. It is useful when you want to loop a specific number of times.

Python
for i in range(3):
    print(i)
0 1 2

range(3) gives the numbers 0, 1, and 2. It starts at 0 and stops before 3. This is consistent with how Python counts from zero.

Python
for i in range(1, 4):
    print(i)
1 2 3

range(1, 4) starts at 1 and stops before 4. The first number is inclusive, the second is exclusive.

Your task

Use range()

  • Print the numbers 0, 1, 2, 3, 4 using a for loop and range().