While Loop | Python | Tutorial 20

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about while loops in Python now a while loop is basically a structure in Python which allows us to loop through and execute a block of code multiple times so I could specify like a few different lines of code and then I can put that code inside of a while loop and it would basically loop through that code executing it repeatedly until a certain condition was false so while loops can be awesome and there's a lot of situations in Python where we're gonna want to loop through specific lines of code so in this tutorial I just want to show you guys the like bare basics of how while loops work and then in future tutorials we're gonna use while loops to create little games and stuff like that so let's talk about while loops when I create my while loop the first thing I want to do actually is create an integer and basically creating a variable that's a number and you don't have to do this for a while but just for our demonstration I'm going to so I'm just gonna call this I and I'm gonna set it equal to 1 and now what I want to do is I want to create a while loop so I'm basically just gonna say wile and over here I want to specify a condition and this is what's called our loop condition or you can also refer to it as a loop guard and basically we are gonna keep looping through the code inside of the while loop as long as this condition is true so whatever condition I put in here as long as it's true we're gonna keep looping through the code inside the while loop so I'm just gonna say while I is less than or equal to 10 and now I'm gonna put a colon and I'm gonna make a new line and so anything that's below this while loop declaration and that's indented like this is going to be considered code that's inside the while loop and so that code is gonna get repeatedly executed while this condition up here is true so I'm just gonna put a very simple line of code here we're just going to print out the value of and then down here I'm gonna actually increment I so I'm going to add one to I so I'm going to say I is equal to I plus one so basically I'm taking this I value and I'm adding 1 to it there's actually a shorthand that we can use in Python to do something like this though I could just say I plus equals 1 and that's automatically going to add 1 to I so this is like a little shorthand and you'll see people in Python using that a lot so over here we basically have our while loop so I have the outline for a while loop so we're defining this variable up here and we're gonna keep looping through the code inside the while loop as long as this condition up here is true so if this condition is false for example if I is equal to 11 then we're not going to loop through this code anymore we're just gonna move on so down here I'm actually just gonna print done with loop so this will kind of illustrate this for us all right so let's go ahead and run this program I'm just gonna run it and you'll see down here we get some output so we're actually gonna get numbers printed out 1 all the way down through 10 and then we're printing out done with loop so basically what's happening is we're printing out every time we go through this loop so initially I is equal to 1 right the first thing that pythons gonna do when it gets to this while loop is it's gonna check this condition so before it does anything else it's gonna check this condition and it's gonna say okay is 1 less than or equal to 10 and that's true so as long as this guy is true we're going to go through and loop through all the code inside of here so we're gonna print out I which has the value of 1 and then we're gonna add 1 to I so now I is going to go from 1 to 2 now what we're gonna do is we're gonna go all the way back up here to this while loop declaration and we're gonna check the condition again so remember I checked the condition the first time and then I loop through all the code then I go back up to the top check it again so I'm gonna say okay is I less than or equal to 10 at this point I is equal to 2 so I is less than or equal to 10 so now we're gonna go through and we'll execute all this code again so I'm gonna print out I so it's gonna be 2 and I'm gonna add 1 to I so now we have 3 then python is gonna go all the way back up to the top and it's gonna check the condition again so on every iteration through the loop before python does what's inside of this loop it's gonna check that condition and as long as the condition remains true we're gonna keep executing the code inside the loop eventually though in this program I is gonna end up being 11 right so on the last iteration it's gonna end up being 11 we're gonna come back up here and we're gonna say hmm is 11 less than or equal to 10 nope and so sense it's nope we're gonna come down here and we'll print out this done with loop line so that's the basics of how these wire loops work we're specifying a loop condition as long as that conditions true we'll keep executing the code inside of the loop and after every execution of the loop we're going to come back up and check the condition again so that's how we end up with this printing out down here so that's the basics of a while loop and we can use these while loops to do all sorts of things like while loops are very very powerful hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying drop academy' and you want to help us grow head over to draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 27,717
Rating: 4.9806995 out of 5
Keywords: Programming
Id: Ghz4YwOXtTA
Channel Id: undefined
Length: 6min 7sec (367 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.