Get our 4 hour online Python programming course FREE for life with the link below:
http://stoneriverelearning.com/courses/python-programming-for-beginners?product_id=12227&coupon_code=YTPYFREE
FREE PYTHON E-BOOK: http://srepythonprogramming.instapage.com/


Hello and welcome to Chapter 5 in the Python 3 Basics Tutorial Video. In this video, we're going to be talking about the while loop' in Python 3. There are basically two major loops within the Python Programming Language and that is the 'while loop' and the 'for loop'. The 'for loop', we'll be covering in chapter 6. But for now, we're going to be talking about the 'while loop'. The 'while loop' basically does what it sounds like. It basically asks the question, while something is the case, do something. Now, this is going to bring up the conditional operators within Python but luckily the conditional operators make a whole lot of sense. So, I should have no problem covering the two of them all at once. So with that, let's get started.

So the 'while loop' is going to say, while a condition is such, do something. And then it's going to continue running whatever block of code, and by block of code I mean there's a chunk of code that is underneath something and tabbed over. I'll explain that when I show it. Basically, it's going to run that block of code until the condition is no longer the case.

So the easiest way to show that would be a simple counter while loop. So, we'll say condition as our variable equals 1. And then we're going to say while condition, and we're going to say, is less than 10: and this is what makes Python kind of nice is generally – well basically all of the programming languages have what are called 'standards' and most programming languages don't actually require anybody to have good standards, and what I mean by 'standards' is kind of like your code's organization, so to speak. So when we write this colon (:), when we hit the 'return' or 'enter' key, we see that we're actually automatically tabbed over a little bit. And now, what we're going to go ahead and do is we're going to print (condition). And then we're going to go ahead and do condition. And then what we're going to do is a slightly confusing operation but I have no fear, += 1. And what += does is it just adds 1 to the value. So basically it's just, it's kind of like condition += 1 is basically the exact same as, for example, condition = condition + 1. That's basically what += 1 will do for us.

So, I'm going to get rid of this. And in fact, actually, I'll show real quickly, commenting in Python. So we can leave this here and we can do what is called a comment in Python. And a single line comment is designated with a pound sign (#). So there's a number sign and pound sign. You can also do multi-line comments with a triple quote (' ' ') like that. And in here, multiple lines can go here. Like that. So that's such a short version there.

But back to what we were doing in our 'while loop'. And so, we have condition += 1. Let me move this down just to make it pretty clear. Like that. And then that's all the 'while loop' does. So, what we can do now is we can save and run this and we get the output as being 1, 2, 3, 4, 5, 6, 7, 8, 9. And then after 9, it stops. So here, we were basically asking, while condition was less than 10, it prints the condition. So we saw while was 1. That was okay. So, it printed 1, 2, 3, 4, 5, 6, 7, 8, 9. And then at 9, that was the last time the condition was less than 10. So, that worked out. And the conditional operator is the less than sign.

So we could say, condition = '5'. Now, we can ask the question, while condition is less than 15, print ('True'). Let's save and run that. And we throw an error – because this condition here is a string. So we cannot do that.

Now, if we get rid of this and we run this, we're going to find a lot of 'True' are being printed out to our console, and we want to stop that. So to stop it from doing that, if you find yourself in an infinite loop or you just want to stop your script for whatever reason, you could hit the X to X up or you can do a CTRL C and that will break the script. So this generated an infinite loop because 5 will always be less than 15.

Basically what is happening in this 'while' statement is the 'while' statement is going to either return a True or a False. So, the easiest way to make an infinite loop would be, while True: (do something). And we could say, print ('infinite'). Like that. And actually we'd never make it to this. So, I'm going to go ahead and comment out these two lines because this loop here would never end. We're going to save and run this. Now we see we have an infinite amount of infinites. We'll break this, maybe. It doesn't want to break. It took a little bit there. Sometimes there's like a backup too. So don't be too frustrated if it doesn't stop immediately.