Skip to content
Related Articles
Python | math.factorial() function
• Last Updated : 11 Mar, 2019

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.factorial() function returns the factorial of desired number.

Syntax: math.factorial(x)

Parameter:
x: This is a numeric expression.

Returns:  factorial of desired number.

Code #1:

 # Python code to demonstrate the working of factorial()    # importing "math" for mathematical operations import math     x = 5    # returning the factorialprint ("The factorial of 5 is : ", end ="") print (math.factorial(x))
Output:
The factorial of 5 is : 120

Code #2:

 # Python code to demonstrate the working of factorial()  # importing "math" for mathematical operations import math   x = 5y = 15z = 8  # returning the factorialprint ("The factorial of 5 is : ", math.factorial(x))print ("The factorial of 15 is : ", math.factorial(y))print ("The factorial of 8 is : ", math.factorial(z))

Output:

The factorial of 5 is :  120
The factorial of 15 is :  1307674368000
The factorial of 8 is :  40320

Code #3: Throws ValueError if x is not integer

 # Python code to demonstrate the working of factorial()    # importing "math" for mathematical operations import math     # when x is not integerprint ("math.factorial(13.7) : ", math.factorial(13.7))

Output:

ValueError: factorial() only accepts integral values

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up