The Wayback Machine - https://web.archive.org/web/20240814165018/https://www.geeksforgeeks.org/python-math-factorial-function/
Open In App

Python | math.factorial() function

Last Updated : 16 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.
Time Complexity: O(n) where n is the input number.
Auxiliary space: O(1)

Code #1: 

Python3




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


Output:

The factorial of 5 is : 120

Code #2: 

Python3




# Python code to demonstrate the working of factorial()
 
# importing "math" for mathematical operations
import math
 
x = 5
y = 15
z = 8
 
# returning the factorial
print ("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 

Python3




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


Output:

ValueError: factorial() only accepts integral values


Similar Reads

Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Python | sympy.factorial() method
With the help of sympy.factorial(), we can find the factorial of any number by using sympy.factorial() method. Syntax : sympy.factorial() Return : Return factorial of a number. Example #1 : In this example we can see that by using sympy.factorial(), we are able to find the factorial of number that is passed as parameter. # import sympy from sympy i
1 min read
How to find the factorial os a number using SciPy in Python?
SciPy is an open-source Python library used to solve scientific and mathematical problems. It is built on NumPy and it allows us to manipulate and visualizing with a wide range of high-level commands. Scipy also provides a scipy.special.factorial() function to calculate factorial of any number. scipy.special.factorial() This function takes two para
2 min read
factorial() in Python
Not many people know, but python offers a direct function that can compute the factorial of a number without writing the whole code for computing factorial. Naive method to compute factorial [GFGTABS] Python # Python code to demonstrate naive method # to compute factorial n = 23 fact = 1 for i in range(1, n+1): fact = fact * i print("The facto
2 min read
Python math function | modf()
modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Syntax : modf(number) Time Complexity: O(1) Auxiliary Space: O(1) Parameter : There is only one mandatory parameter which is the numbe
3 min read
Python math function | hypot()
hypot() function is an inbuilt math function in Python that return the Euclidean norm, [Tex]\sqrt{(x*x + y*y)} [/Tex]. Syntax : hypot(x, y) Parameters : x and y are numerical values Returns : Returns a float value having Euclidean norm, sqrt(x*x + y*y). Error : When more than two arguments are passed, it returns a TypeError. Note : One has to impor
2 min read
Python | math.fabs() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.fabs() function returns the absolute value of the number. Syntax: math.fabs(x) Parameter: x: This is a numeric expression. Returns: the absolute value of the number. Time Complexity: O(1) Auxiliary Space: O(1) Code #1: C/C++
1 min read
Python | math.floor() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.floor() function returns the largest integer not greater than x. If number is already integer, same number is returned. Syntax: math.floor(x) Parameter: x: This is a numeric expression. Returns: largest integer not greater th
1 min read
Python math function | copysign()
math.copysign() is a function exists in Standard math Library of Python. This function returns a float value consisting of magnitude from parameter x and the sign (+ve or -ve) from parameter y. Syntax : math.copysign(x, y) Parameters : x : Integer value to be converted y : Integer whose sign is required Returns : float value consisting of magnitude
1 min read
Python | math.ceil() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.ceil() function returns the smallest integral value greater than the number. If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is a numeric expression. Returns: Smallest integer no
1 min read
Practice Tags :
three90RightbarBannerImg