Technical skill practice
Python range Function: Exclusive Stop and Descending Steps
Python range represents an integer sequence defined by start, an excluded stop and a nonzero step. It is not a prebuilt list of every number. Convert it to a list when you specifically need to inspect or store all the values of a small range.
Start with a small, explicit task
Make synthetic chart ticks at five-unit intervals. Produce ascending and descending versions, then check a step whose direction cannot reach the stop. No charting dependency is needed because the task is to verify the exact labels before displaying them.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_range.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
[5, 10, 15]
[15, 10, 5]
zero step rejected
ticks = range(5, 20, 5)
ascending = list(ticks)
descending = list(range(15, 0, -5))
assert ascending == [5, 10, 15]
assert descending == [15, 10, 5]
assert list(range(5, 20, -5)) == []
assert len(ticks) == 3 and ticks[1] == 10
assert list(ticks) == ascending
try:
range(0, 3, 0)
except ValueError:
result = "zero step rejected"
else:
raise AssertionError("expected invalid step")
print(ascending)
print(descending)
print(result)
Explain the result
The ascending sequence contains 5, 10 and 15 but excludes 20. The descending sequence starts at 15 and stops before zero. The negative step in range(5, 20, -5) moves away from the larger stop, so no value satisfies that sequence’s direction and the result is empty.
The example obtains length and an indexed item directly from ticks, then iterates it again. Unlike a consumed map iterator, this range can be iterated repeatedly. That does not mean you should materialize a huge range merely to run a loop: the list conversion here is for a deliberately tiny checked display.
Avoid these mistakes
A zero step is invalid rather than an infinite-repeat instruction. Ordinary range uses integers, not fractional increments; choose a different explicit representation when the task requires decimal steps. Check both direction and excluded stop instead of fixing an off-by-one error by blindly adding one.
Questions, answered
Is the stop value included?
No. range(5, 20, 5) ends with 15 in this exercise.
Can I iterate over the same range twice?
Yes. The example converts ticks twice and obtains the same sequence.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.