Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • 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.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
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