Python range() function (original) (raw)

Last Updated : 10 Mar, 2026

The range() function in Python is used to generate a sequence of integers within a specified range. It is most commonly used in loops to control how many times a block of code runs.

**Note: range() returns a lazy iterable, not a full list. It generates numbers dynamically instead of storing them all in memory.. To access elements like a list, convert it using list(range(...)).

**Example: This example shows the use of range() to generate numbers starting from 0 up to (but not including) a given value.

Python `

for i in range(5): print(i, end=" ")

`

**Explanation:

Syntax

range(start, stop, step)

**Parameters:

**Return: A range object representing the sequence

Examples

**Example 1: This example generates numbers starting from a custom value and ending before another value.

Python `

for n in range(5, 10): print(n, end=" ")

`

**Explanation:

**Example 2: This example generates even numbers by skipping values using a custom step size.

Python `

for v in range(0, 10, 2): print(v, end=" ")

`

**Explanation:

**Example 3: This example demonstrates how range() can be used to generate numbers in reverse order by providing a negative step value.

Python `

for i in range(10, 0, -2): print(i, end=" ")

`

**Explanation: