#Use math.factorial function
#import the math module
import math
#Use the factorial function
print(math.factorial(5))
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
.
math.factorial(n)
Where, parameter n
is an non-negative integer whose factorial is to be calculated.
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
If n
is not a non integer, a TypeError
is raised. Whereas, if n
is a negative number, a ValueErro
r is raised.
import math
math.factorial(5.5)
import math
math.factorial(-5)