4.4 Loop over lists with "for" loops - Video Tutorials & Practice Problems
Video duration:
15m
Play a video:
Video transcript
<v ->We're getting to the end of some of</v> our fundamental concepts, and now we're gonna look at for loops. And so this is what I've found to be one of the more challenging concepts to understand if you are a total beginner programmer. So what for loops are is it'll keep looping, for the number of items you have. So for example, if you have a bunch of presents that you need to wrap, you'll say for each present in the number of presents I have, wrap. And then wrap that present. And then when you're out of presents, then you just stop looping. So you don't have to explicitly tell it to stop. So looking at the list of CDs that we had earlier, we can use a for loop and say for city in cities, print city. And I can run this. That's awesome. So let's look at this syntax here. We're using the for keyword to indicate that it's a for loop. And then we give each item a name. So this is a new variable that can be accessed within the for loop, but the value of this variable is going to change, depending on what part of the loop you're in. And then we say in and this, which has to be something that can be iterated over. It's something that has to be able to like, have a number of items that you can then access each item. And then we can do something with this variable, and in this case we're just going to print it. And so similar to our if statements, we end with a colon, and then we have four spaces to indent. And everything that's indented will be the code block in this for loop. So this can be anything, so I could just call this, I could call this number. And as long as I use it in here, that's fine, and it'll still print out the city. So this is just a variable that you're naming. Okay, there are other ways of, let's see, you can also loop over words. Mississippi, just cause that's a fun one to spell, so I can get each letter, and then do something with that letter. And it prints each one on a new line. And I can also do something like this. So this is a new function called range, and this is useful for doing something just, like, a certain number of times. So it's kind of like creating a new list that starts at zero and ends at four, because it's this number minus this one and you can see that it printed zero to four. And this range also takes in, can take in a number of different values, so if we went like this, it would start at five and end at nine, and if we went like this, and had another number here, then it would get every second number, So all odd numbers between five and 99. You can also have negative numbers in here, but this is the start and this is the end. So in this case, we would have to start with a larger number and end there. Okay, so that's the basics of for loops, and they can look really simple, but they can also be very powerful. So let's look at problem number eight first. So in problem number eight, I've got this code and I want it to request a word from the user, and return how many vowels that word contains. But there is a bug in here. So we're gonna try to find what that is. So we're gonna type a word, Mississippi and then it says there are 11 vowels in it. That is not correct. Let's run it again. A-B-C. And it says that there is three vowels. So what's the problem here? And actually what's happening in this code? So we know this, we're taking a word from the user. And then we initialize a new variable called num vowels and it's outside of this for loop, and so we're going to increase it within the for loop every time we find a vowel. So now we do the for loop. We look at each letter inside of the word, and check if that letter is in the word, then increase num vowels by one. I'm saying if the letter is inside the word. But we know the letter is inside the word, because we're looping over it, so we want this to be a different thing. So this is actually giving us the length of the word and not the number of vowels. So how do we do that? Well, just like using in for strings, to see if a character is inside of a string, we can see if something is inside of a list. Or actually, we don't even have to do that. So there's a few ways we can do this. So we can even just have a word, a string that has the vowels in it. A-E-I-O-U. And, let's run this now. A-B-C. There's one vowel, that's great. Mississippi. One, two, three, four I's. That's cool. We could also do this by having a list. So, this is using more characters. But it still works. Abracadabra. There are five, one, two, three, four, five. That's great. And then let's see what happens if we type my name. It's saying that there are three vowels, but I count one, two, three, four. So it's not quite right, because I'm only checking for lowercase, and Python is case sensitive, but what I can do is just change this to a lowercase string, so that I can compare with only lowercase vowels. And now it counts four. So back to analogy time. What is the difference between while loops and for loops? Well, if you imagine you have a big bunch of cookie dough. You can use a while loop to say while I still have cookie dough left, you know, take a scoop, put it on a pan, and then keep going until I'm out. And, so you don't know how long that's going to take. But a for loop is like if you're baking muffins, and you've got a muffin tray that has a bunch of spots, and you say for each of the spots in this muffin tin, add some batter to it. Let's look at a slightly harder problem, and we're going to deal some cards, and here we're going to like, use for loops and lists. So go to problem nine, card hand. And we want to deal a list of four, a hand of five cards. So we've got some suits, and we got some values, and then we want to fill these lists out with each of the possible cards, and a hand of five cards that are randomly chosen. So for the first one, we want to take each of these suits, and each of these values, and smoosh them together to create a card value. So I can actually have like, let's see. I can say for suit in suits, and so each time it loops around, this suit value is going to change, so it'll loop four times. And then we've got some values, and we want to add, we want to start with spades and a spade to each one of these. So we actually want to do a second loop. So we're going to have two loops here. For value in values. And now we can do something with that. Let's do cards, append, we'll just add it on to the end, and then, something. These are all strings, so we can just do straight up concatenation and put them together. So, suit plus value. So now I'm going to print what these cards are, just to make sure that it's right. Great, and so we've got spade, ace, spade, one, et cetera, until King, and then now we're at clubs. And let's print out the links, so we can see how many cards we have, and it's 52. So that's good. So if this, if you're finding this a little hard to wrap your head around, try re-naming some of these values, and seeing if you can get it to work still. Try working with a smaller set of numbers in these lists, or values in these lists. Or even, you can write them down on little scraps of paper, and tear up those papers, and go like okay, first this value is going to be this, and then it'll do this. These are some different tricks you can do to understand the concept. And then, we want to deal a hand of five cards. So now that we have a big list of cards, we want to get five, randomly from there. So let's use this random module again, random library, and we're going to say, let's pick up a random card from here. So random, dot, and there's actually, the first one is this choice. And I can call that and give it a list, and it should give me just a random item from that list. So card equals, and then print card. So I'll see what happens here. So I'm getting different cards each time. So that's great. We want to do this five times. So I'll use another for loop, so for number in range five, because we want it to happen five times. Get a random card. And cards, append, card. So we're going to add it to this list of cards. Or no, we're going to add it to this hand. So hand, dot, append, card. And then we can print the hand. So that's good. One thing that we haven't done yet though is, we don't want to be able to get the same card twice. So we're going to have to actually remove that card from the list of cards. So we can do that by saying cards, dot, remove card. And that's better. So we're using two different for loops in here. And I'm going to quickly show you now, going back to challenge number two, the number guessing game. Let's try to use a for loop instead of a while loop. So what changes would we have to make? We can say for, num, guess, in and then we can actually just say range of num guesses, because we want to do this a certain number of times. And we don't have to change num guesses any more. Because actually, that would maybe screw it up. No, it wouldn't. But we just don't need it anymore. So that's it. Hey and that still works. So we were able to just change the while loop line into a for, and then delete one line of code, so it is a little bit simpler now. But they're both good.