Python `range()`

function is very popular and widely used especially when you are working with the for and while loops. This function returns an immutable sequence of numbers in a given range. Using this function you can even generate a sequence of numbers in a given step size.

In this tutorial, we are going to learn the working of the `range()`

function with the help of relevant examples in detail.

numbers = range(5) numbers = list(numbers) print(numbers) # Output: [0, 1, 2, 3, 4]

## Syntax of range() Function

The syntax of the `range()`

function is as follows:

range(start, stop, step)

## range() Function Parameters

The `range()`

function can accept one, two, or at most three parameters. These parameters are given below:

**start**– (Optional) An integer which is the starting point of the sequence. The default value is 0.**stop**– An integer value denoting the ending point of the sequence. This is never included in the end result.**step**– (Optional) An integer value denoting the incremental change between any two consecutive numbers in the sequence. The default value of step size is 1.

## range() Function Return Value

The `range()`

function returns an immutable sequence of numbers of type range. It returns an empty sequence if the start and stop both parameters value is the same. Don’t worry we will discuss each possible combination in detail.

## Working of range() Function

The result of the `range()`

function depends on the number of parameters passed to it. Therefore, we will first pass only one parameter(stop), then two(start & stop), and then three(start, stop & step) and will understand the working of the range() function in each case. So, let’s get started.

**1. Pass only one parameter(stop) to the range() function**

When we pass only one parameter to the `range()`

function, it starts the sequence from 0 and ends at stop-1 value i.e. stop value is never the part of the sequence.

**Example :**

numbers = list(range(0)) print(numbers) numbers = list(range(5)) print(numbers) numbers = list(range(10)) print(numbers)

**Output:**

[] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

**2. Pass two parameters(start & stop) to the range() Function**

As we saw in the above example, by default the `range()`

function starts at 0. But, if we want to start it at any other integer, we can easily do this by passing the start parameter to it.

**Example :**

numbers = list(range(1, 10)) print(numbers) numbers = list(range(5, 10)) print(numbers) numbers = list(range(7, 10)) print(numbers)

**Output:**

[1, 2, 3, 4, 5, 6, 7, 8, 9] [5, 6, 7, 8, 9] [7, 8, 9]

**3. Pass all three parameters(start, stop & step) to the range() Function**

What if we want a sequence of numbers starting from 1 to 10 with an incremental difference of 2 or 3? Well, the third parameter(step) of the range() function helps us achieving this. See the example given below:

**Example:**

numbers = list(range(1, 10, 1)) print(numbers) numbers = list(range(1, 10, 2)) print(numbers) numbers = list(range(1, 10, 3)) print(numbers)

**Output:**

[1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 3, 5, 7, 9] [1, 4, 7]

## range() function with negative values

Sometimes a situation may occur where you need a sequence of negative numbers like excessing list items from the end of it. In such situations, the `range()`

function becomes very helpful. Because we don’t have to explicitly make the number negative as the range() function takes care of that.

**Example :** **Reverse range() function**

numbers = list(range(-10)) print(numbers) numbers = list(range(-10, -1)) print(numbers) numbers = list(range(-10, -1, 2)) print(numbers)

**Output:**

[] [-10, -9, -8, -7, -6, -5, -4, -3, -2] [-10, -8, -6, -4, -2]

## range() function with for loop

As I already mentioned, the `range()`

function is mainly used with the for loop and it’s time to implement what I said. So let’s just do that.

**Example 1:** **Print all even numbers from 1 to 10**.

for i in range(2, 11, 2): print(i)

**Output:**

2 4 6 8 10

**Example 2: Access list items using range()**

numbers = [1, 2, 3, 4, 5] length = len(numbers) for i in range(length): print(numbers[i])

**Output:**

1 2 3 4 5

## Accessing range() Items

As we already discussed, the `range()`

function returns a sequence object of numbers in a given range. Therefore, like the lists and tuples, its items can also be accessed using both positive and negative indexing.

**Example 1: Access range() items using positive indexing**

numbers = range(5) print('numbers[0] is', numbers[0]) print('numbers[1] is', numbers[1]) print('numbers[2] is', numbers[2])

**Output:**

numbers[0] is 0 numbers[1] is 1 numbers[2] is 2

**Example 2: Access range() items using negative indexing**

numbers = range(5) print('numbers[-1] is', numbers[-1]) print('numbers[-2] is', numbers[-2]) print('numbers[-3] is', numbers[-3])

**Output:**

numbers[-1] is 4 numbers[-2] is 3 numbers[-3] is 2