In a previous article, we’ve talked briefly about the range function, especially we used it with the for loop. This article is going to look more deeply at the Python range function
If you were to look a Python help you would see something like this
You can see, we’d have the word range and then you’d have two brackets and within the brackets, you can see it says start/stop and in square brackets, it has the word step.
These start, stop and step will be integers that we pass into the function and these are often called arguments or sometimes parameters. If we look very carefully at it’s
"step" in square brackets which means this is optional you don’t have to have it. When t’s not there it’s assumed it’s going to be ane. If we consider the particular range function, what we can say is it’s a versatile function used to create iterables yielding arithmetic progressions.
Now the thing is what is an arithmetic progression? well 1 2 3 4 5, 0 1 2 3 4 5, – 1 – 2 – 3 – 4, 2 4 6 8 all of those are examples of arithmetic progressions.
In Python, you’ll often see range used together with lists and for loops as we saw in the previous article.
You might also like: Python for loop – Python Beginners Series
x = range (0, 10, 1)
Looking on Functions Schematically Makes it Easy!
I like to think of functions through an example and here you can see I’ve got X is assigned range 0 10 and 1 are in the brackets and you can see they’re appropriately separated by commas. The 0 is the start the 10 is the stop and the one is the step.
A good way in my view to think of a function is schematically; here you can see that I’ve got range appearing in a box and this is going to have an input and an output.
The range is if you like some kind of processing or some kind of function. What we’re going to give it is some input, it will work with our input and it will produce an output. The input we’re going to give it is the 0 which is the start, the 10 which is the stop, and the 1 which is the step. They go into the function and the function produces this arithmetic series.
If we look at the arithmetic series we can see it started at zero and that’s because the start was a zero. We can then see it goes to one to two to three, and that’s because it’s going up in steps of one because we passed it to it as the step. It ends at nine, as we’ve passed it ten that is the stop value. It means it stops before it gets to this ten.
This function has produced this arithmetic progression that it’s been assigned to X. We have a variable created X and that will be bound to this arithmetic progression. X now effectively has the arithmetic progression 0 1 2 all the way up to 9.
Changing the Step Parameter
x = range (0, 10, 2)
Let’s now look at another example of the Python range function, here we can see I’ve got X is assigned the range and in
As previously the step was 1, the same kind of a schematic approach is used here. We can see we’ve got the function that will take input and will produce output. Of course, the inputs are going to be the 0 which is the start, the 10 which is the stop, and the 2 which is the step.
They go into the function and what we get out is 0 2 4 6 8. The reason we get this arithmetic progression is that we start at 0, and you can see we go to 2 and then we go to 4 then we go to 6. The reason for this is the step was 2. It’s obvious if you go up in twos you’re going to go from 0 to 2 to 4 to 6 to 8. We stopped at 8 and the reason is if I add two to eight we get ten but the stop value was ten.
If you remember we don’t go as far as the stop value. We have to stop before ten. The function range was assigned to X. Consequently, we can say that X is actually bound to this arithmetic progression.
Use the Range function – Learn by Coding
Let’s have a look at another example of the Python range function. on this occasion, there is no better way to learn Python programming than coding. Use the Python shell below, write a range function passing the parameters: 10, 0, and -1. 10 as the start, 0 as the stop, and -1 as the step. Assign the range function outputs to a variable called X, then print it. Once done, run the code and see what happens.
#Assign values of the range function to a variable X (remember the parameters are 10,0,-1
#Print the variable X
What we want to do now, is alter the first program we used slightly as you can see here:
X = range (0, 10, 1)
Y = list (X)
I’m going to let Y equal list, and to list I’m going to pass as an argument X Then on the third line I’m going to insert Y. We’re going to print Y. Then we run the module, then we get this output here
You can see that this output is exactly the same as what we predicted when we looked at this particular example earlier. In order for us to view what the arithmetic progression was, we had to convert it to a list as you’ve seen.
I hope this was helpful for you to know how the Python range function works. if you want to learn all the Python basics in only 3 hours video, head over to my Python for Absolute Beginners Course. And please help me by sharing this article with your friends.