Skip to playerSkip to main content
  • 3 months ago
Transcript
00:00Hello, everybody. Today, we're going to be learning about for loops in Python.
00:04The for loop is used to iterate over a sequence, which could be a list, a tuple, an array, a string,
00:09or even a dictionary. Here's the list that we'll be working with throughout this video.
00:13And I have this little diagram right here, which kind of explains how a for loop works.
00:18The for loop is going to start by looking at the very first item in our sequence or our list,
00:23and that's going to be our one right here. It's going to ask, is this the last element in our
00:28list? And it is not. So it's going to go down to this body of the for loop. Now, we can have a
00:34thousand different things that can happen in the body of the for loop as we're about to look at in
00:38just a second. Then it's going to go up to the next element and ask, is this the last element reached?
00:44So it'll be no again, because we'll be going to the two and then the three and then the four and
00:48the five. Once it reaches the five, it'll go to the body of the for loop. And then when it asks if
00:54that's the last element, the answer would be yes, because it's iterated through all the items
00:58within the list. And then we would exit the loop and the for loop would be over. Now that may not
01:03have made perfect sense, but let's actually start writing out the syntax of a for loop so we can
01:08understand this better. To start our for loop, we're going to say for, and then we're going to give
01:12it a temporary variable for this for loop. So it's a variable as it iterates through these numbers,
01:18it's going to assign the variable to that number. So for this one, we're just going to say number,
01:23because it's pretty appropriate because these are all numbers. And then we're going to say in
01:28integers. Now, right here, you can put just about anything. This could be the list. This could be a
01:34tuple. This could be a string even, but that is what we're going to iterate through. So we're saying
01:39for the variables, each of these numbers within this list of integers, and then we're going to write
01:45a colon. This is the body of code that's going to actually be executed when we run through and
01:51iterate through our list. So for our first example, we're going to start off super simple. And all
01:56we're going to do is say print, open parentheses and say number. As it iterates through the one,
02:02two, three, four, and five, number becomes our variable that is going to be printed. So during
02:07that first loop, our one will be printed because that will be assigned right here. Then through the
02:13next iteration, the two will be assigned and it'll be put right here in each loop until the very end.
02:19So let's hit shift enter. And as you can see, it did exactly that. Now in this body, and I'll copy
02:26and paste this down here in this body, we really can do just about anything we want. We don't even
02:31have to use this variable number right here. We can just print. Yep. If we wanted to. And what it's
02:38going to do is for each iteration, all five of those, every time it loops through, it's going to print
02:44off. Yep. So let's hit shift enter and it printed it off for us. So really we weren't even using the
02:51numbers within the list. We were really just using it as almost a counter. Now let's copy this integers.
02:56Once again, let's go right up here and let's go copy this for loop that we wrote. Now we do not have
03:05to call this number. This can be anything you want, any variable name that you'd like to name it.
03:10We could call it jelly and we can do jelly plus jelly. I think you're getting the picture, right?
03:20When it loops through that one, it's doing one plus one. When it loops through the two, it's doing two
03:25plus two. That is basically how a for loop works. Now for a dictionary, it's going to handle it a little
03:31bit differently. So let's create a dictionary really quickly. So we'll say ice cream dictionary
03:38is equal to, we're going to do a squiggly brackets. So we're going to say name and we're going to say
03:44colon. We need to assign our value for that item. So we're going to say Alex Brieberg. We'll do our
03:51next one separated by a comma and we'll say weekly intake and I'll say five scoops per week. The next
03:59one we will do is favorite ice creams. And for this one, we're going to do something a little
04:05bit different. For this, we're going to have a list within this dictionary. So we'll say within our list
04:11of my favorite ice creams, we'll say mint chocolate chip and I'll just do MCC for that. And we'll separate
04:18that out by a comma and we'll say chocolate. So now we have this dictionary ice cream dict and within
04:25it, we have my name, my weekly intake and my favorite ice creams with a list in there as well.
04:31Let's hit shift enter. And now we're going to start writing our for loop. Now the for loop is going to
04:36look very similar, but to call a dictionary, it's just a little bit different. So we're going to say
04:41for the cream in ice underscore cream underscore dictionary dot values. And then we're going to do
04:50parentheses and then a colon. Now we're going to print the cream. So in order to indicate what we
04:57actually want to pull, we have to specify within the dictionary what we want. Are we pulling the
05:03item? Are we pulling the value? We need to specify this. So that's why we have this dot values right
05:08here. So let's run this and see what we get. So as you can see, we are pulling in the values right
05:13here. That's why we're pulling in Alex Freeberg five and mint chocolate chip slash chocolate. Now we are
05:20able to call both of those, both the key and the value. So let's go right down here and we can do
05:26both the key and the value. So we can pull two things at one time. And we're going to do this
05:32by saying dot items. So we could also do dot key if we just wanted to do a key, but we want to do
05:40items. We're going to do both of them. So we're going to go right down here and say for key and value
05:45in ice cream dictionary dot items print and let's write key and then we'll do a comma and then let's
05:52give it a little arrow or something like that, something like this, and then we'll do a comma
05:57and we'll say value. And let's print this off and see what we get. So it's looping through and for each
06:05key and value, it's saying here is the key. So that's the name. Then we have weekly intake. Then we
06:10have favorite ice creams. It's giving us a little arrow and then we're also printing off the value.
06:15So we have name, Alex Freeberg, weekly intake five, favorite ice creams, mint chocolate chip and
06:21chocolate. So now let's talk about nested for loops. We've looked at for loops. We understand
06:25how they work and why they do what they do. But what about a nested for loop, a for loop within a
06:31for loop? For this example, let's create two separate lists. Let's create flavors and let's make that a
06:38list by making it a bracket. And we'll do vanilla, the classic, chocolate, and then cookie dough.
06:50All great flavors. So that's our first list. And then we're going to say toppings and we'll do a
06:57bracket for that as well. And we'll say fudge and then we'll do Oreos and then we'll do marsh
07:08marshmallows. Is that how you spell marshmallows? I think it's an E. That looks wrong. I might be
07:15spelling it wrong, but that's okay. So let's save this by clicking shift enter. And now we have our
07:20flavors and our toppings. So now let's write our first for loops. We're going to say for one as in
07:26our number one for loop. We're going to say in flavors and we'll do a colon. We'll click enter.
07:33Now we can write our second for loop. So we're going to say for two in toppings and then we'll do a
07:40colon and enter. And then we're going to say print and we'll do an open parentheses. And then we're
07:46going to say one. So we're printing the one in flavors and then we're going to say one comma,
07:52I'm going to say topped with comma two. So what this is essentially going to do is we're going to say
08:01for one, we're going to take the very first one in flavors and then we're going to loop through all
08:07of two as well. So we're going to loop through hot fudge, Oreos and marshmallows. And once we print
08:14that off, then we will loop all the way back to flavors and look at the next iteration or the next
08:20sequence within the first for loop. So let's run this really quickly and see what we get.
08:25So as you can see, it goes vanilla, vanilla, vanilla, and vanilla is topped with the hot fudge,
08:32the Oreos and the marshmallows. And then we start iterating through our second one in our first
08:37for loop. So there's that hierarchy. So we're iterating completely through this one before
08:41we actually go to the very first for loop and start iterating through that one again. Now that
08:45is essentially how a nested for loop works. These nested for loops can get very complicated. In fact,
08:51for loops in general can get very complicated the more you add to it and the more you're wanting
08:56to do with it. But that is basically how a for loop and a nested for loop works. Thank you guys so much
09:01for watching. Be sure to like and subscribe below and I'll see you in the next video.

Recommended