 Welcome back everyone. Today we're going to talk about loops in Python. So at the top of my screen I have just a notepad open. It'd be something like any text editor you can use for this. And in the bottom of my screen I have a terminal opened or a command prompt. And then if we look at the files we have so far from our first videos we had these Python scripts. Last time we talked about conditions and if statements. Every file here, every script file ends with a .py file extension and that just tells us that these are Python scripts. So today we're talking about loops and I'll be using the script. So in the top my Python script so far I already have one variable x and I'm saying x equals zero. So we've already covered setting variables before. And what we want to do today is something called looping. So sometimes whenever you are running a program you want to run some command over and over and over again. Usually it's for checking or counting or doing all sorts of different things that we can use the same code multiple times and the way that we can easily use the same code multiple times is by doing loops. So it'll make a lot more sense in a second. So first there's a couple different types of loops in Python and one of the most common that you'll use is called a while loop. So just like you would say while I'm doing something or while this is true I want to do something. So while it is raining I want to carry an umbrella. So in Python it would be the same thing. So while and then if you think back to the last video we were talking about conditional statements or checking statements. So we have some condition that comes after this. So we can say for example while x, remember x is an integer starts at zero. So while x let's say is less than 10 we want to do something. So this is very similar to the structure of the if statements that we looked at last time. So instead of an if statement we just say while and we have some conditional statement that we check for x less than 10 and right now x is zero. So this condition is false. Right, sorry it's true. So while x is less than 10 right now this is true. So since this is true we will run whatever code is under here. So just like the if statement we go down and then you want two spaces over or to space over a little bit. And then we'll just print x. And I'm going to put print in brackets this time. So before I printed x like just print x and you can also print in brackets and this is actually the correct way to do it. Okay. So here we have while x is less than 10 print x. Well this is true right now because x equals zero. So we will print x and then it will keep going around and printing right. So if we ran this right now. So I'm going to run python loops.py and then what we'll get is zero and it just keeps going. So this is actually the program running right now and it keeps running over and over again. Okay. So I actually have to stop that early. And the reason that it keeps running over and over again is because x doesn't change the value of x just stays at zero. So x is less than 10 forever and that's called an infinite loop and the loop will never stop. Sometimes we want an infinite loop because we want our program to always keep running and keep checking for something. But most of the time you do not want infinite loops and if your program doesn't exit properly most likely it's an infinite loop problem. So in this case what we have to do is print x and then we need to increment x before we go back and check again. So here I'm going to do increment and I'm going to use a special way to increment. So here I'm saying x and then plus equals one. So normally if we're assigning a value to x we would just use the equal sign or if I was going to just do some basic math I can do x plus one like we did last time. So x equals x plus one. So we could just do this but a shorthand of this is to say x plus equals one. And what this means is add one to x to the value of x. So it's just a shorter way to say it. So this is increment. So now what we're doing is checking while x is less than 10 so this is true. So we're going to print x and then add one to x. Now it's going to go back up and check again. While x is less than 10 well we've only add one to x so now x is equal to one it's less than 10 that's true so we print again add one and then we just keep doing that over and over again until x is less than 10. So let's see what happens whenever we run that. So we started at zero so x is zero and then we counted up to nine. Well why did we only count to nine right? So we were checking until x is less than 10 and what happened is we incremented until we got to nine we printed nine because nine is less than 10 and then we incremented to 10 and then we do another check then x was 10 x no 10 is not less than 10 it's equal to 10 right so it just exit at that point so whenever this becomes false then the while loop exits and that's what we had here we counted from zero to nine. If you wanted to include 10 then just like the if statements we saw last week you can do less than equal to 10 so now this will count from the first value of x to less than or equal to 10 okay so let's count that again and you see that we count up to 10 okay so that's that's pretty much it for while loops you can do while any check is true so you could even just write true okay and this would run forever as well because it's always going to be true if you wanted to actually run forever but in this case just consider that we have some statement that we are checking and while this statement is true then we will run all of the code in here and then keep running it over and over again okay whenever this statement becomes false then the loop will stop and it will jump down to the next line so um we have another thing let's say print I'm outside of the loop okay so now what we're going to do here is finish this loop and then this print statement is outside of the loop so after the loop is finished we will see this print statement okay now what happened is we printed uh basically 10 times 11 times and then once uh this was false then we jumped down to print I'm outside of the loop so then the rest of the program continues on this loop finished this block of code finishes but the rest of the code in the program will continue to run okay um right so the next thing I want to um do so before I go to that um notice that I put a space here so a blank space what this blank space says after the loop is that the loop is finished so if I put this too close to each other then um uh python might misunderstand this print statement as being part of all of this loop code so yeah it still worked here but sometimes python doesn't like everything being close so I usually just add a space there to say kind of delineate this is where my loop ends or this is where my block of code ends and then we can start another block of code down here okay so just be aware about spacings keep one space after some block of code okay um right so just like if statements that we talked about last week you can also have an else statement on a loop so here we have while and then we have some statement so while this is true do something but you can also have a else so then um I'm gonna print finished now this l statement is attached to the loop so it usually runs whenever the loop is finished so I'm gonna say finished okay so now we have some code while x is less than or equal to 10 I'm gonna move that again well x is less than or equal to 10 print x and then increment one so while this is true keep running print x and increment print x and increment and then whenever this becomes false then run all of the code in this else statement which is just print finished that means that the loop is finished okay now these this block of code is all one block of code they're all related to each other um and then we have this empty space and then we have print I'm outside of the loop so once this loop is finished then it will run all of the rest of the code in our script okay there you go so we counted 0 to 9 0 to 9 and then we said finished and then we ran the code I'm outside of the loop so that's the additional code okay so while loops are are pretty common as long as you can set up this this check properly normally we initiate some sort of variable at the beginning and then we do some sort of check on that and then we keep a counter inside the while loop but there's a lot of different ways you can do it and we'll talk more about them in as we go along okay so another type of loop that I want to talk about is a for loop this is used in a little bit of a different way while is just while something is true keep looping and whenever it becomes false stop looping a for loop is usually dealing with things in a list or in python I usually use it for things in lists or arrays we will talk about them later and then I'll use for loops to go over them today I'm going to be using something called a range but they are structured a little bit differently so we have for example for and I'm going to say y in range okay for y in range 20 to 30 so notice for the while loop I had to create my variable or set my variables before x equals 0 and then I could use x equals 0 in my loop in a for statement in a for loop you're creating a variable so I created a new variable called y and it's in the range 20 to 30 okay now let's add just like before we'll add some code underneath this so let's do just print y print y and I don't have to do anything else I'm going to remove that space notice we still have this code outside of the loop so I'm just going to keep that in there so we have for y this is a variable in and then we have a function which we'll also talk about later in the range of 20 to 30 and what this function does is just calculates that range or counts in that range so basically what this function will create is first it will write 20 and then 21 and then 22 and then 23 etc okay so that's what this code remember I'm putting a hash sign or a pound sign and then this text so this is a comment this is not part of my code it's just a note for us okay so a range will basically count within the range that you provide it so this is going to give us the numbers 20 all the numbers between 20 and 30 okay now in terms of the loop what happens is the range will generate the number 20 and then it will put the number 20 in y and then it we will use 20 in the variable assigned to the variable and then do something with it and then the next time we come around for in our for loop it'll be 21 and y will become the value of 21 and then we'll do something with that value okay so it's probably easier if I just run it and show it okay so here we have for y in the range 20 to 30 so and then all we're doing is printing the variable y and what if we run the code we just get 20 to 30 and then we get the code I'm outside of the loop okay I'm going to remove this just to make it a little bit easier to see um so that's all we're doing here is um we don't need any variables created before we can just have this for loop we create our own variable and we can name this anything let's call it um I don't know okay so this is just a variable we can use any name we want and what will happen is it will count the range between 20 and 30 okay and then we're printing taco so the difference between a while loop and a for loop is that we're dealing with different things in a group so in this case we have a group of numbers and that's the range if you gave it a string um let's say banana then we have a group of characters and it should loop over that yeah okay so um we're four loops loop over groups of things and they those groups can be anything and we'll talk more about the different types of data that you can give it um later okay let's stick with uh integers for now okay so we have four y in range and then print y we don't have to do anything with calculations or anything it will just take whatever this value is whatever the current value is and then assign it to y and then we'll do something with it okay now sometimes like you don't always want to um uh let's say just print out everything maybe we want to do something with this data so we can do a couple different things um for example the most common is to use our conditional statements our conditional checks so just like we talked about last week we can do something like if um let's say y is 27 if y equals equals 27 and that means if y is exactly 27 and then the two dots mean do something uh what are we going to do uh let's just print uh the text y is 27 okay so we're doing a check inside the loop notice I've added additional um spaces because this print statement is under the if statement not under the for loop this is called a nested statement so here we have our for statement print is inside four if is inside four and then technically this print statement is inside the if block okay um so just be aware of your spacings okay now what should happen is whenever we're running through we will print y and we will print 27 and then we should see text under here that says y is 27 yep y is 27 okay so we can do all sorts of different conditional checks maybe we want to know um if y is greater than 27 or we want to know of um some character let's say the banana uh text if something is an a we want to say uh we found a vowel or something like that okay so you can do any checks you want okay but sometimes um you might want to get to a certain point and then stop so we can say let's say that we only want to go to 27 and then we actually want the loop to stop we don't want to run the loop anymore well if you want to get out of the loop then you can do something called break okay and what break does is jumps out of the loop and completely um uh doesn't keep going so basically we are testing here if y is 27 we print that y is 27 and then we break which means the loop will stop what will happen is we will count up to 27 print out y is 27 and then we will not print out 28 and 29 because we've broken the loop okay now I need to add some code back this code is outside of the loop I'll just say outside okay now this code is not related to the loop at all and what should happen is once we break out of this loop then we will print outside okay or once the loop exits and we print outside yep so here we counted from 20 to 27 we said y is 27 and then we broke the loop so we did not count 28 29 30 and then we print outside and this is the code so the the program continues on but the loop stops the loop breaks okay um and then we can also do uh one other interesting thing with loops there's a lot of interesting things to do with them but one other interesting thing we'll show here so maybe I don't want to print 24 right so we have our print statement here and this always runs as long as um this is true as long as for y and range is is true then we will get this print y so I'm going to add an additional if statement on top of here if y equals uh what did I say 24 if y equals 24 and then I don't want to print 24 I don't want to run any of this code from print to break I don't want any of this code to run if y is 24 I want to skip everything else so what I can do is if y equals 24 and then I can just do continue okay and continue basically skips all of the code after continue and restarts the loop again so the loop y will go to the next number it will skip to 25 um and then start running all of the code and doing all the checks again so continue will keep the loop going but it will skip everything after it break will just stop the loop where it's at right so whatever your condition is as you're going along break will stop the loop continue will keep the loop going but skip over anything after it so I probably use continue more than I use break because sometimes we don't want to run a bunch of additional code if we know that it's going to be worthless to check it so imagine that you had a lot of code running after some check well if you can just check something very quickly and you know that none of that code is going to run if you skip all of that code your program will work much faster okay but don't really worry about that too much just think about in a for loop or in a loop you can use continue to keep the loop going but skip some code and you can use break to get out of the code so um remember earlier I said our while loop so I can just do while true right um eventually I would have to use a break to get out of that code okay I would have to have some some observation that I'm making here maybe I have a sensor or something like that so I would read the temperature of a sensor if the temperature is greater than 40 degrees okay if it's greater than 40 then break right so what this would do is keep checking the temperature all the time until the temperature is greater than 40 degrees and then it would break from that so this is one case where you might use a while true an infinite loop to check temperature and then stop running whenever temperature gets too hot for example okay so there's a lot of interesting things you can do with loops and by writing a loop and then combining a little bit of logic in your conditional statements like if y is 24 or whatever then we can control how different things go oh I didn't run this okay so if y equals 24 continue so whenever I run this code what will happen is you'll have 20 21 22 23 we'll get to 24 and then we will skip the print statement and this if check so 24 will not print and then we'll get 25 26 27 and then y is 27 and go outside that's what should happen okay so let's see if that's true yep so now 24 is gone because I'm continuing which means I don't even care about this code I'm going back to this for loop okay so there's a lot more you can do with loops and we will cover them later really loops are where the power comes in whenever you combine loops native statements that's kind of the bread and butter of all programming everything gets a little bit let's say code dependence from here depending on how the different code works but most programming languages you're looking at setting variables you're looking at if statements or decision checking and loops so if you can get a good idea of those concepts you should be on your way to becoming a good programmer so I hope that was interesting for you thank you very much