Calculate Factorial in Python

In this article we will discuss how to calculate a factorial in Python.

Table of Contents

Factorial formula
Calculating factorial in Python
Factorial functions in Python


Most often you would see factorials in combinatorics algebra and probability theory.

Factorials are used in permutations (calculating the number of ways to arrange some objects) and combinations (calculating the number of ways a subset of objects can be selected from a set of objects).

Factorial formula

Basically, for any integer n, that is greater than or equal to 1 ((n geq 1)), the factorial is the product of all integers in range ((1:n)) and is denoted by (n!).

The factorial formula:

$$n! = n(n-1)(n-2)…(n-m)$$

where (m=n-1).

For example, the factorial of 5 would be:

$$5! = 5(5-1)(5-2)(5-3)(5-4) = 120$$

Calculating factorial in Python

With the following steps we will create a program to calculate the factorial in Python:

Step 1: Get user input

Similarly to solving quadratic equation using Python, the first step is to obtain some user input. In this case, we would need a user to enter an integer for which the factorial should be calculated:

n = eval(input(“Please enter the integer for the factorial calculation: “))

Of course, in this step a user can input any data type they want. There are some rules for the factorial calculation that we need to check for:

(n) must be an integer
(n) must be greater or equal to 1 ((n geq 1 ))

Let’s add these checks to the user input code:

check_input = True
while check_input:
n = eval(input(“Please enter the integer for the factorial calculation: “))
if isinstance(n, int) and n>=1:
check_input = False
print(“Please make sure the input is an integer greater or equal to 1”)
check_input = True

Step 2: Calculate factorial in Python

Now we know the (n) value and can continue with the factorial calculation.

Recall that the formula calculates the product of every integer in range (n:1), which means that integer (i_{t-1} = i_t – 1). To do this in Python we will simply run a for loop through every (i_t) value:

f = 1
for i in range(n,1,-1):


Here we start with an initial factorial value f = 1. We need to define a variable to do the calculations, and since the operation is multiplication, f=1 doesn’t change the final result of the calculation.

Then we basically multiply the starting value by each integer in the range. As an example, if we set (n = 5), then range(5, 1, -1) would be [5, 4, 3, 2, 1]. And the calculation would look like:

$$5! = 1*(5*4*3*2*1) = 120$$

Running the above program for (n = 5) should have the result equal to 120.

Factorial functions in Python

The math library (prebuilt in Python) has a function to calculate factorial. The result would be the same as in the code in the above section but will only take one line to calculate it.

from math import factorial

f = factorial(n)


You can use the user input code from the previous section to get the (n) value from the user and use the factorial() function to calculate the factorial.


In this article we covered how you can calculate factorial in Python with math library.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Optimization articles.

The post Calculate Factorial in Python appeared first on PyShark.

Flatlogic Admin Templates banner

Leave a Reply

Your email address will not be published. Required fields are marked *