Home Python Python Range() Function: The Definitive Video Tutorial

Python Range() Function: The Definitive Video Tutorial

If you have to repeat something a certain number of times, then you can use loops and with loops, you can add range to your arsenal to do lots of other cool things with it. This article is all about a built-in function (class) called range in python.

Hello Guys,

Welcome to this new article on Python Range. In this article, we are going to deep dive into the range function in python. This article is a one-stop guide for everything related to python range. Let us first start by discussing the python range a little bit.

What is Python Range?

Python range is a Class that generates a sequence of numbers starting from some number (say a) all the way to some number (say b) incremented by some number (say c). Hence, in other words, the generated sequence of numbers would look like a, a + c, a + 2c, ……

It will go until the result exceeds the upper number b.

Version Difference

This same function range was called xrange in the python 2 while in python 3, it is named this. But the syntax of this function was and always has been the same despite the name difference.

Let us now see how to use range in python. Let us take an example where we would like to generate a sequence from 1 to 20. To generate that, let us perform the following steps:

First, start by opening the python IDLE and then in the python shell type the following commands:

Python Range

The above command starts by first typing the name of the class “range” and then followed by parenthesis which is then given two numbers as an argument called 1 and 21.

Here, the first argument marks as the “start” of the sequence while 21 marks as the end of the argument. But the result doesn’t seem promising. The returned result is a range object which can then be used for all sorts of different purposes.

One such purpose is to iterate through the range and print the numbers one by one. We can do that quite easily in python as follows:

Python Range() Function: The Definitive Video Tutorial

Here, first, we used a for loop to access the numbers in the range object one by one and print them one by one. There are a few things worth noting here.

First, if you look closely at the syntax of the for loop, you would find out that the syntax of looping through is similar to the way we loop through a list.

If we just replace the range object with the name of any list, then the above code will work just in a similar manner but then, in that case, it would print the elements of the list.

So, in that sense, a natural question one might ask is whether range objects and list are both similar in some way. The answer to this question is “Yes”. They are similar because both of them are what I call “iterable” in python.

Iteratable in python is a type of data structure that can be easily looped. The examples of iterable are Lists, sets, tuples, and what we have just learned, range objects.

Then, the second thing that we should notice is that the last number (which is 21) is not there in the final print statement. The reason for this is because in python, range’s last value is always excluded.

Hence, if you want values till 5, then you should pass 6 as the last value. I hope you get the idea.

Now, let us call the python range’s Class once again in the for loop but this time without the first argument (which is 1). Let us see what the result looks like…

Python Range

Now, here the only difference in the output is that the printing starts from 0 instead of 1. The reason earlier one appeared because we explicitly passed 1 to the start argument of the range object.

But, here, we didn’t specify any start argument and we just passed one argument to the range constructor. When we do that, range assumes that the start is 0 and the number passed is ended. Also, the number passed will be excluded.

Now, there might be yet one another question.

If the range object is also an iterator just like a list, then why the results are different when we print range object and list.

When we print a range object, the print is just a range object as we have seen in one of the earlier screenshots.

You may like to read our guide on Python Functions: The Definitive Guide.

How can I print a range object as a list?

This is actually quite simple. We just need to pass the range object as an argument to the list constructor function. This has been demonstrated in the following screenshot…

Python Range() Function: The Definitive Video Tutorial

Here, we first created a range object and used that object as an argument to the list constructor, and the result we can clearly see a list starting from 1 all the way to 20.

What if there was no range class in Python?

If there was no range class in python, then we would have implemented it ourselves whenever we needed that.

Say, for example, assuming that there is no range class in python, that we need to generate a sequence of numbers starting from 1 to 20. Then, we might be able to do that using the following code…..

Python Range() Function: The Definitive Video Tutorial

We can clearly see that we have to write around 6-7 lines of code just to create a sequence of numbers starting from 1 to 21. We can easily achieve this same task by just using the inbuilt range class as follows….

Python Range

The highlighted code is what we need to generate numbers starting from 1 all the way to 21. So, obviously this range class does help.

How can range objects be used to iterate through a list?

Now, let us understand how we can use range objects to iterate through a list.

There is already a very simple way of iterating through a list in python and that is using the “for-in” keyword in python. One very simple example of that is displayed here…

Python Range() Function: The Definitive Video Tutorial

