Factorial of Number in Python

In Mathematics, factorial is an important function, which is used to find how many ways things can be arranged or the ordered set of numbers. Let us discuss the definition of the factorial. The factorial of a number is the product of all integers between 1 and itself.

In simple words, if you want to find a factorial of a positive integer, keep multiplying it with all the positive integers less than that number. The final result that you get is the Factorial of that number. So if you want to find the factorial of 7, multiply 7 with all positive integers less than 7. Those numbers would be 6,5,4,3,2,1. Multiply all these numbers by 7, and the final result is the factorial of 7.

Do you still face difficulties in understanding the logic? That’s perfectly fine; we have something more for you. You can scroll down to learn more about getting Factorial of Number using multiple methods.

For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.

Video Explanation to Print Factorial of Number in Python

Program to Print Factorial of Number in Python

There are various methods to get a Factorial of numbers in python. You can find source code and explanations of different methods over here. But before that, let us understand the basic logic of obtaining a Factorial Number.

Consider a number 5. To get the Factorial of 5, we will do.

5*4*3*2*1 – So Factorial of 5 is 120.

Let’s start with our program here.

Method 1: Factorial Number using In-Built function in Python

Source Code and Output

import math
num = 5
print("Factorial of", num, "is", math.factorial(num))
Factorial of 5 is 120

Code Explanation Method 1: Factorial Number using In-Built function

This is an even simpler method. No complex logic, no calculation. Let’s see how. We don’t have to do calculations; all that code is already in the math library’s factorial function.

Here we are importing the math library and using the in-build function to calculate factorial. After importing the math library, we will define the num and assign the value of 5. Then we will write the print statement, and inside print, we will use a factorial function from the math library. By using math. Factorial and pass num as parameters. That’s it. It will return the factorial of that number.

Isn’t it cool to just call a function and get the factorial of that number? This is an In-build function where we don’t have to code much because it is predefined in python, just call it, pass the desired parameter, and you will get the result. It’s as simple as that.

The above program will give you result like Factorial of 5 is 120

Method 2: Factorial Number using Recursion in Python

Source Code and Output

def getFact(n):  
    return n if (n==1) else n * getFact(n - 1);  
num = 5  
print("Factorial of",num,"is",getFact(num)) 
Factorial of 5 is 120

Code Explanation Method 2: Factorial Number using Recursion

The above program is created to get the factorial of the defined number. So, here we start with defining function getFact, which accepts a number as n and returns factorial. Let’s see how. We have the getFact function, which accepts n; inside the function block, we have a return statement. It might look a bit complicated at first, but trust me, it’s not that complicated.

Let’s understand if and else statement inside return first, so if we have checked number is 1. if yes function will return the same number as the factorial of 1 is the same number. In the else section main calculation is written, and the number n is multiplied by all the positive numbers less than a defined number. Here recursively called the same function until the number reaches 1 and returns the factorial of the provided number.

Outside the function, we have defined num as 5. You can instead write an input statement to accept a number from the user and convert it into an integer; after that, print the statement by calling of getFact function and passing the variable num as a parameter.

So the result will be a Factorial of 5 is 120.

I hope now the concept of the Factorial number in Python is clear to you.

About The Author