Python is a powerful programming language, known for its simplicity and readability. One of the many functions that come with Python is the **factorial** function, which calculates the factorial of a given number.

The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n.

For example, the factorial of 5 is 5! = 5 × 4 × 3 × 2 × 1 = 120. In this tutorial, we will learn how to use the factorial function in Python.

### Step 1: Import the math module

To use the factorial function in Python, you need to import the **math** module. The math module has a function called `factorial()`

which can be used to calculate the factorial of a number. To import the math module, add the following line at the beginning of your Python script:

1 |
import math |

### Step 2: Call the factorial() function with a number as an argument

After importing the **math** module, you can use the `factorial()`

function to calculate the factorial of a number. Here is an example of how to use the `factorial()`

function to calculate the factorial of 5:

1 2 3 4 5 |
import math number = 5 result = math.factorial(number) print("The factorial of", number, "is:", result) |

The factorial of 5 is: 120

### Step 3: Use a custom factorial function (optional)

In case you want to implement your own custom factorial function instead of using the built-in `math.factorial()`

function, you can do so using a **for** loop or **recursion**. Here’s an example that demonstrates how to create a custom factorial function using a for loop:

1 2 3 4 5 6 7 8 9 |
def custom_factorial(n): result = 1 for i in range(1, n + 1): result *= i return result number = 5 result = custom_factorial(number) print("The factorial of", number, "is:", result) |

The factorial of 5 is: 120

Similarly, you can also create a custom factorial function using recursion, as demonstrated below:

1 2 3 4 5 6 7 8 |
def custom_factorial(n): if n == 1 or n == 0: return 1 return n * custom_factorial(n - 1) number = 5 result = custom_factorial(number) print("The factorial of", number, "is:", result) |

The factorial of 5 is: 120

## Full code

Here is the full code using the built-in `factorial()`

function as well as the custom factorial functions using a for loop and recursion:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import math def custom_factorial_loop(n): result = 1 for i in range(1, n + 1): result *= i return result def custom_factorial_recursion(n): if n == 1 or n == 0: return 1 return n * custom_factorial_recursion(n - 1) number = 5 result_builtin = math.factorial(number) result_loop = custom_factorial_loop(number) result_recursion = custom_factorial_recursion(number) print("The factorial of", number, "using the built-in function is:", result_builtin) print("The factorial of", number, "using a loop is:", result_loop) print("The factorial of", number, "using recursion is:", result_recursion) |

The factorial of 5 using the built-in function is: 120 The factorial of 5 using a loop is: 120 The factorial of 5 using recursion is: 120

## Conclusion

In this tutorial, we learned how to use the **factorial function** in Python through the built-in `math.factorial()`

function as well as implementing custom factorial functions using a for loop and recursion. Now you can easily calculate the factorial of any number in your Python programs!