(gentle music) In this lesson, we are going to talk about recursion. Now recursion is very difficult to describe but it's quite easy to understand. So we'll spend more time doing than talking. What recursion is, is in our case, it's a function in Python that calls itself to derive an answer. Now, I know that's difficult to understand but if you recall, as a youth, you were probably told never use the word you're defining in the definition of that word. That is exactly what recursion is. So if I have a piece of cheese and two pieces of bread and I put the piece of cheese between the two pieces of bread, if you look at that sandwich from either side of the sandwich, both of them could be the top, both of the pieces of bread could be the bottom. The top with the cheese is a mirror of the bottom and the cheese, that is recursion. In Python it's used a lot in the calculation of different mathematical processes. In our case, we're going to take lesson three and we're going to save it as lesson four. And what we're going to create is we're going to create probably the most popular of the recursive functions and that is dividing the factorial of a number. So the first thing we have to do is we have to create a function. So we define the function and we'll call it factor. And factor is going to take in a variable. In our case, it's going to be an integer. And we are going to define that function by using function in its definition. Now, what we're going to do is we're going to create an if statement to allow this function to call itself. So if num equals one, meaning if someone enters the number one into our program, then it won't do anything because num does not have a factorial. So it will just return itself. Else, it is going to return num times factor num minus one. And there's our function. So now instead of having an input statement or anything like that, we are just going to assign a value to a variable. Out here after the if statement, we're going to create a variable called X and we're going to assign to it, the number five. After we set the variable we can now just write a print statement that calls and prints out the return value of our function. So it's print factor. And then inside of factor, we have X and now we can save it and run it. And the factorial of five is 120. What a factorial is, is any number that is multiplied by every number that precedes it until it reaches one. So in the case of ours, we have five times four times three times two times one. And that is recursion. In our next lesson, we're going to look at an elegant example of recursion. So see you then.