Python Range – Range() Function in Python
Keeping you updated with latest technology trends, Join DataFlair on Telegram
1. Python Range Function Tutorial
Something you’ve always seen with a for loop, python range() function is a handy feature in Python. Like we saw in our tutorial on Python Loops, range function in python provides us with a list of numbers to iterate on. Actually, it returns a range object, which we then convert to a list to iterate on.
So, let’s begin the Python Range Function Tutorial.
2. Introduction to Range in Python
The range() function is an inbuilt function in Python, and it returns a range object. First, let’s see what it looks like.
3. Syntax of Python range() function
The Range function in python takes from one to three arguments. Let’s see how.
a. One Parameter
For an argument n, the function returns integer values from 0 to n-1.
Let’s take an example to be clearer.
[0, 1, 2]
What happens if you provide a negative value?
The fact that it returned an empty list tells us that range() is internally coded to traverse to the right. And there are no integers from 0 to the right -3, because -3 falls to the left of 0.
b. Two Parameters
Now, we’ll try different combinations for two parameters.
[1, 2, 3, 4, 5, 6]
Here, both arguments are positive. Also, 7 falls to the right of 1. So, it prints integers from 1 to 6 (7-1). Hence, we conclude that it prints integers from the first number to one from the second.
Now we try passing arguments where the first is greater than the second.
To back what we just said, it returns an empty list because 7 falls to the right of 1, and it traverses to the right from 7. Therefore, it never reaches 1.
In section a, we saw that we can’t pass a negative argument to range(). But it is indeed possible to pass a negative value or two when you pass two or more arguments. Let’s see how.
[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2]
[-7, -6, -5, -4]
This, again, returns an empty list, for -3 lies to the right of -7.
There are no integers between 3 and less than 3.
c. Three Parameters
Finally, the range() function can also take a third parameter. This is for the interval.
We’ll see this one by example.
[7, 6, 5, 4, 3, 2]
After bragging about how you can’t have the second argument smaller than the first one, now we tell you that you can. But on one condition- you must specify a negative interval. Here, we used -1 as an interval. We could’ve used -2 as well.
[7, 5, 3]
Here, note that the first integer, 7, is always returned, even though the interval -8 sends it beyond 1. Let’s now take a look at more examples for three parameters.
Traceback (most recent call last):
File “<pyshell#17>”, line 1, in <module>
TypeError: ‘float’ object cannot be interpreted as an integer
Note that all three arguments must be integers only.
[1, 3, 5]
Let’s take another example.
Traceback (most recent call last):
File “<pyshell#26>”, line 1, in <module>
ValueError: range() arg 3 must not be zero
It raised a value error, because the interval cannot be zero if you need to go from one number to another.
Actually, when we provide one or two arguments, the interval is assumed to be +1. This is why we were unable to have it traverse to the left. Now that you know what’s going on, it is easier to toy with range().
Let’s take one last example to make sure we’re on the right train of thoughts.
Now this returns an empty list in python because in a positive interval of 2 means traversing to the right, but 2 falls to the left of 12.
4. Python Iterate Function
Now that we know how to use Python range() function, don’t you want to know where to use it? Well, one application is the for loop.
>>> for i in range(6): print(i*2)
Read: Python Jobs – Python Career Opportunities
So, this was all about Python Range Function. Hope you like our explanation.
To sum this tutorial up on Python Range, range function in python is an in-built function, in Python, that lends us a sequence of integers. We can use this to iterate on using a for loop. Now you know that it’s possible to call range() in python with one, two, or three arguments. We would like you to come up with a creative use of the range() function; tell us in the comments.