
Hello Techies,
This tutorial is based on the Python range() function, In this blog, you will learn How to use the range function with the help of multiple examples.
Table of Contents
Python range() Function
The range() function generates a sequence of numbers starting from 0 by default, and increments by 1, till the given number. range() is a built-in function in Python.
Syntax of range() function
range(start, stop, step)
It has three parameters, with two options:
- start: Starting position. If we do not specify a start by default, it is 0. It’s an optional parameter.
- stop: Ending position. The range of numbers stops one element before the stop. If the stop is j, it will stop at the exact j-1. It’s a mandatory parameter.
- step: Increment by stepsize. If we do not specify a start by default, it is 1. It’s an optional parameter.
How to use Python range() function
range(stop) with One Argument
When you pass only one argument in range() it will create a sequence of numbers from 0 to stop-1.
Here, start = 0 and step = 1 as default values.
Example:
# Print the first 10 numbers for i in range(10): print(i, end=' ')
Output:
0 1 2 3 4 5 6 7 8 9
range(start, stop) with Two Argument
When you pass two arguments in range(), it will create an integer starting with the start number to stop -1.
Note:
- Here, step = 1 as the default value.
- If you set a stop value lower than the start, the range will return an empty sequence.
Example:
# start = 1 # stop = 9 for i in range(1, 9): print(i, end=' ')
Output:
1 2 3 4 5 6 7 8
range(start, stop, step) with Two Argument
When you pass all three arguments in range(), it will start with the start number, increase by step number and return the sequence of numbers that stopped before the stop number.
Example:
# start = 10 # stop = 50 # step = 5 for i in range(10, 50, 5): print(i, end=' ')
Output:
10 15 20 25 30 35 40 45
Python range() function for loop
The range() function is usually used in a for loop to iterate the loop at certain times.
Example:
# iterate the loop 5 times for i in range(5): print(i, 'I Love Python')
Output:
0 I Love Python 1 I Love Python 2 I Love Python 3 I Love Python 4 I Love Python
Python range reverse
We can give a positive or negative number for any of the parameters in the range. This feature offers the opportunity to apply a reverse loop. We can do this by passing a high index as a starting and negative step value.
Example:
for i in range(15, 5, -2): print(i)
Output:
15 13 11 9 7
Python Range to List
The Python range object is an iterable one that lazily creates elements. In other words, all elements of the range are not stored in memory. During the iteration, only that element is returned by the Range object. The Python list is not like that. All the components in the list are stored in memory.
To convert a Python range to a list, use the Python list() constructor with the range object passed as an argument.
Example 1: Python range to list using list() constructor.
range_obj = range(2, 30, 3) list_obj = list(range_obj) print(list_obj)
Output:
[2, 5, 8, 11, 14, 17, 20, 23, 26, 29]
Example 2: Python Range to Python List using For Loop
range_obj = range(2, 20, 3) list_obj = list() for x in range_obj : list_obj.append(x) print(list_obj)
Output:
[2, 5, 8, 11, 14, 17]
Points to remember about range() function
- All arguments must be integers, be they positive or negative.
- You cannot pass a string or float number or any other type in the start, stop, and stepsize.
- The value of the step size should not be zero.
I hope you understand the concept of the Python range() function. If you have any doubt please comment down below.
