Python | math.factorial() function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:



The factorial of 5 is : 120

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

ValueError: factorial() only accepts integral values


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.