def factorial(n): fact = 1 for num in range(2, n + 1): fact = fact * num return(fact)
Here is what the above code is Doing:
1. We define a function called factorial.
2. We create a variable called fact and set it equal to 1.
3. We create a for loop that iterates through the numbers 2 through n + 1.
4. We set fact equal to fact * num.
5. We return fact.