Python range() Function
range() Function
The built-in range() function generates an immutable sequence of integers. It is most commonly used with for loops when you need to repeat an action a specific number of times, or when you need a series of numbers such as 0–9, 1–100, or every second number.
for i in range(3):
print("Hello")
Expected output:
Hello
Hello
Hello
What range() Returns
range() returns an object of its own data type, called range.
r = range(5)
print(r)
print(type(r))
Expected output:
range(0, 5)
<class 'range'>
Two properties make range objects special:
- Immutable — the sequence cannot be changed after it is created. You cannot append, remove, or replace numbers.
- Lazy and memory-efficient — a range does not store every number. It stores only its
start,stop, andstep, and calculates each number when it is needed.range(1_000_000)uses roughly the same small amount of memory asrange(5).
This is why printing a range shows range(0, 5) rather than the numbers themselves.
Syntax
range(start, stop, step)
| Parameter | Meaning | Required | Default |
|---|---|---|---|
start | The first number (inclusive) | No | 0 |
stop | The number to stop before (exclusive) | Yes | — |
step | The difference between consecutive numbers | No | 1 |
All three arguments must be integers. range(1.5) raises a TypeError.
Using range() With One Argument
With one argument, the value is the stop value, and the sequence starts at 0.
nums = range(8)
print(list(nums))
Expected output:
[0, 1, 2, 3, 4, 5, 6, 7]
range(8) produces 8 numbers, from 0 to 7. The stop value 8 itself is not included.
Tip:
range(n)always produces exactlynnumbers. This is whyfor i in range(n):is the standard way to repeat somethingntimes.
Using range() With Two Arguments
With two arguments, the first is the start and the second is the stop.
nums = range(4, 9)
print(list(nums))
Expected output:
[4, 5, 6, 7, 8]
The sequence begins at 4 (included) and ends at 8 (just before 9). To include a specific end value, set stop one higher — for example, range(1, 11) produces 1 through 10.
Using range() With Three Arguments
The third argument is the step: how much is added to get the next number.
nums = range(2, 15, 3)
print(list(nums))
Expected output:
[2, 5, 8, 11, 14]
Starting at 2, each number is 3 more than the previous one. The next value would be 17, which is not less than 15, so the sequence stops at 14.
Counting Down With a Negative Step
A negative step produces a decreasing sequence. The start must be greater than the stop.
nums = range(10, 0, -2)
print(list(nums))
Expected output:
[10, 8, 6, 4, 2]
The sequence stops before reaching 0.
Empty Ranges
If the start, stop, and step cannot produce any numbers, the range is empty — no error is raised:
print(list(range(5, 2)))
Expected output:
[]
range(5, 2) counts upward by 1 from 5, but 5 is already past 2. To count down, use range(5, 2, -1).
Note: A step of
0is not allowed:range(1, 5, 0)raisesValueError: range() arg 3 must not be zero.
Using range() in Loops
range() is most often used with for loops.
for number in range(5):
print("Count:", number)
Expected output:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Practical Example: Multiplication Table
number = 7
for i in range(1, 6):
print(f"{number} x {i} = {number * i}")
Expected output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
Practical Example: Sum of Even Numbers
total = 0
for n in range(2, 11, 2):
total += n
print("Sum of even numbers from 2 to 10:", total)
Expected output:
Sum of even numbers from 2 to 10: 30
range(2, 11, 2) produces 2, 4, 6, 8, 10. Using a step of 2 is more efficient than checking every number with if n % 2 == 0.
Converting a Range to a List
Because a range does not display its values directly, convert it to a list (or a tuple) to see the full sequence.
print(list(range(3)))
print(list(range(2, 7)))
print(list(range(1, 12, 4)))
Expected output:
[0, 1, 2]
[2, 3, 4, 5, 6]
[1, 5, 9]
Converting creates a real list in memory, so avoid it for very large ranges unless you actually need a list.
Indexing and Slicing Ranges
Ranges are sequences, so they support indexing and slicing just like lists and tuples.
r = range(12)
print(r[4]) # Access a single element
print(r[-1]) # Access the last element
print(r[1:6]) # Slice a range
print(list(r[1:6])) # View the slice's values
Expected output:
4
11
range(1, 6)
[1, 2, 3, 4, 5]
Note:
- Indexing returns a single integer.
- Slicing returns a new range object, not a list.
Checking Membership With in
Use in to check whether a number belongs to a range.
even_nums = range(0, 20, 2)
print(10 in even_nums)
print(11 in even_nums)
Expected output:
True
False
For integers, this check is very fast: Python calculates the answer mathematically from start, stop, and step instead of looking at every number.
Practical Example: Validating Input Range
age = 25
if age in range(18, 61):
print("Eligible")
else:
print("Not eligible")
Expected output:
Eligible
range(18, 61) covers ages 18 through 60. (For whole-number checks this works well; a comparison like 18 <= age <= 60 also handles decimal values.)
Finding the Length of a Range
len() returns how many numbers a range contains.
r = range(1, 20, 3)
print(len(r))
print(list(r))
Expected output:
7
[1, 4, 7, 10, 13, 16, 19]
Common Mistakes
| Mistake | Example | Problem |
|---|---|---|
| Expecting the stop value to be included | range(1, 10) | Produces 1–9, not 1–10 |
| Counting down without a negative step | range(10, 1) | Produces an empty sequence |
| Using a float argument | range(0, 1, 0.1) | TypeError |
| Using a step of zero | range(1, 5, 0) | ValueError |
Expecting print(range(5)) to show numbers | print(range(5)) | Shows range(0, 5); use list() |
| Trying to modify a range | r[0] = 10 | TypeError — ranges are immutable |
Related Concepts
- Python for Loops — the main place
range()is used - Python Lists — converting ranges into lists
- Python Iterators — how ranges produce values on demand