Python, recognized as one of the most prominent programming languages, provides a variety of built-in functionalities that enhance numerous tasks. Among these, the range function emerges as a flexible tool for iteration and sequence generation. In this article, we will explore the syntax, arguments, and real-world uses of the range function.
The Python range function generates an immutable sequence of integers that begins at 0, increases by 1, and concludes at a designated number.
Python range Function Syntax
It has the following syntax:
range(start, stop, step)
Parameters
There are three options for the range function: start, stop, and step. A brief description of each parameter follows:
- start (optional): Indicates the beginning stage of the succession. In the event that did not give, it defaults to 0.
- stop (required): Characterizes the endpoint of the grouping. Since it is unique, the sequence will end just before it reaches this value.
- step: It specifies the increment between the sequence's values. In the event that not determined, it defaults to 1.
Return
It provides a non-changeable sequence of integers that begins at 0, increases by 1 for each step, and concludes at a designated number.
Different Examples for Python range Function
Python range Function Example 1
The following example demonstrates how the range function operates in Python.
# empty range
print(list(range(0)))
# using the range(stop)
print(list(range(4)))
# using the range(start, stop)
print(list(range(1,7 )))
Output:
[]
[0, 1, 2, 3]
[1, 2, 3, 4, 5, 6]
Explanation:
Note: We have returned a generator-like object that only prints the output on demand in the preceding example after converting the range to a Python list.
An object generated by the reach constructor can similarly be accessed through its index. It is capable of supporting both positive and negative indices.
Python range Function Example 2
The following example demonstrates how to generate a list of numbers within a specified range utilizing the range function.
start = 5
stop = 12
step = 4
print(list(range(start, stop, step)))
Output:
[5, 9]
Python range Function Example 3
Let’s consider an additional example to illustrate the functionality of the range function in Python.
for num in range(1, 10, 2):
print(num)
Output:
1
3
5
7
9
Explanation:
In this instance, the step parameter is defined as 2. Consequently, odd integers ranging from 1 to 10 are generated by incrementing the preceding value by two.
Utilizing the range Method
The range function finds extensive use across various programming contexts. It is beneficial to explore a few practical applications to highlight its usefulness.
Iterating over a sequence:
In the context of traversing a specific set of values, the range function is often utilized alongside loops, particularly the for loop. By leveraging range, we can effortlessly manage the number of iterations by defining the intended range of values.
for m in range(6):
print("Iteration", m+1)
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Generating a List of Numbers:
To generate a collection of numbers that lie within a specified range, one can utilize the range function in conjunction with other functions such as list.
num = list(range(1, 6))
print(num)
Output:
[1, 2, 3, 4, 5]