 Welcome back everyone. This is Brian. In this video, we're going to talk about the for loop and the range function. The difference from the for loop compared to the while loop, which we talked about in the previous video, is the for loop has a beginning and an end and the range function helps us create that beginning and end. Really helps us to just avoid that nasty business of infinite loops. Let's dive in and take a look. All right. First things first, let's go ahead and look at the for loop on list tuples and sets. This is super, super ridiculously simple. And I'm just going to make a simple list. And we're going to say for I in notice what we're doing here, though, we're saying for, and then we're creating a variable. And this variable is named I, you can name it x, z, zebra, whatever you wanted to name it. I'm just gonna say for I in. And now we have to give it some sort of container. Let's say x, and then the colon. This reads just like a while loop, but we are now creating the variable in the statement itself. What we can do now is just simply say print. And let's say I, I'm going to print out the value of I. Go ahead, run this. You see I is one, two, three, four, and it stops. We don't have to tell it to break or to return or to do anything like that. And we don't have to increment some sort of counter. It does it for us under the hood based off the you guessed it, the length of this list. So it's taking this iterable container. And it's saying each one of these is now a separate variable. And we're going to treat it independently. And you can do this ridiculously fast. And you can do it across different types of things here. So for example, think I made one too many. We're going to say list, a tuple, and a set. And we're just going to say, this is L. This is T. And this is X. So we've got a bunch of different things here. And let's actually one of the little nuances of this is you got to have it in the right style bracket here, or this is the parentheses. And the set has a little weird squiggly thing. There we go. And we can just switch this out. So I'm going to do the list. Works as expected. Let's switch this out to the tuple. Works as expected. And I know you're already probably rolling your eyes going, we know, we know the set's going to work just fine. And it does everything works as advertised. This makes it just ridiculously easy to loop through a container. Let's take a look at the for loop in regards to dictionaries, we're going to treat this a little bit differently here. So I'm going to make a dictionary. And I'm going to say dict. And let's just go ahead and build this out. Brian, I'm going to say my age is 46. Say Tammy. Don't tell her I put this in the video, she'll get very mad, but she's 48. And then Heather, let's go ahead and say Heather is 28. And Chris, let's just say Chris is 30. We could add more if we really wanted to. So we have our beautiful dictionary here. Go ahead and print this out just so we can see what it looks like. And it creates a dictionary for us. Now we're going to work with this in regards to the looping. When I say the looping, there's a couple of different ways we can do this. So the first way, we're going to say 4k in x dot ease. I gotta be a little careful here. If you forget to call it as a function, and we haven't really covered functions yet, but trust me on this, if you just do this, you're going to have a bad time. And let's actually see it fail just to prove that hey, it is going to fail. And we're going to say ease a equals. And then we're going to fill that out later. But let's just watch this thing fail. See, built in function or method object is not iterable. What what is going on here? What does this mean? plain English, built in function or method? Well, we haven't really covered these yet. But basically, it's a chunk of code that we can call. And to do that, you need these parentheses. So if you forget those, you can have a bad time. This is object is not iterable. What does that mean? Well, it needs to be a container of some kind, for example, a list tuple set or a dictionary. So simply by saying, Hey, use those parameters, it's now going to return some sort of data that is iterable, which we can go through and work with. All right, let's continue on. Let's say x. And we want to get that specific key. And I'm going to just assume you watch the previous videos, but just in case you skipped them. Let's slow this down just a smidge here. Or six first, we need to fix our little error there. So what's going on here is we are creating our dictionary. The dictionary is just a key value pair, key value, key value, key value. And you can see that in the dictionary right here, where it's created those key value pairs. Now we're going to say for each K or each key in our dictionaries, keys, it's going to return back some sort of list, we're going to say for each one of those, get the value. So we're saying x, okay. So really what we're saying here is x is a dictionary, k is the key. So we're saying for whatever key, for example, if it was Tammy, it's going to say print out Tammy, because that's the key, and then print out the actual value here, which is that right there. So each key is going to correlate to values, what I'm trying to really drive home here, maybe I explain that badly. So keys, and then you get your key value, key value, key value. There is another way of doing it. And I wanted to bring this up simply because you're going to see people do it both ways. So we're going to say for K comma V, this is going to blow your mind if you're coming from another language, we're going to say two different variables here, key and value in, we want to switch this out to items. And again, we're calling a function. So we need those parentheses there, if we forget those, we're gonna have a bad, bad time. And because we are now pulling these are unpacking those, we don't have to do this little bit here, we can just simply swap that out with our variable. So what items is doing is, well, it's actually pulling each value pair out and returning it. So now it can unpack the key and the value. So you have two different variables. See that in action here, let's save and run. And actually, let's call this items. So we don't get that confused with the keys. And sure enough, items and it is unpacked them correctly. Brian 46, Tammy 48, Heather 28, Chris 30. So which way's better? Well, it depends on what you want to do. Personally, I tend to do this method right here. That way, I don't have to mess around with all these little brackets. Remembering what does what, but each way is perfectly fine. Let's talk about range. And we've touched on it briefly, but let's really look at range. So let's kind of quick review here, we're going to say x equals and we want a range of five. And if we print this out, this is where you're going to get really confused really, really fast. So let's go ahead and run this. And here's our x right here, range, zero to five. Wait, what? You're thinking this is going to return a list or something like that. Actually, it's going to return a function call. So there's range zero to five. What is this zero to five? Where did this come from? We said five. Well, we're going to look at that in the next little segment here. But just understand what we're really doing is we're saying x is really going to equal a call to that function. We're going to cover functions in depth in other videos. It's actually a large topic. So we're going to split it into multiple videos. Just understand what's going on here. You're not actually getting some sort of iterable container that you can go through. But what we can do here now is something like this. For I in x, notice how I don't have these parentheses, because we are not calling a function directly, we're calling a variable. You try to do something like this, you're gonna have a very bad time, just understand the difference between those. And then from here, just some becomes really ridiculous, so simple. This is what I love about Python, everything just becomes very simple to the point I'm almost embarrassed to just even talk about it. Because to me, because I'm an old programmer, it seems like just common sense. And there we go, range zero through four. So what's happening under the hood here, is it saying for I, so we're creating a variable in x, and this variable is actually pointing to a function. So it's making this function call getting that value back, and then it's going through each item. Now, if that last little bit hurt your brain, fasten your seatbelt range can get a little complex here. So we have a start a stop and a step. What does this really mean? Let's go ahead and do this. Let's say x equals range. And I'm just going to leave it here for now. So we can read this and see what's going on. We have stop int. So that's the default. But there's different ways of calling this. You can do a start, which means the number you're going to start at a stop, which means the number you're going to stop at, and a step, meaning how many numbers in between those two you want to jump. Let's take a look at this and see what it really looks like under the hood. So let's say we want to start at five, we want to stop when we hit 20. And we want to take three steps at a time. All right, pop quiz, what is this going to look like? Let's go ahead and print this out. If you thought it was going to say range five, 23, you are absolutely right because beginning to understand here that this is pointing to a function, not some sort of value. But we can actually work with it now that we've assigned it to a variable called x. Go ahead and say for I in x. Remember, we're calling a variable not a function. Go ahead and print. Okay, bonus round. Who knows what this is going to print out here. Let's see. Starts at five. And then it goes eight, 11, 14, 17, and it stops at 20. Notice how it didn't fire off a 20. If we said 21, let's see what happens. Now suddenly includes that. So that's the major takeaway from here is it's not going to include that stop, it's going to stop when it gets to there. Remember under the hood, this is using some sort of loop, it's saying probably something that we've seen before like this, while, and then acts as less than 20. So when it gets to this 20 is just going to break out of that loop and stop doing it. Once you understand how this works, it makes life ridiculously simple. Something like this would require a little bit of math and other languages, but Python as usual, makes it nice and easy. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on you to me.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.