 Hi everyone a quick refresher on while loops and for loops in Python while loops and for loops are used to Do iteration which is the process of repeatedly performing some sort of operation? And it's important to know where these two styles of loops are most appropriate They are interchangeable and you can do the same thing with both of them But one is just built for they're built for different things the while loop is built for when you need to loop Until a condition is met. So you want to do things over and over until Something tells you to stop so for example in this This little while loop here. We're going to loop until x is until this condition Here which we call the loop condition until this thing is false So the while loop this block of code keeps going until this becomes false, right? So when we start out with x gets to X is to which is less than 10. So it proceeds in the loop x gets x times 2 So x will take on the value for We'll print out that for will come back up to the beginning of the loop is for less than 10. Yes double the size of x print it out 8 8 less than 10. Yes, okay double it to 16 print it again 16 less than 10. No, we stop immediately and kind of go to whatever's down here That's a while loop looping until whatever the loop condition is met The other style is for loops for loops are more appropriate for when you have a List or a sequence which and strings are sequences. So are tuples and some other things and you want to do something To each item in that list Colloquially we would call that a for each loop, right? So you want to do something to every item in a list or when you want to do something a Certain number of times. Okay, so that's kind of the distinction between when you want to use a while loop when you want to use a for loop I'm gonna switch over to the code and Just illustrate a couple of other things. Let me grab this code here I'm gonna switch over to pie charm in a file that I'm working in and paste in this code, right? So if we run this We can see the output down here prints out for Because it prints after doubling it for 8 16 it prints And then it checks that condition and then it's done. In fact, let me just add a little statement here. It says Done okay so One note about while loops This stop condition What Python is doing is it's checking is this true if it is true do this stuff if it's false Stop and go down here. Okay, this condition can be complex We can do more with it, right? So let me change this to while X is less than a thousand just for the heck of it Now this thing's gonna print a lot more right multiplying multiplying multiplying But we can make this condition complex. In other words, we can have more than one condition so let me have another condition where I've got y is equal to five and Inside my loop what I'm gonna do is subtract one from five Every time I go through the loop, so we'll go five four three two one and Beyond actually at this point but Now I'm gonna include why in my condition, right? So now I've got this kind of complex condition here if it makes you feel comfortable You can put some parentheses around this you don't need them And now so basically what this loop is gonna do is it's gonna loop until this thing is As long as this thing is X is less than a thousand and Y is greater than zero another way of looking at that is This loop will stop When X is greater than equal to zero one thousand or Y is less than or equal to zero, okay So why is going down by one every time so this ought to go regardless of how big I make this number Only five times one two three four five because the Y side of this is evaluating It's coming up false before the X side of it is as well. Okay, so One more thing. Let me switch back over here Let me grab the four loops out of my code and I'll paste them in and You can stop here and pause the video copy this stuff in if you need it Couple one little trick I want to show you here. So if we run this Still getting my while loop output, let me comment this out So it's not printing my while loops anymore Let me print out the first for loop So dogs is a list of strings chow poodle corgi for I In dogs print I what's happening for this is it does for each element in This list of dogs I will take on the value of the first item the second item the third item Hey print it out Right So that's what's going on there. I can also do this Another way which if you're coming from Java land Yeah, say hi to my wife in the background if you're coming from Java land This is a loop where you're kind of looping over a number here I've saying hey for I in the range Up to length of dogs. Oh my gosh. What's length of dogs? Well, the length is three Okay, the value of range of three. Let's print that out is the range from zero to three Exclusive of the three So when I print out I here I is going to take on the value zero one two and Then I'm just going on here something a little bizarro having Zero zero a thing I in the dogs list is the chow Whoo playing connect the dots here the first indexed item is poodle The second indexed item is corgi, right? So, you know, this is looping over things using Indices of list sometimes this is useful. Sometimes it's not It may come up again, right? But just be aware of that there are a couple of different modes for looping using for loops Know what for loops versus while loops are good for I want to show you one more trick here All right for I in dogs chow poody poodle corgi You see that this goes left to right it goes in sequence from zero one two Hey, we can do it backwards with a little magic, right? If we do this for I in reversed Dogs what reverse does is it's going to give me back a list in reverse order? So instead of chow poodle corgi. I should get corgi poodle Corgi poodle chow Kind of neat. Sometimes you want to go backwards. There are benefits to that There's another way to do it. That's even more cool looking and show your friends this say oh your elite hacker, right? Just a little syntactic sugar Who what in the world is that? Well, it has the same effect corgi poodle chow going backward This is a slice. This is called a slice of the list I don't expect you to know it, but if you want to impress your friends use this It's exactly the same thing as Reversed okay, that's it for while in four loops brush up on them from your 131 notes We will be looping a lot in this class. You got to be comfortable with them