Beginner · 10 min
Counting with range
Repeat an action using a sequence of integers.
Start counting at zero
range(3) supplies the integers zero, one, and two. The stop value is excluded. It is useful for repeating a block a fixed number of times.
for number in range(3):
print(number)Output
0 1 2
Choose a start and step
range(start, stop, step) uses your start and increment, but still excludes the stop. Omitting the step uses one.
for number in range(2, 7, 2):
print(number)Output
2 4 6
Counting with range
- List the integers before running each loop.
- Check the excluded stop value.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Count tickets
Use range() and a loop to display 1, 2, 3, and 4 on separate lines.
The stop must be one greater than the last number you want.