3.2 Looping With For
One of the most basic ways to create a block of code that will run repeatedly is with the
for loop. This lesson will be devoted to just that topic.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
3.2 Looping With For
In this lesson we're now going to introduce the first of the standard looping statements that we can use to help control the flow of our application and that's going to be the for statement. Now we're gonna use the for statement to iterate over a particular list. So if you recall from one of our previous lessons, a list is really just a grouping of different items that we can put together in a single variable. So just to kind of play around with this a little bit, let's go ahead and start up our shell. And we're going to start by defining a list. So we'll say list is going to be equal to, and we're just gonna put a few digits in here, 1, 2, 3, 4, and 5. So now we have a list, now what if I wanted to go through all the numbers in this list or all the values in this list and do something with them? Something simple like maybe print them out, how would I go about doing that? Well, we can use a loop for, and in this particular example, the for loop. To iterate over them. So what does a for loop look like in Python? Well, I'm going to warn you it's going to be a little bit different than any other programming languages you might have used if you've ever done anything in the vein of C or C++ or Pascal or C Sharp or Java or all those types of things. We are going to do this, and the for loop in Python is somewhat similar to maybe a for each loop that you might see in other languages, if you're familiar with that construct. So the way that this works is we're going to start with the key word for, and then we're going to give a variable name that we ultimately want to refer to each of the elements within this list with. So in this case, we'll just say for item. And so this can be any word that you want, any variable name. It could be I or V or whatever. But I'm just going to call this item to be a little bit more specific as to what it is I'm talking about. And now we need to specify that for each of these items in our case, in a certain list, we want to do something. So we're going to use the in keyword so for each item, in list. So now we see the for keyword. Now we have a variable name here that's going to specify what we're going to be able to refer to each of the items in the list as in, the keyword in to now specify what list we wanna be looking in, and it just so happens that we want to be looking in list. So there is the beginning of our for statement. So then we can once again specify that we're going to run some sort of block of code that has some sort of scope, so we're going to use our colon again, and then from this point now, we're going to be allowed to enter in more lines, so I'm going to go ahead and tab in. And now I can say what it is I want to do with this particular item. So what's going to happen is, this for loop is going to go through this entire list for us starting with the first index, the index 0, and move through all of them. And as it goes through each one of them these values within here are, one at a time, going to be assigned to the item variable. So now I can say print and I wanna print out item. So this is a fairly simple piece of logic. So I can hit enter and it's going to allow me to write extra code in here for this for loop, I'm not necessarily going to do that, I'll just hit enter again. And as you can see here, it's gone through each one of those items and has printed out, let me get this out of the way so it's easier to see, it has printed out each of those values. Now I can do any sort of logic I want to do in here, I can do several different lines, I can put big blocks of code in here to do a lot of logic on all of these. And really, the sky's the limit, you can do just about anything you want. Another interesting thing that you can do with this is I can start to write this same loop again, so I can say for item in list. Now in this case, what if I only wanted to deal with a subset of that list or, as we talked about before, a slice of that list, how could I do that? Well, I could say I want to iterate for all the items in the list and I can then give it a range here, or a slice of this list or I can say I wanna start with the first item and I wanna end on the third item. And at that point I can then do, excuse me, I forgot my colon. And then I can go ahead and jump in here and say print item once again. And now I only get the second and third item, so as you can see here, the for list is really wanting to deal with some sort of list. Now there's a couple different ways that you can do these lists, but ultimately that's what it's looking for. So I'm going to close out of this and I'm gonna open up my text editor again and let's give ourselves a new file. And let's start to write a little bit of an application or a little bit of code here that's going to do a little bit of logic in a for loop. So let's go ahead and write a very simple little application that's going to take in some data in the form of a list and then loop through it and then do something with that data. So we're going to create a simple little list and we'll call this names. And we'll set of equal to a list. And we'll have been here Derek, Jensen, Tuts+, and then finally Python. So here is a simple little list of names. And what we wanna do is we want to loop over them and we wanna perform some sort of logic on them. So let's start by creating our for loop, so we'll say for name in names. So once again, as you can see here I'm going to logically pick a variable name that makes sense when I'm looping over a particular list. Now this is a convention that I like to use regardless of the language I'm using. When I create a list or some sort of collection, it's typically going to be a plural name. So in this case names or words or whatever you have, and then when I create a loop I want to create the variable that's going to iterate over all the individual values its going to be similar or if not the same as the name of that list not pluralized, so in this case its gonna make sense that I'm going to say four name in names and that's just an easy way for me to logically in my head and maybe even for people that are reading my code to be able to understand what it is I'm talking about within this loop. So now I'm going to indent here, again I'm gonna once again use the tab. And in this case I'm going to say print, and I will print out the name. And that's where we're gonna start, so let's go ahead and save that, so we'll call this names, and we'll save this as a Python file. So I'll go ahead and save that. So now if I were to come back into my shell, and I'm gonna go ahead and write Python. And I want this to execute names. So as you can see here, it's going to put an individual entry on each line for each of the names within that list so that's not surprising. But what if I wanted to do something else, I wanted to print out the name and then I also maybe wanted to print out its length or something like that? Well I could do it a couple different ways I could put the len function in here that we've seen before and I could put name in here like this. I could say that, I could once again I actually use a little bit of screen splitting here so we can keep this flow going so I can say all right write the names and I can see I'm getting the length of each of those strings, I could also put it on an additional line where I could drop this length function into its own print statement underneath here. So that's fine. So I'm doing that and then what about if after that I wanted to put some sort of logic in there as well. So I'm gonna drop down to another line and this time, I'm going to combine my for loop with some if statements. So I could say now, if name is equal to, and we'll just say we'll check to see if the name is equal to Derek. And in that case, if it is I'm going to put a colon and I'm gonna drop down and indent again. So for each scope for a conditional statement, after a colon I need to indent additionally. So this is gonna introduce several layers of indenting in creating these blocks of code that are ultimately going to run in these certain scenarios. So if the name is Derek, I'm going to say print, what a great name! Because well, it just so happens that it is, so we'll go ahead and save that, so now if I come in here and I run my application again it's going to look through just like you did before it's going to come across Derek and say oh, wow you know what a great name, well thank you I appreciate that. So this is the basic way that we're going to start creating these for loops that are ultimately going to loop through all the values that we want and then do some sort of logic on them. So finally, one other thing is I could say, all right, from these names I only wanna look through a particular block. So let's say I wanted to, remember, if I just put a colon in here it's going to assume that it's gonna start at the beginning and go to the end. So actually doing this is no different then just saying names without this little slice operator here, but if I were to save that and rerun my application you see that the flow is just the same but in this case if I wanted to say start at number one which is going to be, Jensen remember zero based. I can save that, and it's going to go to the end, just like we've seen before. Now in the next lesson we're going to start to discuss another way to create these lists here, that's going to ultimately allow us to add a little bit more of idea of a dynamic spin to this instead of us having to say all right. I know I have to create this list and I'm going to have to go through all of it's indices one of the time like this there's another way for us to kind of be a little bit more descriptive as to which values we want to go through and that's in the form of creating ranges.