In this section, we will learn what the range() function is and how to use it in Python.
Note: in this section, we’re assuming you’re familiar with the for loop in Python.
Python range() Function
The `range` function is used to create a sequence of numbers.
These sequences then could be used as the elements of a list, tuple, or in for loops.
Python range() Function Syntax
range(stop_number) range(start_number, stop_number) range(start_number, stop_number, step)
Python range() Function Parameter
The range() function takes numbers as its arguments. We can pass either one, two, or three arguments to this function.
`stop_number`: if we pass only one argument to the function, that means we want a sequence of numbers starting from the value 0 up to the specified argument (the argument itself is excluded). For example, if we set the value to be like 10, that means we want a sequence of numbers starting from the value 0 up to the value 9.
`start_number`: the second way of calling the range() function is by adding two arguments to the function. This way, the first argument (start_number) declares the starting number in the sequence. The second argument (stop_number) declares the ending number of the sequence (the second number is excluded).
For example, if we put the values 10,20 as the first and second arguments of the function respectively, that means the starting number in the sequence should be the value 10 and the final number will be 19.
`step`: the third way of calling the range() function is by passing a third argument to it which defines the steps of the sequence numbers produced by the range() function. For example, if we set the value of third argument to 2, and 4, 30 for the first and second arguments of the function respectively, that means the starting number of the sequence is 4, then two steps forward and the second number becomes 6, again two steps forward which becomes 8 as the third number of the sequence and so on… until we reach the maximum number (30 in this case).
Python range() Function Return Value
As mentioned before, the return value of this function is a sequence of numbers.
Python for i in range Example:
for num in range(5, 50, 5): print(num)
Output:
5 10 15 20 25 30 35 40 45
Python range() Function and Negative Numbers
The range() function can work with negative numbers as well!
But remember, a few notes here:
- The third argument of the `range()` function is set to +1 by default. This means the first argument of the range() function should be less than the second argument so that at each step, the sequence gets closer to the final value of the sequence.
- If we put the third argument (the step) to a negative number (like -3 for example), in that case, the first argument of the function should be higher than the second argument! Because we’ve reversed the sequence by setting the steps to a negative number! (Meaning now the highest number returns, then the steps are backward from the top number of the sequence to the lowest number).
Example: using negative numbers in Python range() function
for num in range(5, -10, -2): print(num)
Output:
5 3 1 -1 -3 -5 -7 -9
Python List and range() Function
The `range()` function can be used in a list object as well! This way, the sequence that is created from the range function will be set as the elements of the list.
Note: we put the result of the range function in the `list()` function in order to create the list object with the sequence of the range function.
Example: using range() function to create a list in Python
list1 = list(range(5, -10, -2)) print(list1)
Output:
[5, 3, 1, -1, -3, -5, -7, -9]
Python Tuple and range() Function
The `range()` function can be used in a tuple object as well! This way, the sequence that is created from the range function will be set as the elements of the tuple then.
Note: we put the result of the range function into the `tuple()` function in order to create the tuple object with the sequence of the range function.
Example: using range() function to create a tuple in Python
tuple1 = tuple(range(5, -10, -2)) print(tuple1)
Output:
(5, 3, 1, -1, -3, -5, -7, -9)