To find factorial in Python you can use factorial()
function from Standard math Library of Python Programming Language.
The product of an integer and all the integers below it is called factorial. For example, factorial of five ( 5! ) is equal to 120 by multiplying 5 * 4 * 3 * 2 * 1. !
(Sign of exclamation) is used to represent factorial in mathematics.
factorial()
function exists in Standard math Library of Python Programming Language. The purpose of this function is to calculate factorial and return its value.
Python factorial()
Function Syntax
The syntax of factorial function in python is:
math.factorial( x )
Python factorial()
Function Parameters
x
– Any valid Python number (positive). This parameter is required.Note: if the
x
parameter is not a number or a negative number, factorial function will return an Value Error.
Python factorial()
Function Compatibility
Python 2.x – Yes
Python 3.x – Yes
Python 3.x – Yes
Python factorial()
Function Return Value
The factorial function will return an integer value after calculating the factorial.
Python factorial()
Function Example
1 2 3 4 5 6 | # import math library import math print(math.factorial(5)) print(math.factorial(7)) print(math.factorial(15)) print(math.factorial(20)) |
Output of Python
factorial()
function:
120
5040
1307674368000
2432902008176640000
Note that if parameter x
value of math.factorial( x )
function is negative then Python compiler raises ValueError exception.
Loading...