 Okay, welcome everyone to intro to recursion in Python. And in this video, I'm going to talk to you about a little bit about recursion, go figure. So first, I want to thank my channel members, Kevin, Paul and Jan, who have been a member for one month or more, Moe Kim, Chiang, Phinez, Peter, Uli, Bogdan, Ezra and welcoming and joining them, I should say is Eric. Thank you so much for supporting the channel. So today I'm going to be talking about recursion. As you can see, I've already actually written the code. I'm going to show you kind of how it works. And this is a very simple example. It's nothing too complex. But it does explain the basic concept. So first, let's take a look at this section of the code. And this is something that hopefully you're familiar with. If you're not familiar with loops, you're not ready for recursion. So this is a very simple basic countdown loop using a while loop. And we start here at 10. And then while x is greater than or equal to zero, we print x, then we decrement x by one until we get down to negative one. And the loop stops. So basically we have 10, 10 is greater than or equal to zero print 10. So track one, that gives us nine, nine is greater than or equal to zero print nine, so on and so forth. Now once we get to zero, zero is greater than or equal to zero. So we print it, we get to negative one. That's important for the recursion. And then we say is x greater than or equal to zero is negative one greater than or equal to zero. The answer is no, functions are the loop is over, and we continue on with the rest of the program. Now I'm going to show you how to do the same thing using recursion. So recursion is just a fancy term. And it is for a function that calls itself. So as you can see here, I created a function called countdown. It accepts one argument x in this case. And then we have something called a base case, which I'll get to in a minute or base condition. So when we call the function, it says is x later less than zero. If so return none return is how we exit a function is a way of I should say a way of exiting function. So then otherwise we're going to print x. And then here we're going to actually call countdown again by itself. So and with the decrement. So let me kind of explain that one more time. So countdown. So we call the function x is 10. Now is x less than zero? Of course not. 10 is not less than zero. If it were, we would return none functions over. So think about that for a second. Up here we had while x is greater than or equal to zero. And down here we have if x is less than zero. These are the same thing basically. So zero is true here. And zero would be false here. So these are kind of a little bit opposite to true and true and false. So what we're looking for is x is less than zero, because we want to actually get to zero. We want to print zero. So as long as x is not less than zero, we go ahead and then we call a function with x minus one. So we say countdown 10 goes here is 10 less than zero? No. Print 10. Call countdown again with 10 minus one, which is nine. Now we got nine. Same thing. Eight, seven, six, five, four, three, two, one, and then zero. And then is zero less than zero? No. Print. Call countdown with negative one. Is negative one less than zero? Yes. Return none. We go back and continue the program. Okay, so let me go ahead and run that and show you what that looks like. Okay, you'll see there's a little bit extra code there. So we did our countdown loop 10 down to zero. And then we have our countdown with recursion 10 down to zero as well. So this is what we expect. We expect the exact same output. So this is just something to keep in mind. You don't have to use recursion. It sometimes makes the code easier to read, depending on the programming language, it can be faster than a loop. But it's not absolutely necessary to do, although it does make certain types of problems easier to solve, at least in the way that you code them, as opposed to using a loop. So we took this loop, and then we just basically recreated it in this function. And instead of looping, the function calls itself. That's what recursion is. This is when the function calls itself. So now let's take a look here at a simple linear search. I did a different video about searching and that kind of stuff. I'll try to remember to put a link down below. So if you haven't seen that, you might want to watch that one first, and then kind of come back to this one. But you see here, we've got a list of scores. Now this doesn't have to be scores. It could be a list of social security numbers. It could be a list of credit card numbers. It could be a list of names. It doesn't really matter. It's just something that we are looking through and searching for. So we've got a target score. In this case, it's going to be 50. So we're looking for the index of this particular number. So it's going to be 0, 1, 2, 3, 4, 5, 6. It's going to be 7. So that's the answer. So what we've done is we've iterated through each of these items. So we start with the index, 0. We go all the way to the last item, the length, which is of course the length minus 1. So length of scores. And then if the scores index equals the target score that we're looking for, print the index. And I probably could have put here actually break. So we could break out of the for loop. So let's go ahead and run that, see what that looks like. You see here, it gives us a 7, because this is 0, 1, 2, 3, 4, 5, 6, and 7. Now I want to do the exact same thing with recursion. So if we go back to this particular example here, countdown, we need to think of the two, kind of two things here. We need a base case. And we need to continue, or base cases I guess, maybe, and we need to continue calling the function until we get what we're looking for. Now in this particular problem, we actually have two possibilities. You find it, or you don't find it. Here, a countdown only had one possibility. The countdown is over. So in this one, it's a little bit more complicated because we have a case where we find it, we have a case where we don't find it. Now with a loop, we don't have to worry about that, because basically it just stops at the end of the loop and the program continues, which is quite nice. Now with a recursive function, we got to be careful, because if we don't stop the recursive call by using a return, the program will get stuck in a loop and you'll get a probably recursion depth exceeded error or something. You'll see that. I'll talk about that a little bit. So what we want to do, so you can see it's gotten a little bit more complicated. So I'm doing just a basic linear search. And this linear search, I'm going to be starting at a certain index. I'm going to be looking through a list of items for a particular target. Now when we start searching, of course, we want to start at zero. And here's our list. And our target is still going to be 50. So one base case, as I mentioned before, is where we don't actually find the item. So the item doesn't exist. So if I was searching for 51, instead of 50, which is 51, sorry, 51 is on the list. If I was searching for 52, which is not on the list, then I would return none. I want to return zero because zero is actually one of the possibilities. Now I could also, I could have changed this. I could have made this negative one. Certain languages would do it that way. But I'm just going to say it's going to be none for now. And that's really up to you as a programmer. The other possibility is that we do find our target. If we do find the target, we're going to return that index. And then once again, once we return that, the recursion is going to be done. Notice here, if index equals the length of items. So if I've gotten all the way out to the end, okay, so if my index is the length of the items, then I return none. That means I haven't found that particular item. Now, if I haven't reached the end of it, and I haven't found my target, then I return the function call again. But this time, I add one to my index. That's the same as incrementing. So we start out index zero is index zero, correct? No, is index one, index two, index three, index four, index five, index six. Of course, once we get to index seven, then we've got the right number. And we return that particular index. So this will keep searching until we find it. So what I've done is I've printed linear search. We're starting at zero. Our list is called scores. And we're looking for a particular target score. And so if it's not zero, so let's let's do this together. So index zero. So does zero equal the length of items? We've got one, two, three, four, five, six, seven, eight, nine, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19. 19 items. Zero, of course, does not equal 19. Does the value here, so item zero equal the target of 50, does 12 equal 50? Answers no. So then we have to return. Now, here's a really common problem or situation. I did it myself. I forgot to put return here. I do it all the time. I always forget to do that because recursions boggles the mind sometimes. So you need to return the function call. And don't forget to increment. If you don't increment, it's just going to keep searching zero, zero, zero, zero until you get an overflow error. So that is another little gotcha with recursion. So again, so we get zero, then we go one, two, three, four, five. And once we get to seven, which is 50, that's actually our target. So does items index seven equal target? Yes. Then we return seven. And then it will print out the result of that, which is going to be seven. So let me go ahead and run that. And so you can see here we have seven and we have seven. It's the exact same result, which is what we would expect. So that's basically how we do recursion. Now, I mentioned earlier, if you forget to increment here, you'll get an error. And it's called the recursion limit or the recursion depth limit. So you can find out what your recursion limit is on your system by importing the sys module and printing sys.get recursion limit and parentheses. So if I was actually searching for, or if I was doing a countdown, probably easier, and I was trying to do a countdown from 2000, keep in mind, I'd have to call this 2000 times. And this will go beyond the limit. And that will cause an error. Now, there is a function called set recursion limit, you can increase that limit. But I don't know if it's recommended, I don't know enough about it to be perfectly honest. But if you have a small enough search space, and you're certain that you'll keep it small enough, you won't hit your recursion limit, you can just go ahead and use a recursive function where you would have a loop instead. Okay, and again, there are certain types of problems, like if we were doing a binary search, you know, it would probably be cool to do it with recursion. But again, you don't have to do it that way. There's no requirement to use recursion. But it does make certain problems easier to code and easier to update and maintain that code, instead of having a bunch of loops, nested loops, nested inside of each other. So yeah, so that's basically it. That's recursion. Now, I would show you the recursion limit, exceeding the recursion limit, but it crashed my computer last time I did that or crashed part of the interface. So I don't want to do that and lose my video that I've been recording here. So yeah, that's it. That is intro to recursion in about, oh, 1415 minutes. So I hope that helps more than it hurts. If you haven't subscribed, subscribe below. If you haven't joined the channel as a member and you're able to do, please do that and join these fine people here who are directly supporting my efforts. And I do very much greatly appreciate that. Have a good one. Keep on coding.