The Python factorial function is one of the math functions used to find the factorial of a specified expression or a specific number. The syntax of the factorial function is shown below
math.factorial(number);
- If the number argument is a positive integer, it returns the factorial of a given number.
- If it is a Negative integer, Positive or Negative Decimals, it returns ValueError. And if it is not a number, it returns TypeError.
Python math factorial Function example
This math function finds the factorial of a given number. In this math example, we will find the same for different data types and display the output.
- First, we used the math factorial Function directly on Positive integers.
- Next two statements, we used it on Python Tuples and Python Lists items. If you observe the above screenshot, this Math factorial function is working perfectly on them.
- Next, we tried directly on multiple values.
- Here, we used the factorial on the String value, and it returns TypeError: an integer is required (got type str)
- Next, We used on Decimal value. It returns ValueError: it only accepts integral values
- Last, We tried it on Negative value. It is returning ValueError: it. not defined for negative values.
TIP: Please refer to the Python Math functions article from our Python basics page to understand the remaining methods.
import math
Tup = (1, 2, 3, 4 , 5) # Tuple Declaration
Lis = [6, 7, 8, 5 , 9] # List Declaration
print('Factoial() Function on Positive Number = %.2f' %math.factorial(3))
print('Factoial() Function on Positive Decimal = %.2f' %math.factorial(6))
print('Factoial() Function on Tuple Item = %.2f' %math.factorial(Tup[2]))
print('Factoial() Function on List Item = %.2f' %math.factorial(Lis[4]))
print('Factoial() Function on Multiple Number = %.2f' %math.factorial(10 + 45 - 48))
print('Factoial() Function on String Value = ', math.factorial('Python'))

Using the Python factorial function on negative numbers
As we mentioned earlier, the built-in math factorial function accepts only positive numbers. If we pass a negative number, it throws a ValueError.
import math
print(math.factorial(-2))
ValueError: factorial() not defined for negative values
TIP: As it does not work with the negative values, use Python fabs() to get the positive number. And then, apply the factorial function. Similarly, it does not support floating-point numbers; use the Python ceil and Python floor functions to round the number to the nearest value. Next, apply the factorial() function on it. We can also use the Python round.
Use math.factorial on a list of items
In the following example, we declared a list of five integer values. Next, we used a Python list comprehension (alternatively, use the Python map() function) to iterate over the list items. On each iteration, apply the math.factorial function to find the factorial of each list item.
import math
n = [1, 2, 3, 4, 5]
fact = [math.factorial(i) for i in n]
print(fact)
[1, 2, 6, 24, 120]
How to calculate factorial without using the math module in Python?
There are several approaches to calculating the factorial, including the for loop, while loop, or recursion. In the following example, we use the Python recursive function to calculate the factorial of a given number. I suggest you refer to the Python Factorial Program article, Python for loop, and Python while loop topics.
def calFactorial(n):
if n < 0:
print("Factorial requires a Positive Number.")
return
if n in (0, 1):
return 1
return n * calFactorial(n - 1)
print(calFactorial(6))
720