Last Updated : 11 Jul, 2025
range()
function in Python is used to generate a sequence of numbers. It is widely used in loops or when creating sequences for iteration.
Let’s look at a simple example of the range() method.
Python
# Generate a sequence of numbers from 0 to 4
for i in range(5):
print(i)
Explanation:
0
up to 4
(excluding 5
) and prints them.range()
Method
Parameters:range(start, stop, step)
0
.1
.range
object, which can be converted to a list or used directly in loops.range()
Method 1. Basic Usage with Default Parameters
When range() function called without arguments defaults to starting at 0 and ending at a specified value (exclusive). It generates a sequence of numbers from 0 to that value.
Python
# Generate numbers from 0 to 4
print(list(range(5)))
Explanation:
0
by default and ends at 5
(exclusive).We can specify both the starting and stopping points of the range. The range(start, stop)
generates numbers starting from the start
value up to (but not including) the stop
value.
# Generate numbers from 2 to 9
print(list(range(2, 10)))
[2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
2
and ends at 10
(exclusive). range(start, stop, step)
allows us to specify a step value, which controls the difference between consecutive numbers in the range. A positive step generates an increasing sequence, while a negative step generates a decreasing one.
# Generate numbers from 1 to 9 with a step of 2
print(list(range(1, 10, 2)))
Explanation:
2
in each step.By specifying a negative step value, the range()
function creates a sequence that decrements from the starting point down to the stopping point, providing reverse order numbers.
# Generate numbers from 10 to 1 in reverse
print(list(range(10, 0, -1)))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Explanation:
range()
range()
does not store all values in memory but generates them on demand, making it ideal for large sequences.range
object cannot be changed after creation.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4