100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

range() Function in Python

Learn how Python's built-in range() function generates sequences of numbers efficiently for use in loops.

Control FlowBeginner7 min readJul 7, 2026
Analogies

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

python
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

python
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

text
[0, 1, 2, 3, 4]
[2, 3, 4, 5, 6, 7]
[10, 8, 6, 4, 2]
Sum 1..5 = 15

6. 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

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#RangeFunctionInPython#Range#Function#Syntax#Explanation#Functions#StudyNotes#SkillVeris