Now, the question is how can I use a range object to accomplish the same task. It is quite easy. We just need to remember the fact that the list’s indexing starts from 0 and it goes all the way to the length of the list subtracted by 1.

Owing to these facts, the following is the code for iterating through a list just by using range object….

Python Range

Here, first, we accessed the length of the list and then created a range object using that length. Then, we looped through this range object and accessed each value by I which will serve as the index of the list and this index has been used as a filtering index for the list.

Syntax of the range class

The range class in python has three (including nonoptional) parameters. Those parameters are “start”, “end” and the “step”.

The start parameter is an optional parameter that is not required as we have already seen in the tutorial. If this value is not set, then the default value of 0 is taken.

The end parameter is a parameter which is always required. So, we need to specify exactly where the range object will end. Now, there is one more optional argument called step which takes a default value of 1 which means the sequence will have an interval of 1. But just like other arguments, we can change this step argument a little bit.

Let us say for example we want to generate a sequence of all the odd and even numbers between 1 and 100. How would we go about it?

The following code shows how we might approach this problem using the range object in python.

Python Range() Function: The Definitive Video Tutorial

All the other things are the same, we just changed the start argument of the range and the remaining things are just the same. We took a step length of 2. For generating odds, we took it as 1 while for generating Even, we took it as 2.

Now, there is one more thing that we should know with steps and it is that it can be negative too. Let us take an example where the step is negative.

Python Range() Function: The Definitive Video Tutorial

Here, I have printed the even numbers in a reverse order. So, the start was 100 and the end was 1 while the step length is -2.

Range doesn’t work with floats

So, far we have discussed a lot of things about this range function. We saw that it has three different types of arguments called “start”, “end” and “step”. The one important thing is that this function never accepts any float argument.

You can try out this function with any float value but this function will simply not work and will throw an error.

But there might be some real situations where you actually need a range with float, then in those scenarios, you would be better off by using some external library functions such as numpy’s arange function which is just like range but works just as efficiently with float values too.

Just to show all of you guys, that when the range function is used with float value, what type of output do we get….

Python Range() Function: The Definitive Video Tutorial

Now, you can clearly see from the above screenshot that when we use float input to the range, then the python throws an error.

Underscore keyword in Python for storing variables

Python range is often used for repeating a certain portion of code a certain number of times. In those cases, when we use range, instead of using a variable such as i, many a times, an underscore (_) character is used. Let us quickly see an example of this:

Python Range() Function: The Definitive Video Tutorial

Here, we just wanted to repeat a piece of code a certain number of times (10 in this case).

Another way of accessing the range values

Earlier we discussed how range objects and lists are very similar to each other. We also showed how a range object can be converted to a list.

But what if you don’t want to convert range objects to a list but rather be interested in directly accessing the range values. Is there any way to do this?

Yes, there is.

We can access values of range objects just as similarly as we do this for lists. We can access those using the indexing method. The following screenshot demonstrates that only….

Python Range() Function: The Definitive Video Tutorial

In the above example, we first created a range object storing values from 0 to 10 and then accessed them using the square bracket indexing method of python. In particular, we assessed values at the last index and 2nd index.

Using range to loop over a string character

The string in python is also considered as an iterable just like a list. The individual characters of that string can be accessed using a loop as follows.

Python Range() Function: The Definitive Video Tutorial

Now, the same can be accomplished using the range as follows:

Python Range() Function: The Definitive Video Tutorial

Here, we used the range with lento produce that same exact result.

Okay, now we are almost done with this article. We only need to discuss a few more use cases and problems based on range.

Problem. 1 You have been given a list of values such as 76,23,45,45,89,100. Calculate the sum of these values using the range function.

Answer Code: Here is the answer code for this particular problem.

python range function

Problem. 2 Use range to print individual words of the text “He is a great guy”.

Answer Code: Here is the answer code of the above problem.

Python Range() Function: The Definitive Video Tutorial

Here, we first stored the text in a separate variable and used a split method of string to split the text into words separated by space. Then, finally, we used range to loop through the indexes and then printed the words in the text.

Now it’s time to explore our Python Range() Function video tutorial guide.

Python Range Function(): The Definitive Beginners Guide

So, with this last discussion, I will stop right here and I hope that you have been able to follow through this tutorial effectively.

Thank you so much for reading the article. 🙂