FREELessons: 48Length: 7.6 hours

• Overview
• Transcript

# 3.3 The Range Function

We can loop through a list of data the way we did it in the previous lesson, with a for loop. But what if you want to be a little more selective in the choice of items? That's a job for the range function.

## 10.Conclusion1 lesson, 02:08

### 3.3 The Range Function

In the previous lesson we started to talk about creating for loops to be able to iterate over a list and the way that we were iterating over a list was by specifying the name. And this would make the for loop go through each of the items within that list a single time and then do some sort of logic on them as we saw here. Now we're also able to slice that list a little bit and do that as well but what if we need wanted to be able to iterate over specific indexes of that particular list. So let's say I wanted to iterate over one, two, and three, or something like that so I could do it this way and I could definitely put the slicing operator in here and I could do that and that would be fine, but there's another way to do that in Python, and that's with something called the range function. And the range function is similar to doing this, but it's a little bit more explicit in the way that it's defined. So let's see what a range is like. So we're going to start over here just by looking at our interactive shell. So let's go ahead and start up Python. And when we do that we're going to start to use a function called range. Now, range is going to allow for a couple different variations of the number of inputs that you give it. But ultimately range is really going to return basically just a list of data. So I could specify range open close parentheses, and you're gonna see here that it's assuming that we need at least one argument, it's expecting one argument. Okay, so what is that? So the first version of range, is you can specify the upper limit of the range that you want to create, and it's going to take an integer value, and it's going to assume that the start is going to be zero. Remember most everything within Python, and in most programming languages is zero based. So I can say give me the range of ten. So the range of ten is going to create a range where it's assumed the beginning value is zero, and we're going to have ten total values. So what do I mean by ten total values? Well, let's just go ahead very quickly and create a for loop, so we'll say for value in range ten, because remember, this is going to create a list that we can then iterate through. We'll put in our colon, and we'll indent and we'll just print val. So what would you expect to show up here, zero to nine or one to ten? And the answer is zero to nine. Now, remember, this first digit, if you're just using the single argument version, is going to return. Or is actually going to create a range where you're starting at value zero, and you're going to have ten total items. So remember, you're gonna be zero through nine. So that's that digit to say how many you're going to create is definitely going to be exclusive. So it's going to go ten values. Not actually hit ten but zero to nine. So what if I want to create another range where I'm specifying the start value as well. Well, I would do the same thing I would say range and I can give it a start value. So let's say I wanted to start with two and go to ten. So once again, if I were to run this, you'll see that it's going to give me a range two to ten. So let's go ahead and create another for loop for val in range two, ten. And in this case I want to print val, so what's it going to give me? It's going to do the same thing. It's going to go from two up to ten, so in this version, we're not give, this ten is not the number of digits we want to do. It's the number at which we want to stop at. So we're going to go from two up to ten. Exclusively, so we're gonna go from two to nine once again. So I know that can be a little bit confusing, but with a little bit of practice and going through this a little bit, you're gonna start to remember that quite easily. The next option when it comes to creating ranges is to give it a third parameter, which is going to define the step or, how many we're going to skip to get to the next number within that range. So let's go ahead and create our for loop again. And we'll say for val in range. And we're going to specify we want to start at two, we wanna end at ten. Remember, that's exclusively. And then we can give it a third parameter, which is going to be the step, which is gonna be the distance between each of the numbers in that range that we want to use. So we'll use two in this case, just to specify we want a step of two. And then once again we'll go ahead and print val, so let's see what that gives us. As you can see here now we're going to start at the value two we want to end at ten exclusively and we want to do every second number so we're going to get two we're gonna skip three and go to four we're gonna skip five and go to six and we'll skip seven and go to eight and then we would skip nine and go to ten but remember ten is exclusive in this case. So as just as another example, let's do for val in range two, ten, and let's do a step of three this time. And we'll once again print val. And now we go two, now we skipped three and four, we go to five, we skip six and seven, go to eight, and then we would skip nine and ten, to go to eleven. But that is beyond the upper end of our range. So big deal, you say. What is that going to help us do? Well the nice thing about that now, is now we're able to, if we wanted to loop through a particular list again and we wanted to refer to each of the values within that list as its index within that list. We can use the range function to do that. So let's go ahead and see how that would work, so let's go ahead and dock this over on the right again. So let's modify our names file again now. So we're going to go ahead and we're going to go through all the names. So we'll save that. Now we want to introduce how we can use the range function to refer to each one of these by their index so how would I do that? So let's say we wanted to go through all of the names in our names list and refer to each one of them by their index. We would start by creating a range and I could say I want to create a range of something, so what do I want to start off there I wanna start at the beginning of the list do I want to start and the end well, we'll go ahead and start of the beginning this time. So let's go ahead and assume, well we can put the zero in here if we want. But we'll just assume that's going to start from zero which means we want to give it all of them, we wanna create a range that goes through all of the names. So now I can say I want to create a range. Now I need to specify the integer of how high I wanna go. And that's going to be the length of my names list here. So I can use my length function, my len function, and I can put in names. So I'm going to create a range that goes from zero up to the length of my list. So let's go ahead and just print out the names at this point so we'll go ahead and hit save but now we are going to print this out a little bit differently we are going to print out the index at where we are within the list and then print out the name, so let's change the name variable here to just say i to represent index. And then I want to print out i, which will say the index at which we are at within the list. And then I wanna print out the value at that index. So then I could say names, I. So I can refer to a specific value at an index within my list. So let's go ahead and save that, and I will go ahead and I will run Python, and I will give it my names dot ty file. And as you can see here now, we are printing out all the values within my list. Based on the index that they are found so I can start at index zero and wind up at index three, so that's a total of four, and I can print out all the values there. And once again, I could really play around with this a little bit. I could say, okay, I want to start at index zero. I want to go up to the length. And once again I could say I want to give it a step of two. So I can save that. I can run my application again, and now we're just going to get zero and two, Derek and Tuts plus. But another nice little trick that we can use here is to be able to use ranges to print these things backwards. So how would we do something like that? Well then we would have to really play around with our range a little bit. So let's see what we could do here. Let's say we wanted to start at the end of the list and go to the beginning, so let's just logically put this together. Well, we know zero's not going to work, so we're actually wanting to start at the end of the list, so let's just make a little hypothesis and say we want to start at length of names, so if the length of names is four, then we want to start at the end, so that's going to be where we'll start. And then we want to finish at the beginning. So let's say we want to finish at zero. And how would we get there? Well, we can specify a step of being a negative one. So we can go backwards. So let's go ahead and see what this does for us, just out of curiosity. So, we'll run this. And as we see, this is not going to work. Because the list index is out of range. So the problem here is that we're trying to go from index four. So if we were to do zero, one, two, three, and as you recall, since I'm specifying an index outside the bounds of this list, we're gonna get an exception. So we can't do this, we're gonna need to say, all right, I wanna start at the length of names minus one, so if the length is four I'm gonna start at three, zero, one, two, three. All right, that looks a little bit better, let's go ahead and save that and run. We're getting close but we have one other problem here, as you see, we did Python tuts plus, Jensen, and nothing, so we missed the first one, which is at index zero. And if you recall, like I said, the second parameter in this case is going to be exclusive. So if we wanted to go all the way back down to the beginning we'd have to say our upper range, at this case, is going to be negative one, so let's go ahead and save that. Rerun our application and there we get all of them, Python, Tuts plus, Jensen and Derek. So there you have it, that is how we're going to introduce the concept of this range function when we are using this in our for loop. And you'll see we can use this in other loops as well but it can be a very handy little function to give you ranges of integer values, so you can use those integer values to do a number of different things. And one of the more common things to use them with is by referring to specific indexes within a list.

Back to the top