 In this video, we're going to learn about the while loop. On the previous video, we saw that iteration, so the idea of executing code repeatedly can be achieved via two different strategies. So the first one we saw was the recursion strategy, a function that calls itself. The other strategy we simply refer to as the looping strategy. And we know that from very early in this course because in our very first example, we already used a for loop to determine the average of all the even numbers in the list of numbers. Okay, so let's go ahead and create a new file and let's call it while loop. And we will see a first example of how a while loop works and discuss a little bit why a while loop is so important to have in a programming language. So our example in the last video, let's also briefly review that. It's a countdown until a party begins. That is what the example that we talked about in the previous video. Let's make that a bit smaller. And how did we do that? Well, we achieved that using a for loop. So using the looping strategy, of course. And we did that using the range build-in. So let's first investigate what is typical for using a range build-in and why does that enable us to use a for loop? So a range build-in, the range build-in is called with one number, let's say 10, gives us the 10 numbers going from zero to nine. If you want to see the numbers, the best way to do that is to use the list constructor to transform the range object into a list object. And we see the range object here has the numbers from zero through nine. If I go ahead and of course, alternatively we could also use a for loop. So we could say for n in range and then simply say print n and this gives us or prints out the numbers one by one on a line on their own. Then we saw if you want to specify a start value, we just have to give the range build-in two arguments. So the first argument and the second argument, the first argument is the first number that is in the range of whole numbers. And the second one is the number that is just not in the range, okay? So it's the first number, the first whole number that is not in the range. So in this example, we are simply going from the numbers from one through nine. If I want to include a 10, I can do so by simply changing the 10 into 11. Now this goes in a step size of one. So every number, every next number is obtained by taking the previous number and adding one to it. This is what we call the step size. This is the third argument in the range build-in. So we simply add one here and now comes a trick. So first of all, we can change the step size. Let's say I want to model only the odd numbers. So I go like this. So this is the odd numbers from one through nine because 11 is the first number that is not included anymore. But now we can use a trick and we can go backwards. So instead of, let's say we want to model a countdown from 10 to until the party begins. And instead of starting at the number one, we start at the number 10 and we are going to countdown, let's say, until one. And now we have to change the step size, for example, into negative one. And now we are basically looping backwards, okay? Now the one here is missing. Why? Because the right-hand side is always excluded. So in order to include the one, we have to set the second argument to zero. And now we have the countdown that we also saw in the previous video. So again, what property is it that enables us to use a follow-up here conceptually? Well, the idea is that we can express whatever sequence we want to loop over by a mathematical rule. So a rule where given one number, we can always calculate the next number. And also, one thing that is also very important here in the follow-up, a follow-up requires us to also have an end, okay? So in other words, the range built-in models the numbers, in this case, from 10 to one. And that means we know it is going to be 10 numbers and we also know where the end is. We also know the end is at one, okay? So that is important for using the follow-up in the first place. And we will see in a future video when we talk about the so-called collats example of when this fails. In particular, when we don't know when the loop ends, then we cannot use the follow-up, okay? But we will do so in another video, so don't worry about this right now. So now, what we do in this video is, let's look at the alternative way of how to model loops a more generic way. And we are going to use the while statement. And the while statement is usually included in basically any programming language. And it is the more generic loop. So in other words, the for loop is kind of a special case of the while loop. And we will see why. So how does the while statement work? Well, instead of for, we write while. And then we are going to write some condition, okay? And a colon. And then we have a code block where we write some code. And the code inside the code block is the code that is going to be repeated. So that is the code that is going to be run repetitively. So if I want to model a countdown again here, what we will do is I will simply go ahead and copy paste the print n here. And this is simply, yeah, this is simply the code that is repeated. And now what happens is Python or any other programming language using a while loop will go ahead and will check this condition. And as long as this condition is true, then this code is going to run, okay? So in other words, we have to come up with some condition first. So what can we do? Well, what we can do is the following. Let's go to before the while loop. And let's say I will start with an n set to 10. Okay, I want to count down from 10, just as we do above. So how long do I have to count down? So how long do I want to repeat this code here, the print n line here? Well, that is now rather easy to see. Well, we are counting down. And until n becomes, and we will subtract one, of course, from n. And as long as n is greater than zero, strictly greater than zero, this is how long we want to repeat the code. So let's go ahead and write a condition for that. Let's say as long as, so while n is greater than zero, that is a condition. So this is going to return true or false. And now we have to add another line that basically manages the n variable. What do I mean by manage? Well, if I execute this code, this code here would run forever, okay? So maybe let me comment out the function here, and let's simply the print function call, and let's maybe simply write n, and let's execute this code. And what you will see is, this code cell is going to run forever, okay? We see the star here, something is being calculated, and it is going to run forever. So why is that? Well, the condition that n is greater than zero is always true. Why? Because I set n to 10 and I don't change n, okay? So nothing is going to happen. If this happens on your computer, what you can do is you have to press the stop button, and then you get this keyboard interrupt error, and this is basically what forces the cell to stop, okay? And I guess in a couple of the exercises that you will do, you will run into these cases where your program basically or a code cell runs forever, and then you have to do to force stop it, right? I like this. Now, and also the reason why I didn't use the print here, why I commented out the print is, Python is a very fast language. So in other words, we are going to see basically thousands of lines where we simply see the number 10, and I didn't want to do that here, okay? So this is why I commented out the print line. So now we have to do something within the while loop. So what we are going to do is, we have to come up with some rule of how we want to modify n, and that is pretty simple in this case. So in this situation, we simply go ahead and we'll say the new n is going to be our old n minus one, okay? So now let's recap what this code cell does. First we set n to 10, then we go into a while loop. The while loop takes a condition. As long as the condition is true, this code in the code cell is going to be executed repeatedly. So we go to the bottom and we go back up to the top and back up to the top and so on. That is why we call it the loop. As long as this condition is true, the loop is going to run. So in other words, we have to make sure that this loop at some point will stop. And how do we do that? Well, as long as we start with some positive integer and we subtract one at all times, at some point n will become zero, right? If we keep subtracting one from some number that is larger than zero, at some point we will hit zero. So let's see how this works. Let's execute this code cell. And indeed, now we see the numbers 10, nine, eight and so on until one, okay? So now let's simplify this a bit. So what we could do is we could go ahead and instead of saying n equals or n is set to n minus one, we could simply say n minus equals one. So this is the shortcut version. This makes it a bit more concise. It does the same thing. And now let's also go ahead and make this a function. So what do I mean by make this a function? Let's go ahead and indent everything one level to the right. And let's say just like we did in the previous video, let's say we define a function called countdown. The function takes an argument called n or the formal parameter. The only one the function takes is called n. So in other words, this n set to 10 is now not needed anymore because n is now the input, the parameterized input to the function. Let's also go ahead and write some nice doc string. So just as before, we simply write print a countdown until the party begins. And again, the function takes an argument going to be an n. It's supposed to be an integer. It's the seconds until the party begins. And it better be a strictly positive number. And we will see in a future video where we investigate what happens if we don't pass an integer. So we can actually mess this up by not giving the function what it wants. The function wants a positive integer, a strictly positive integer. And now the only thing that is still missing in the function is we also have to print happy new year or print happy birthday, whatever the function wants to model as we saw in the previous video. And now what we do is after the while loop is over, we simply go ahead and maybe let's leave one line empty. Let's simply go ahead and say print happy new year. And now if I go ahead and I call the countdown function with a while loop, let's say with 10, then it says 10 until one, 10, nine, eight, and so on until one, and then it says happy new year. Okay, so now the function works. Okay, so now you can compare that with the countdown functions that we saw in the previous video. The first one was the recursion version. And the second one was the version using a for loop. And now we have a third version using a while loop. And now let's also go ahead and copy paste that into Python tutor. And you see that in this Python tutor example here, I on purpose left this open because here we see the recursion version. So let's contrast this within Python tutor with the same code, the same example, but using a while loop and see what is different here. So let's also go ahead and this was three. So let's also go ahead and say we call the countdown function here with an argument of three. And let's visualize this code. We also see as we did before, there's this little white area for the print output. You only see with this in Python tutor if we actually use the print function. So let's go ahead. We first create the function object. Now we call it where n is set to three. Okay, let's go ahead. And now the function begins to run and it checks while n is greater than zero print n. Okay, so print n, print three. And now the function does something internally. It takes whatever n it got. So in this case three, and it simply subtracts one from it. So n becomes two. Okay, that is a big difference to the recursion version. So let's go back to the recursion version. So in the recursion version, we also have a global countdown object for the function. The function is being called with n set to three. And then because the condition in the if clause is not true, the function will jump into the else clause. It will print out three just like this function does. And now the function in the recursion version is gonna go ahead and will call itself. So we get a second function call object. So a second scope, right? And then we saw in the previous video where we talk about this in detail that the function calls itself a couple more times. And then after a while, it will simply basically collapse all the function, the function scopes, and they all go away. But we see that's the important point. The important point is we have many of them. And that is of course always true for a recursion because in a recursion, the whole idea is that we have a function that calls itself, okay? But now let's look at the Y loop. In the Y loop, the function starts, let's go back to the start of the function call n is set to three. And then inside the function scope, we are going to update n to two, then we print out two, then we update n to one, then we print n again, which is now one. And now we're gonna go down, we decrement n one more time, it becomes zero. And now the interesting thing happens, the Y loop has a condition that is now false, okay? So if I now compare if zero is strictly greater than zero, the result will be false. Therefore the Y loop is over and we see the red arrow will jump right down to the print happy new year line, okay? And it will simply print out happy new year. So in other words, at all times, when I called this function here with a Y loop, we only have one function call. In the recursive version, we have many function calls in parallel where each one waits for the next one to finish, okay? That is what we call a stack. So the function call is basically, every function call is waiting for the next one and only the last one, once we hit n set to zero, then they start to collapse again. That is usually what we call a stack, but that's a technical term that's not so important. But the important thing is the recursion version needs a lot more memory, right? Because it needs to have in this case four function scopes that all have a variable called n and the while version only has one function called, function scope with one name called n, okay? So we have less variable usage here. And that is, of course, an issue. Once we deal with a recursion where the function calls itself, let's say, many thousand times and we will see an example of that in a future video here, okay? So that is the big difference between the Y loop and the recursive strategy. Let's go back to JupyterLab and let's also recap of why I introduced the Y loop here. Well, I introduced the Y loop first and foremost because it is a generalization of a loop. So in other words, this Y loop here does exactly the same as the for loop and the for loop is limited in that it can only loop over things, over sequences that where we have a predictable number of elements that are to be printed out. In other words, a for loop only works if I can predict the end of the loop. And we will see in a future video called the collage example that this is not true for all problems where we have to loop, right? So there will be examples where the for loop will not work and the Y loop will still work, okay? So the for loop is basically like a special case of the Y loop which also works only in special cases and it happens to be, it turns out in real life, usually a for loop solves the problem but again, there are a couple of problems in programming that can only be solved using a Y loop but now we have seen the count on function in three different implementations using a recursion, using a for loop, using a Y loop, all of them do the same thing but the important idea is the looping version so both the for loop but also the Y loop versions they only use one function scope, the recursion uses many function scopes, okay? That is the big, big difference in memory and this will lead to a couple of difficulties we will also see in a future video. Okay, so that is the Y loop. So I will see you then in the next video.