Python range() Function Explained With Examples

Python provides various functions that are utilized to perform different basic to complex tasks. To generate a sequence of numbers, the “range()” function is used in Python. It generates a sequence of numbers from a specific number to a user-defined number.

This Python post will discuss different use cases of the “range()” function with suitable examples. 

Python range() Function

The inbuilt “range()” function is used in Python to create the sequence of numbers by specifying the “start”, “stop” and “step size” parameters. The syntax of the “range()” function in Python is demonstrated below:

range(start, stop, step)

From the above, we state that the “start” parameter (optional) is used to represent the starting number of sequences. While the “stop” and “step” parameters are used to indicate the stop value (stop value is mandatory to define) and step size (default ‘1’) of the sequence.

Using range() Function With for Loop

The “range()” function is mostly used with “for loop” to generate the sequence of numbers in Python. The following code displays how we can use the “range()” function with the “for” loop in Python.

Code: 

for items in range(5):
    print(items)

The for loop iterates over the range sequence and prints all the sequence numbers.

Output:

The sequence created using the “for” loop and “range()” function is shown in the above snippet.

Using range() Function With Steps

You can specify the specific step size within the range() function to create the sequence according to the given “step size” value:

Code: 

for items in range(0, 50, 5):
    print(items, end=' ')

In the above code, the “range()” function takes three parameters “start”, “stop” and “step” and returns the sequence. The sequence starts from “0” and stops at “50” with a step size value of “5”.

Output:

The above output demonstrates the sequence of numbers created using the “range()” function.

Using for Loop and range() Function to Iterate Over the List

The “range()” function can also be used along with for loop and “len()” functions to iterate over the iterable list. Here is a code example:

Code: 

list_val = [1,2,3,4,5,6]
for items in range(len(list_val)):
    print(list_val[items], end=' ')

In the above code, the “len()” function gets the list length and “range()” function creates the sequences by accepting the length of the list.

Output:

The above output shows the sequence of numbers.

Using range() Function in Reversed Order

The “range()” function, along with the for loop, can be used to generate the sequence in reversed order. Let’s understand it by the following examples:

Example 1: Reversed a Range Using Negative Step Value

In the code below the “range()” function generates the sequence using negative step value:

Code:

for items in range(20, 10, -1):
    print(items, end=' ')

In the above code, the “range()” function takes “-1” as a step value. This step value decrements the number from the start position “20” to the stop position “10” of a sequence.

Output:

The above output shows the sequences generated in reversed order.

Example 2: Using range() Function to Reverse a List

The “range()” function is used to reverse an element of the list using the “len()” function. Here is an example of code:

Code:

list_value = [5, 22, 33 ,42,32]
for items in range(len(list_value) - 1, -1, -1):
    print(list_value[items], end=" ")

In the given example, the “for” loop iterates over the “range()” function that accepts the len() function as an argument. The expression “len(list_value)-1” is passed as a first argument to the range() function, which is the start of the sequence in reversed order.

Output:

The above output shows the list in reversed order.

Using Python range() Function with float

When a float value is passed as an argument to Python’s “range()” function, then a “TypeError” exception will be generated. Here is an example:

Code:

for items in range(5.5):
    print(items)

In the above code, the “range()” function takes the float value as an argument and returns the “Type Error”.

Output:

The above output shows the “TypeError” when the float is passed inside the “range()” function.

Using the range() Function to Access With an Index Value

We can also access the specific index position from the sequence using the range() function with the square bracket. Here is an example code:

Code:

print('First Element Value: ', range(10)[0])
print('Last Element Value: ' , range(10)[-1])
print('Third Element value: ', range(10)[3])

In the above code, the “range()[0]” is used to get the first element of the sequence. Similarly, to get any element from the sequence, we used “[index_value]” along with the “range()” function.

Output:

The above output shows the element value of the sequence.

Conclusion

The Python “range()” function generates the sequence of numbers from the specified starting value to stopping positions. The Python “range()” function is used along with the “for” loop to iterate over the sequence and perform certain operations. This guide presented a detailed guide on how to use the “range()” function in Python with numerous examples.