Python math.factorial() – Factorial Function

Python math.factorial()

math.factorial(x) function returns the factorial of x as an integer.


The syntax to call factorial() function is



xYesAn integer value. x >= 0.

Note: Since Python v3.9, factorial() does not accept floats with integral values like 5.0.

If x is not integral value, then factorial(x) raises ValueError.


1. Find factorial of integer

In the following program, we shall read an integer from user, and find its factorial.

Python Program

import math

x = int(input('Enter an integer : '))
result = math.factorial(x)
print('Factorial :', result)

Output #1

Enter an integer : 5
Factorial : 120

Output #2

Enter an integer : 0
Factorial : 1

Output #3

Enter an integer : -4
Traceback (most recent call last):
  File "/Users/pe/Desktop/Projects/Python/", line 4, in <module>
    result = math.factorial(x)
ValueError: factorial() not defined for negative values

2. Find factorial of integer online

You can run the following program online and find the factorial of a number.

Python Program

import math

x = 7
result = math.factorial(x)
print('Factorial :', result)
Run Code Copy


In this Python Math tutorial, we learned the syntax of, and examples for math.factorial() function.

Related Tutorials

Code copied to clipboard successfully 👍