 In the next couple of videos, we are going to learn about the concept of iteration. We have seen iteration before when we used for loops, but now we are going to look at this topic into far more detail. So let's first go ahead and create a new file and let's call it a first look at iteration. And again, there will be a couple of further videos following this one. So let's first start with some definition. What is iteration? So what we mean by iteration in this course is to execute code repeatedly. So maybe let's write that down to execute code repeatedly. That's just the concept of iteration, just in very generic terms. And in this course, we are going to use two different concepts to make this happen. So the first one is going to be what we are going to refer to as recursion. And then the second way will be what we are going to refer to as simply looping. So sometimes people use them a little bit differently, but in this course, with iteration, I generally mean to execute code repeatedly. That's it. And then there are two concepts by which we can make this happen. The first one being recursion, the second one being looping. We have seen before, but we will see a lot of more detail about the loops in these next couple of videos. So let's start with the first example to get the idea down. So the first example is going to be waiting for a party to start, or maybe let's call it a countdown until a party begins. So in Corona times, obviously, there are not so many parties, but usually when there is a party, let's say a birthday party, or maybe a new year's Eve party, usually what happens is about a minute before midnight, people get excited. And then 10 seconds before midnight, people start to count on. And they say 10, 9, and so on. And then once we hit midnight, people either say happy birthday, if it's a birthday party, or they just say happy new year. So let's see how we can write code that basically given some number n, let's say 10 or 3, just counts down from that to 1. And then instead of 0, we just say happy new year or happy birthday. So let's write a function to do that. And you will soon see why I use a function. So let's call the function simply countdown. And as I said, it takes one argument going to be called n, n for a natural number, so a whole number. Let's write some doc string. So let's say print a countdown until a party begins. And then we say the arguments, again, it's an n, it's supposed to be an integer, okay? And it's simply the seconds until the party begins. And we could also already specify here that the function needs a positive number. So maybe let's say strictly positive. And then usually what we learned in the last couple of videos when we talked about functions, functions return something, but here this function will not return anything. So this function will simply print out some text, but it will not return anything. But in the next video, we will see how we can also use the concept of recursion with functions that actually return something, okay? So now let's go ahead and write some code. And I claim that what I'm going to write here will lead to code being executed repeatedly. So I'm going ahead and I will say if n is equal to zero, then we're going ahead and we're simply going to print out the message, let's say happy new year, okay? You could change that to happy birthday, but the point is clear. And then we're going ahead and we will say else. And here we will simply say print n and then something interesting happens in the next line. The function will simply call itself from within, so to say. And the argument is not going to be simply n, but it's going to be n minus one, okay? And my claim is that this function models a countdown. So let's see if it works. So let's call the function countdown. Let's just make it three here so that the series will not get too long. And indeed, the function prints out three to one happy new year, okay? So the function does what we expect it to do. Now the question is, which code in this function is the code that is being executed repeatedly? Well, obviously, it is this line here, print n, okay? We see three to one, so this line must have been executed several times, okay? So let's note this down, the code that is repeated. So now let's do the following. I will simply go ahead and copy paste the function and we will go to Python tutor so that we see how this works in detail. What I will do here is I will get rid of the doc string so that we have less to focus on. And down here, I'm simply going ahead and we'll say countdown and we call it again with n equals three. Okay, let's go ahead. So what is different to previous visualizations by Python Twitter that we saw in this course is that now we have this area up here where it says print output, okay? So this is basically the screen to which we are printing out. Down here, we still have our same memory diagram as before. On the left hand side, the frames, so the list of all the names that exist and the scopes in which they exist. And on the right hand side, we have all the objects. So let's go ahead and click next ones and run the function in slow motion. So what this does is it executes the entire dev statement. So the entire code here is going to be executed. So the code is not yet run, but the code that is inside the functions body will move or will be encoded, so to say, inside the function object in memory. And the function has a name. It's called countdown. So countdown references in the global scope. It references the countdown function. So now let's go ahead and call the function. And as we saw before in chapter two, what we see is we get a new scope. So this is an area in which variables exist and the variables, they are local to only this function call that is going on, okay? So the N, which is set to three, will only exist inside this function call. And after the function is done, after the function returns, so to say, then these variables will be removed. And the global scope will survive. But now let's see what's going to happen. So what's going to happen is the first line is executed. This is the condition if n is equal to zero, and this is of course not true. So what happens is we see the red arrow will jump right into the else clause to the line where it says print n. And now let's execute this, print n. And we see indeed we are printing out the number three. Now the red arrow goes to the next line and executes the next line. And the next line is another function call, and it's the function call countdown. So the function is calling itself with n minus one. So n minus one is two, of course. And now what we see is something interesting. So we see we get a new scope here and a new scope basically means the function is now being run two times in parallel, okay? So that is something that we have not seen before. So the function is now being run two times in parallel. And the first function, the first function call to be precise where n is set to three is now waiting for the second function call where n is set to two, okay? So the first function call is not yet done. It's still waiting. It is waiting for the last line in its body to be done. And the last line can only be done if the function or when the function returns, okay? So in other words, this function has to be finished before the upper function call can be finished, okay? So now what does the next function call do? Well, obviously it does the same thing. The condition is still not true. It goes into the else clause. It prints out, it prints out the number two. And then the function calls itself one more time with n minus one. So n is going to be one here. So fast forward, the same thing is going on. So the condition is still not true. So we are still going into the else clause. We're going to print out the number one. And the count on function is going to call itself one more time. And now what we see is we have a chain, so to say, of function calls, okay? So first we have the global scope where the function exists. The function is being called. We get a first scope. The scope creates another function call, which creates another function call, which creates another function call. That is typical for recursion. That is what a recursion is. A recursion is a function calling itself. But we will get back with a more proper definition of what a recursion is in a bit, okay? So now what we see is the ends, the variables ends. They live inside the local scope of the four different function scores. And they're totally isolated, okay? So it's no problem to have the same variable several times in memory in different scopes. So now what's going to happen is the condition, if n is set to zero, when n is set to zero, well, the first condition that n is zero is actually true. So now what's going to happen is the function call that is down here, that is started with n set to zero, will go into the line where it says print happy new year. And this will lead to the output happy new year, of course. And now what's going to happen is, now after this line, print happy new year has been executed, the Python interpreter wants to execute the next line in this body here, in the function body. However, there is none, right? The else clause is obviously not executed because we are in the if clause. So what's going to happen, Python comes back down here after the if else statement, so it would continue right here, okay? However, there is no code. So in other words, nothing is going to happen. The function is basically done. And because the function is done, it has to return. And in the last couple of videos that we talked about in detail about functions in chapter two, what we had is we always had this return statement. And the return statement would always give something back. So a function takes input and it provides us back with some output. However, here we don't specify a return statement. And therefore, the Python default is that the function must have a return value. And if we don't specify one using the return statement, the return value becomes simply the none object, okay? None, as we saw in chapter three, it's a special object, which is basically the absence of a value. So we don't know what the value is. And it is not to be confused with the false. So there is a true and a false, the two booleans. But then there is also none. And none happens to behave like false in a boolean context as we learned. But it is not false, of course. So it's something different. It basically means we don't know what it is. Na, not available. This is usually what we write in English. Okay, and this is now the return value. So this is now the object, the none object, that is now going to be returned from the function call down here, where n was set to zero. And this is now going to be returned to the function call back up here. Now, what is going to happen is this function scope would go away, as all function scopes go away. And the function call where n was set to one will now continue to run. So where are we? Well, we are now in the last line, because this is the line, count on n minus one. This is the line that was executed when I go one step back. Then this line was executed and that was the line that in the first place created this function scope down here. So this is now where we are going to continue. Now, here we have the same observation as we did in the step before. So Python now, now after this line here is done, the count on n minus one line, Python wants to go ahead and execute further code in the function body. However, there is none. Okay, there is no statement or there's nothing, no command, nothing after the countdown in the else clause, but there's also nothing after the if else statement. There's simply no code. So therefore, this function call is also over. It's also done. And similarly as before, it is also going to return the none object. Okay. And now we go back and all of the function calls are now in the last line. So all the only thing they do is basically they return to the previous function call. That's all they do. And the outcome of this basically of this visualization is that in the memory in the Python process, there is nothing left except the countdown variable in the global scope that references the function object that has all the code. And we see on our screen, the output here. And now you may wonder what does this print output really mean? Well, in JupyterLab, we see that as we saw here in JupyterLab, we see that below the code cell in a program in Python that runs on a command line. Maybe you see that on the in the black terminal window, but you could also think that this print output is written to some text file and then we can later on open a text file and see the output. So a printing in so to say has different meanings. So it basically means we print text to someplace and the place could be the screen in JupyterLab. It could be the screen up here in this white box, but it could also be some file to which we basically write this text. Okay. So that's basically like a side effect that the program has. It prints out some text and now we saw how this works. Okay. So now let's go back and introduce some more terms. So what we now are going to introduce is easy to live with definition for what a recursion is. So we say a recursion is a function that calls itself. And that's the important part. And let's be rather casual here and has a way out. So what do I mean with has a way out? So we see when I define the counter function here, the function calls itself down here in the else clause, but in the F clause, it does not call itself. And that is the way out. Okay. And we have a name for that. So the entire if clause here is what we are going to refer to as a base case. So in other words, the base case is the case in the function where the function does not call itself. Okay. So maybe let's add this here. So let's say that's the base case, the way out in simple terms. We can simply say the function has a way out if we don't have a base case. So if, you know, in both branches, the function would call itself, then what we would have is an infinite recursion. So the function would never stop. Okay. That is, of course, an error because we want the function to some sometime it has to stop when it's done. Okay. So that is the base case. And then here the else clause, we could call it the recursive case. So let's say recursive case, but this is also obvious because we could also just leave it away because that is just the recursion, right? A recursion is a function that calls itself. We see in the else clause, the function does indeed call itself. So this is the recursive step. Okay. Okay. So that is what a recursion is. And in future videos, we will learn a lot more about recursion. In particular, we will look at recursion examples where something is actually returned because this function, again, has no return statement in it. So it does not return anything meaningful. It only has a side effect of printing something out. And now to finish this video, let's quickly look at the same example. So maybe let me just copy paste the header right here. And let's do the same function one more time. But using a different, using a strategy that we know from before, let's look into how we could model a function like that also with a for loop because that is what we are familiar with in this course already. So let's simply copy paste the entire header of the function. So the first line, the header line, plus also the doc string, because this won't change. And let me maybe change this function into underscore loop here, the name, so that we have a different name for this. And now how could we model this function or solve the problem without a recursion? Well, what we need is we need to loop over the numbers three to one. Let's maybe as a side experiment, simply write for a number in range, we saw the range object before. So the range object is a thing, a specialized object that just gives us a sequence of whole numbers. So for example, if I give it 10 as the argument, and let's say here I simply write print 10, or maybe let's probably better write print number, that is, of course, a better thing, then we see that we are going to print out the numbers from zero through nine. So this is 10 numbers starting at zero, that is why we already end at nine. Now, if we go ahead and give the range built in a second argument, let's say we want to go from one to 10, then we see that the first argument, the one is included here, and the second argument, the 10 is excluded. So now we have nine numbers. And we can also see that from the arguments, because 10 minus one is nine. So this range object will give you nine elements. And now there is a third argument. This is the so-called step size. So if I give it an argument of one, I just get the same result. However, if I give it an argument, let's say of two, I only get every other number. Okay. So basically starting at one, I always add two until I reach a number that is just smaller than 10. Okay. The 10 is the first number that is not in the range anymore. So this is now the numbers from one through nine, but only the odd numbers. Now, what we could do is, we could simply rearrange the argument. So instead of going from one to 10, I could say I go from 10 to one. And now I go in steps of, let's say not two, because this wouldn't make sense, but I go in steps of negative two. So I started 10 and in every iteration of the loop, I'm simply going to subtract two until I reach one, but one is excluded. Okay. So let's see what happens. And we see indeed it goes 10, eight, and so on. Of course, if you want to model a countdown, we would do so with negative one. And also one more thing we have to change is we have to go until including the number one. So therefore the second argument, the upper bound, so to say, has to be changed from one to zero. And now we are going, we are basically looping from 10 to one. So this is now 11 numbers. No, yeah. No, it's 10 numbers because the zero is not there. Okay. So now let's use what we learned from this cell and copy paste that into the function. And this helper cell here we don't need anymore. So let's get rid of everything here. And now what we have to do is, we have to parameterize this expression. Why? Because we don't want to count on from 10 over times, but we want to count on from n. Okay. So let's replace the 10 with simply n. And let's also maybe make this here a bit shorter. Let's simply call it i. We could also give it some other letter x maybe n. n would be for now n is the one, the only one we couldn't use because n is used here. And now let's go ahead and simply go ahead and say print i. And then after the for loop. So if I execute this function, so let's go ahead and say countdown loop. And I call the function let's say with three as the argument. The only thing that is missing is the happy new year. And this we can copy paste from above here. And of course this has now to go after the for loop, right? If I put it inside the for loop like this, I see happy new year on every line. But if I put it after the for loop just like this, then we see three, two, one happy new year, just the same output as above. And now maybe let's also mark the code that is actually repeated in the loop. This is this code here, the code that is repeated. And that is now obvious. We knew that from before, right? So we saw four loops before. We probably did not see a for loop where we loop backwards, but it's not a big deal. As we see in previous videos, we usually looped over a list objects here. We are looping over a range object, but it's really not a big difference here. And yeah, so this is the code that is repeated. The code that is in the code block managed by the for header line here. And above here, it is a bit more tricky. The code that is repeated is in the else clause here. Okay, so that is now two different concepts. The one concept called recursion, the other concept is the one called that I call looping. And together they are two sides of the same coin. And the coin is basically iteration. Okay, so the concept of iteration making code repeatable can be achieved using two strategies, the recursive strategy using the recursion approach, or a looping strategy. Okay, and here I showed you the looping strategy using a for loop. There will be another video, the next video, where I will quickly tell you what other kind of loops exist. The so-called Y loop. And but for now, simply note what a recursion is. That is the big idea in this video, so that you know what a recursion is. And we will look into more recursion in future videos. So see you in the next video.