Python range for fast counting and indexing
The python range built-in creates an immutable sequence of integers defined by start, stop, and step.
Use it when you need reliable counters or index offsets without allocating a list of every number.
It highlights how start, stop, and step control the integers a range produces.
For loop patterns that use range, see the Python for loop example.
Python Range Example For Step Counting
Output:
Output will appear here...
Output:
pen: 10 mm
stapler: 30 mm
How This Example Works
range(0, len(items), 2)walks list indexes with a step of 2.- The stop value is exclusive, so it never reaches
len(items). - Each index reads an item and prints a value tied to the index (10, 30).
Common Pitfalls with python range
Mistake 1: Expecting the stop value to be included.
for n in range(1, 5):
print(n)
for n in range(1, 6):
print(n)
Why it happens: range stops before the stop value, so inclusive sequences need stop + 1.
Mistake 2: Using a positive step while counting down.
for n in range(5, 0, 1):
print(n)
for n in range(5, 0, -1):
print(n)
Why it happens: a positive step only moves upward; to count down, the step must be negative or the range is empty.
Mistake 3: Passing non-integers for start, stop, or step.
for rate in range(0, 1, 0.1):
print(rate)
for basis_points in range(0, 101, 5):
rate = basis_points / 100
print(rate)
Why it happens: range arguments must be integers (or implement __index__), and the step cannot be zero.
python range vs list(range): Which to Use
Use range when… | Use list(range(...)) when… |
|---|---|
| You only need to iterate or index by integers. | You must store or mutate the numbers later. |
| You want constant memory while looping. | You are fine paying O(n) memory for a concrete list. |
| You need a sequence object with predictable start, stop, step math. | You need list methods like append or sort. |
Rule of thumb: reach for range for iteration and offsets; materialize a list only when you truly need all values in memory.
When you need index + value pairs, prefer enumerate — see the Python enumerate example.
Performance Considerations
range stores only start, stop, and step, so it uses O(1) memory even for huge sequences.
Membership checks like n in range(...) are constant time for integers, which is faster than scanning a list.
If you call list(range(...)), you pay O(n) memory and time to allocate every value, so do it only when you need a real list.
More Patterns for python range
Count down retries with a negative step.
for remaining in range(3, 0, -1):
print(f"Retrying in {remaining}s")
The negative step makes the sequence move downward, and the stop value is still exclusive. That keeps a countdown short and predictable without extra conditionals.
Generate business hours without storing a list.
for hour in range(9, 18):
print(f"{hour}:00")
range(9, 18) yields 9 through 17, which matches a 9-to-5 schedule with an exclusive stop.
This is a clean way to produce fixed slots without prebuilding the numbers.
When to Use python range
- Use it for counters, offsets, or fixed-step loops where the data is an integer sequence.
- Use it when you want a memory-efficient sequence rather than a prebuilt list.
- Avoid it for non-integer steps; scale to integers or use other numeric tools.
- Avoid
list(range(...))unless you need to store or mutate the values later.