Python 3 programming tutorial: While Loop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Lo and welcome to my basic tutorial for Python 3 on the while loop the while loop is used to perform an operation while a condition is whatever you set right so the while loop is very much like the next loop the for loop and they're interchangeable so it really just comes down to personal preference so let's go ahead and show an example so now what I want to say is let's just say to make things you know super simple we'll say condition y equals 1 so whereas condition is a variable and we're assigning the value of 1 to the condition ah the next thing we're going to say is Wow condition is less than 10 so we're actually using something called an assignment operator we will talk a little bit more on those uh in probably a couple videos but for now this should make total sense to you you should be able to read this and be like ok that makes sense so while the condition is less than 10 what do we want to do well for now let's just print whatever the condition is and now we're now we're going to use another thing that I was trying to show you guys earlier but for whatever reason it yelled at me and we're going to do condition plus equals 1 and what plus equals does is it takes that variable and adds one to it so plus equals you might be thinking why didn't you just do condition plus one why not that well the reason why you don't do that is condition plus one will just yield a value but it won't actually assign that value to anything so condition plus equals one is really the only thing that's equal to can do the same operation as plus equals will be condition equals condition plus one now as you can see uh this is a much shorter way to do that but that this is what it's doing it's just taking the previous value and adding one to it so we'll do that while it's less than 10 it's going to add one to it and then obviously eventually it's going to get to the point where it's no longer less than 10 and this while loop will stop or break so let's save and run that one and as we can see we ran all the way through 1 2 3 4 5 6 7 8 9 and stop so as you can see when it did become 10 it didn't run because 10 is not less than 10 so um so that's one of the neat things you can do now the other thing probably the most common use for me uh with the while loop is the following so I actually prefer the next loop that I'll be showing you guys more but while does have a nice little thing for it and that is called the Wow true loop and what's nice about this is it makes what's known as an infinite loop now sometimes the infinite loop is something that you don't want but other times the infinite loop is something you do want so in order to make your own little infinite loop you can do while true and true basically I means this will always run because while true is true it runs and it's just like this like so when you look at wow it's almost like an if statement is being thrown in here and I'll teach you guys about if statements soon but condition less than 10 basically it's asking is that true or false is that true or false every time through and when as soon as it's false it stops running so that's why when you just throw true in here true is like a abilene so to speak in programming terms and it's like you know 0 or 1 and so you can use true and it'll do stuff now I haven't showed you guys the time module yet so this is really going to go quite crazy it's going to print doing stuff as fast as it can so I will warn you guys beforehand I'm sure some of you guys have already run the script out of it just cheer excitement and now you're probably like oh my gosh what do I do well what you need to do at this point is to break this infinite loop or any running script you can do control hold ctrl and press C and that will break the script so let's go ahead and and run this bad boy so run it save it run it and it's just like going crazy printing stuff right it's just wrong so to break it control see and it might take a second because I let it go for a while so it probably has quite the queue yes it is going for a while I shouldn't have talked so much there it goes finally man anyway so if it doesn't happen initially for you it's just because it had so many you know that it got behind because the this little console here is pretty slow if you did the same thing like in an actual you know you executed the script control see usually is instantaneous pretty much so anyways that's the while loop hopefully you guys understand the idea here um and in the next tutorial what we're going to cover is the for loop typically the wow loop is most used as a form of counter right so it's just it's there to count something that's where I honestly see it use the most but actually the for loop and the while loop can be completely interchangeable so the while loop can do what a for loop does and a for loop can do what a while loop does it just comes down to preference I tend to like the for loop better but it really doesn't matter so anyway that's what we're gonna be doing in the next video is the for loop hopefully you guys learn something new hopefully you guys are enjoying the tutorials as always thanks for watching thanks for all the support and subscriptions and until next time
Info
Channel: sentdex
Views: 505,831
Rating: 4.8677192 out of 5
Keywords: Python (Software), while, loop, while loop, programming, tutorial, basics, python 3, python 3.3, python 3.4, python 2 and 3, python 2.7, 2.7, 3.3, 3.4, Python (Programming Language), Control Flow, Computer Programming (Conference Subject)
Id: jSs58VZVLw8
Channel Id: undefined
Length: 5min 55sec (355 seconds)
Published: Tue Jan 21 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.