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