1. Introduction
range() is a built-in Python function that produces a sequence of integers, most commonly used to control how many times a for loop repeats or to generate indices for accessing elements of a sequence. Rather than building and storing a full list of numbers in memory, range() returns a lightweight, memory-efficient range object that generates values lazily as they're needed.
Cricket analogy: range() is like a scoreboard's over-counter that generates over numbers 1, 2, 3... on demand as the innings progresses, instead of pre-printing every over of a full Test match onto paper before play begins.
range() accepts one, two, or three arguments, giving you flexible control over the starting point, stopping point, and step size of the sequence it produces.
Cricket analogy: range() flexibly lets you set which over to start counting from, which over to stop at, and how many overs to skip between counts, just like configuring a highlights reel to show every third over from over 5 to over 40.
2. Syntax
range(stop) # 0, 1, 2, ..., stop-1
range(start, stop) # start, start+1, ..., stop-1
range(start, stop, step) # start, start+step, start+2*step, ... (before stop)3. Explanation
With a single argument, range(stop) starts at 0 by default and produces integers up to but NOT including stop. With two arguments, range(start, stop) begins at start instead of 0. With three arguments, range(start, stop, step) also lets you control the increment between values, including negative steps to count downward. The stop value is always exclusive — it is never included in the produced sequence.
Cricket analogy: range(50) is like counting overs 0 through 49 of a T20-style 50-over cap, never reaching over 50 itself; range(10, 50) starts the count at over 10; range(50, 0, -1) counts overs backward from 50 down to 1, like a countdown to a run chase.
range() objects support indexing, slicing, len(), and the 'in' operator, and they can be converted to a list with list(range(...)) when an actual list is needed.
Cricket analogy: range() objects support checking 'is over 25 in this spell', slicing a subset of overs, len() to count total overs, and can be converted to a full list of overs when you need the entire printed sequence.
Off-by-one errors are the most common mistake with range(): range(5) produces 0 through 4, NOT 0 through 5, because the stop value is exclusive. If you want to include 5, you must write range(6) or range(start, 6).
range() in Python 3 returns an immutable, lazy range object (not a list, unlike Python 2's range()). This makes range(10_000_000) essentially instant and memory-cheap, since values are computed on demand rather than stored all at once.
4. Example
print(list(range(5)))
print(list(range(2, 8)))
print(list(range(10, 0, -2)))
total = 0
for i in range(1, 6):
total += i
print(f"Sum 1..5 = {total}")5. Output
[0, 1, 2, 3, 4]
[2, 3, 4, 5, 6, 7]
[10, 8, 6, 4, 2]
Sum 1..5 = 156. Key Takeaways
- range(stop) starts at 0 and excludes stop from the produced sequence.
- range(start, stop) and range(start, stop, step) offer control over the starting point and increment.
- The stop argument is always exclusive — a classic source of off-by-one bugs.
- range() in Python 3 is a lazy, memory-efficient object, not a materialized list.
- Use a negative step to count downward, e.g., range(10, 0, -2).
- Wrap range() in list() only when you actually need a concrete list, not just iteration.
Practice what you learned
1. What does list(range(5)) produce?
2. What does list(range(2, 10, 3)) produce?
3. In Python 3, what type of object does range() return?
4. How would you produce the sequence 10, 8, 6, 4, 2 using range()?
5. What is a common off-by-one mistake when using range() to loop through indices 0 to 9 inclusive?
Was this page helpful?
You May Also Like
for Loop in Python
Understand how Python's for loop iterates over sequences and iterables like lists, strings, and ranges.
while Loop in Python
Learn how Python's while loop repeats code as long as a condition stays True, and how to avoid infinite loops.
List Comprehension in Python
A concise Python syntax for building lists from iterables in a single expression, including conditionals and the memory-difference vs generator expressions.
Lists in Python
A mutable, ordered sequence type in Python used to store collections of items, with a deep dive into mutation, aliasing, and common list operations.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics