ExampleEdit & Run
#Use math.factorial function

#import the math module
import math

#Use the factorial function
print(math.factorial(5))
copy
Output:
120 [Finished in 0.011269974056631327s]

The math.factorial() function is used to calculate the factorial of a given number. The factorial of a number n  is written as n! and is equal to the product of all positive integers less than or equal to n. For example the factorial of 5 is 120, because 5 x 4 x 3 x 2 x 1 = 120.

Syntax:
math.factorial(n)
copy

Where, parameter is an non-negative integer whose factorial is to be calculated. 

ExampleEdit & Run
import math

>>> math.factorial(0)
1
>>> math.factorial(1)
1
>>> math.factorial(3)
6
>>> math.factorial(4)
24
>>> math.factorial(5)
120
>>> math.factorial(7)
5040
>>> math.factorial(11)
39916800
>>> math.factorial(20)
2432902008176640000
copy

If n is not a non integer, a TypeError is raised. Whereas, if n is a negative number, a ValueError is raised.

ExampleEdit & Run
import math 

math.factorial(5.5)
Output:
Traceback (most recent call last):   File "<string>", line 3, in <module> TypeError: 'float' object cannot be interpreted as an integer [Finished in 0.010470842011272907s]
ExampleEdit & Run
import math

math.factorial(-5)
copy
Output:
Traceback (most recent call last):   File "<string>", line 3, in <module> ValueError: factorial() not defined for negative values [Finished in 0.010298786219209433s]