range() function in Python is used to generate a sequence or series of numbers, starting from 0, ending just before the given limit and increments by 1. These are the default values which can be changed as per the requirements.
Syntax
range(start, end+1, step_size)
start : This signifies the starting point of the sequence.
end : This signifies the ending point of the sequence.
step_size : This signifies the value with which each digit of sequence will either increase or decrease.
Like, in series 2, 4, 6, 8, we have two as a step size.
Do you know ?
Step size in range() function can be negative as well.
Below are the different ways in which range() function can be used :
1. range(n) : We will use this when we want to generate a sequence of numbers from 0 to n-1, keeping 1 as step size.
2. range(0, 10) : We will use this when we want to generate a sequence of numbers from 0 to 9, keeping 1 as step size.
3. range(0, 10, 2) : We will use this when we want to generate a sequence of numbers from 0 to 9, keeping 2 as step size. This series will be as ‘0, 2, 4, 6, 8’.
4. range(10, 0, -1) : We will use this when we want to generate a sequence of numbers from 10 to 1, keeping -1 as step size. Here, starting number is 10, and ending number is one before 0, i.e 1.
Let’s understand the working of range() function with the help of above example :
Program : To traverse a given list of items.
# for loop illustration with range()
planets = ['mercury', 'venus', 'earth', 'mars']
for planet_iterator in range(0, len(planets)):
print(planets[planet_iterator], end=' ')

We all know that just like arrays, Python Lists are indexable too. In the above program, we were able to access each element of the given list with its index using range() function.
Consider line-3 of code
for planet_iterator in range(0, len(planets)):
This for loop will run ‘len(planets)’ times, starting from 0 and ending to len(planets) – 1.
Variable ‘planet_iterator‘ holds integer value.
Do you know ?
The Python len() method is a built-in function used to calculate the length of any iterable object like string, list, tuple, set etc.
Here, in our program, len(planets) = 4
Hence, this for loop will run four times, starting from 0 and ending to 3.
| planet_iterator | 0 | 1 | 2 | 3 |
| List[planet_iterator] | mercury | venus | earth | mars |
Hence, we now know the significance of range() function.
That’s all, see you in the next lecture.
