 Hey everybody, this is Brian, and this is the sixth Python tutorial. Just had an earthquake. It's kind of weird because I live in Michigan, and we really don't have earthquakes here. I think that's the second one I felt my whole life. Kind of weird, so. Anyways, I was right in the middle of doing a video when it happened, so I have to start over, which bothers me. But, so today we're going to discuss loops. Before we dive into loops, I want to revisit scope. Let's actually just make a list here, and we're going to make a blank list. And we're going to say, 4, i, n, range, and I'm going to say 10. The range command just makes a range of numbers, and you're going to x, append. We've done this before, where we're adding to a list, and we're going to print x. And I'm going to just run this, just so you can see what goes on. We're making a list, and each time it jumps into the loop, it's expanding the list by adding. See? But before we really discuss it, I want to discuss scope. Because the last tutorial, we said this would be scope 1. Let me actually comment that out there. Which means this would be scope 2, and this would be scope 3. Now I know some of you are actually sitting there going, wait a minute. Scope 1 and scope 3 are actually the same scope. Well, you're right. You're absolutely right. I wanted to show the steps 1, 2, 3 for illustrative purposes, but the reality is they're the exact same scope. There are some gotchas, however, you need to be aware of. Like, let's say you want to print a variable called name. And, you know, because it's the same scope, you want to do it down here. You're going to say, name equals Brian. If you try to run this, it says name not defined. You can see the little red squiggly line here. So you have to define your variables before they can be used. See? There you go. And that prints it out. And because it's in the same scope, you can actually, let's actually put it under there so you can see. You can see it still works. Now let's actually take this and move this into the second scope here. Notice how it still works, even though it's not in the same scope. The reason for that is twofold. First, we've set the variable, or we've declared it, I should say, before we've used it, so that works. And scope 2 is a subscope of scope 1, meaning scope 2 actually lives inside of scope 1. So anything inside scope 2 is accessible to scope 1. Same rule still applies, though. You have to declare the variable before you can use it. See? Fails. So I really wanted to clear any misconceptions you might have about scope before we really deep dive into looping. All right. So what is looping? Looping is like having a conversation with a six-year-old child that keeps asking, why? Oh, why is this guy blue? Why this? Why that? Why? Why? Why? Or an old person? Well, what's this? What's that? What's this? What's that? Not trying to disrespect young children or old people, but it's an example of a real-life loop that you've had to deal with. A loop is just a segment or a scope of code that's going to repeat itself. 4i in range 10, meaning we're saying from 0 to 9 because it's going to make a zero-based index, you're going to append x.append the current number. This is called an iteration. Whenever a loop jumps back to the beginning, it's an iteration. In some languages, they actually used to have go-to statements, but in Python, simple iteration. So let's run this, and you can see the iterations. We have 10 iterations, and with each one, our list is growing because we're appending to it. Pretty neat, huh? And if you wanted to not see the iterations, but just see the finished product, you would just take the print statement and throw it back into the first scope. And there it is in all its glory, our finished list. I'm going to actually bump that back into the subscope. So that's a 4 loop. A 4 is very handy for going through a range or a list or a tuple or even a dictionary. Now we're going to do a little bit of fun here. We're going to say 4i in x, meaning for each object in our list, we're going to print index is percent d, and we're going to percent d x, and we're going to say i minus 1. The index is 9, 1, da, da, da, da, da. What that does is we're going through our list that we built here, and we're saying the current index is. Now notice how it's not sorted. It's just whatever random order. So sometimes you may actually want to sort that and make sure. Now I shouldn't really call that an index, it's a position within the iteration. It's a little bit different, but it does kind of highlight that this is a zero based array. For example, if you say i and try running this, now it actually works the way you'd expect it. Index is 0 through 9, and let's say i minus 12, just to see what happens here. Notice how we're out of range. Out of range is a very common error for looping. Really what you need to understand with out of range is you've gone out of the bounds. There is no i minus 12 index. Now when you say i minus 1, what's going on here is another common misconception. Because it's a zero based array, you think, oh I got to get the position minus 0. No, this is an iteration. The iteration is automatically going to start at 0, which is why this works correctly. So those are some gotchas if you come from other languages. So what we've learned so far is that the for loop will take an array or a list or a tuple or even a dictionary, and you can iterate through them. And that's what we've really gone through here. If that's not making sense at this point, think of a for loop as a repetitive task. You're a mailman and you have to deliver mail to 100 homes. So for i in range 100, 100 homes, deliver mail. That's what you're really doing. And let's actually do a dictionary. Why not? And we're going to actually have to make a dictionary. We're going to call it ages. And that's one thing I really like about Python is once you've done it once, it just sticks. It's really hard to describe. You just kind of remember Python, unlike most other languages, is small enough to actually fit in your head, meaning you're not constantly looking things up. So we're going to, let's actually space that out so we can see here. We're going to print. We'll say, whoopsie, percent s is percent d years old. Maybe, there we go. And we're going to say name age. So what we do is we've got our dictionary. And now we're saying for name and age in ages.items, which makes an iteration list. We're going to run this. Brian's 40. Heather's 22. Now you see the real power of four loops. You can take a complex data structure such as a dictionary and make it do something meaningful. And now we're going to do the while loop. Now the bonus question for anybody out there wanting to score extra credit. What's the difference between a while and a four? Anybody? Anybody? Four will always execute while may not execute. Maybe if I can spell this. We've got a lot going on tonight. It's prom night. We just had an earthquake. I've got a couple new video games. I just passed my PMP exam. So it's been busy. While true. What's that going to do if we run this? This is another common mistake I see people making in every programming language, not just Python. While true. Well, that variable, because that is a variable, is never going to change. So it's just going to keep going. You've just created an infinite loop. See, it just says too much output to process. And it's still running. I have to stop it. We have to interrupt it. And it just, I mean, there's thousands and thousands and thousands and thousands and thousands of these things. So if we'll say n plus equal one, we're going to increment n. And we're going to actually just print out n. Let's see what happens here. It's got to be on the same scope. There we go. And you see how that number just keeps going up and up and up and up. Eventually this program will crash because we're incrementing and it'll just go out of, it'll go, it'll, what's the, oh yeah, it's a range check error that'll end up happening. Basically it goes into data types. Certain data types can only hold so much information and eventually it'll get so large it'll just stop. Or it may actually may be wrong because in some languages it'll actually revert back to zero and start over again. So we can say while n is greater than 10. What's going to happen here? Well, when we run this, nothing happens because n is already zero. So this already is false. That's the difference between a four and a while. A four is always going to have that range that it's beginning with. Where a while will evaluate the expression and may not actually execute. So let's actually just go back to true. Our example of an infinite loop here. And we're going to actually do some processing here. And an infinite loop, in case you're wondering why you would want that, it's actually quite common if you're working with like say a network connection or a file or something and you want to monitor the bytes in. You just want to loop indefinitely until you have some sort of breaking point. That's what we're going to talk about next. So if is greater than or equal to 10, then we're going to break. Break means it jumps out of the current loop. It just literally stops and says, Nope, I'm done. And we're going to actually execute that. And we're going to go print finished. Wow, I cannot spell finished looping. Let's run this. And now you see it says finished looping. And let's actually just print this out here. We're going to be a little crazy actually. We're going to say, if n equals six, whoops, print six is awesome. Otherwise, actually let's just continue. And I'll explain what continue does in just a second. All right, so here's our program. We're going to run this. You can see how it says, one, two, three, four, five, six, seven, eight, nine. But it says six is awesome. And we finished looping. So this is the while loop in all its glory. We're incrementing our n. And then we're saying, if n is greater than 10, we're going to break, which means it's going to break right out of the scope, right out of this loop. So it's going to go all this. It's going to say, I'm done with you. I'm no longer iterating. And it's going to jump here to finished looping. If, however, it's six, we're going to say, Six is awesome. And we're going to continue. Notice how it doesn't just print out the number six. What continue does is it jumps back to the beginning of the iteration. It just jumps right back. So anything down here will not get processed once that continue statements there. Very key concepts you should know are break and continue. That's how you break and continue out of loops. Break will exit the loop. The continue will just start at the next iteration or the next step. That was a mouthful. And we've learned quite a bit. Some things you should bear in mind is range checking. I mentioned earlier that if we just kept incrementing this, eventually it would crash. Always check your ranges. So if you have a defined range, like a list or dictionary tuple, you should almost always use the for loop because it does the automatic range checking for you. You don't have to say if it's greater than the list count, exit, it's all done for you. All right. Well, that's it for this tutorial. I hope you found this educational and entertaining. Be sure to visit my website, voidromes.com. I've got the source code for this and other tutorials out there. And be sure to join the voidromes Facebook group. We have, I think we're just about hit the 200-some-odd mark of developers in there from all different languages, not just Python. So if you've got like a Java or C++ question or even like a Ruby or a Scala or something like that, there's people in there that know this stuff and they can help.