Python Range() Function – Learn to iterate numbers using loops
We have already used the range() function in our Python loops tutorial but in this article, we are going to understand the range() function in more detail.
So, let’s start with the Python range() function tutorial.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
The range() function in Python is an inbuilt method which is used to generate a list of numbers which we can iterate on using loops.
The range() function is a renamed version in Python(3.x) of a function named xrange() in Python(2.x).
Python range function has basically three arguments.
- Start: Integer representing the start of the range object.
- Stop: Integer representing the end of the range object.
- Step(optional): Integer representing the increment after each value.
The range() function can be called in three ways –
- range(start, stop)
- range(start, stop, step)
Let’s discuss the above ways of Python range functions in detail.
1. range(stop) – One argument
The range() function with one parameter takes the stop argument and it then returns us a range object from values between 0 to stop – 1.
We can iterate over the range object using a loop or we can also convert the range into a list.
Let see the example.
print( range(5)) print( list(range(5)))
[0, 1, 2, 3, 4, 5]
Here, we called the function range(5) and it generated numbers between 0 to (5-1) i.e. 0 to 4.
print( list(range(10) )) print( list(range(-4) ))
2. range( start, stop) – Two arguments
The range function with two argument takes the start and stop arguments.
This way we can generate the range object between any two integer values.
The integer values can be from positive to negative.
print( list(range(5, 10)) ) print( list(range( -2, 4)) )
[-2, -1, 0, 1, 2, 3]
Here, we see that the range(start, stop) function will generate numbers from start to stop – 1.
We can also generate between negative integers but if the start value is greater or equal to stop value, then we will get an empty list.
print( list(range(4, 0)) ) print( list(range(-2, -10)) )
3. range( start, stop, step) – Three arguments
The range() function with three arguments take the start, stop and a step as an argument.
With the step argument, we get more flexibility in generating numbers within a range.
This is useful in skipping numbers between the range.
print( list(range(0,10,2)) ) print( list(range(-5,5,3)) )
[-5, -2, 1, 4]
Here in the first line, we generated numbers from 0 to 10-1 by incrementing with 2.
In the second-line, we also saw the same with negative numbers.
Recently we told you that the start should be smaller than stop otherwise we will get an empty list.
There’s a way around by using a negative integer in step.
It then decrements the numbers.
Let’s see this with an example.
print( list(range(10,0,-1)) ) print( list(range(5,-5,-2)) )
[5, 3, 1, -1, -3]
Note: The range() function only takes integer values in its argument and the third argument cannot be zero.
Iterating on range object
We saw the working of the range() function and now let’s see the application.
This function is mostly used in loops to iterate over a range of numbers.
The advantage of this method is that it does not generate all the numbers range at once.
It yields the sequence one by one so it is memory efficient.
for i in range(4): print(i)
Note: The range() function only supports integers in the argument.
Using a float value in range() will raise an error.
for i in range(3.4): print(i)
File “<stdin>”, line 1, in <module>
TypeError: ‘float’ object cannot be interpreted as an integer
In this article, we talked everything about the range() function which is the built-in function of Python.
We saw three different ways to call the range() function based on the number of arguments passed.
Moreover, we saw the application of range() function in loops.
Now you can easily use Python range() function in your programs.