In this video, we're going to look at how to use a While Loop in Python

Video Transcription

Hi my name is Art and I teach Python at Noble Desktop. In this video, I'll show you how to use a while loop in Python, compared to a previous example where we were using a for loop.

While loop is totally different. So, for starters, while loops are not definite, so we have no idea how many times it will run. We need to use the keyword "while" and then there needs to be some kind of condition and then a colon.

Right now I'm not actually writing the code, I'm just showing you the anatomy of this while loop. While checks if the condition is true and then, if it's true, it runs whatever we have here within the scope of the while loop. Then it checks the condition again, if the condition is still true, then it runs the print again. We need to be careful because we might run into an infinite loop.

So, the condition is very important and we have to do something with this condition. We could say that the condition is defined as zero. Then, we could say while condition is less than 10. Then I want to print "hello", but there is a problem. If I run this while loop like this, the condition will always be true because 0 is always less than 10 and we will get into trouble and then we will get an infinite loop.

So, how can we change this condition? We need to, for example, with each iteration, increase the condition by one. So, what I'm going to do is I'm going to do condition plus equal one and then I'm going to change this number. Let's do three so I don't want to run it 10 times. So, we could say if condition is less than 3 then print hello.

Now let me explain what's going to happen and then we will run this. So, while checks the condition and at first condition is true because 0 is less than 3. Print "hello" and at the same time increase condition by one, so condition will be one. One is less than three so print "hello" again. Then, increase condition by one so now condition is two. But still, condition is true because two is less than three. Then again we'll see this "hello" and we need to increase this condition by one one more time and then we're going to get three and three in this condition won't be true anymore because three is not less than three.

Let me run this and you'll see it. You'll see again this "hello" three times. Why? Because what we just did was actually increase this condition by one with each iteration and at some point we got three and three, like I said, and you know that three is not less than three, so the condition is no longer true.

So, while loop will iterate as long as the condition is true and then it checks the condition. If the condition is false then it does nothing. So, what I'm going to do here is I'm going to run this again but I'm going to print condition one more time so you see how we're increasing this condition.

Then, I'm going to do this label. So, print "hello". Let me run it now. You'll see condition, first condition is zero less than three, so print "hello". Condition is one less than three then print "hello". Condition is two less than three, so print "hello". Then condition is three and this condition is no longer true, so we stop. That is it, that's a while loop.

Watch my other videos and you'll learn more about Python and how to run different functions and write different comprehensions in Python.