Python Program to Find Factorial of a Number

To find factorial of given number, multiply all integers from 1 to the given number. For example, the factorial of 5 (denoted as 5!) is 1*2*3*4*5 = 120. Factorial is not defined for negative numbers and the factorial of zero is one (denoted by 0!).

Python program for factorial

# Python program for factorial
# of given number using while loop

number = 5

factorial = 1

while(number>0):
    factorial = factorial * number;
    number-=1

print("Factorial of given number is ",end="")
print(factorial)

Output:

Factorial of given number is 120

Explanation:

We are storing the number whose factorial is to be found in the variable names number. factorial variable is used to store the factorial of given number. factorial is first initialized to 1. Then in while loop, factorial is multiplied by number and the result is stored into the number so for first iteration it will be like factorial = 1 * 5 so value of factorial is now 5. number is decremented by 1 and for next iteration value of factorial becomes 5*4, this iteration continues until number becomes 0. Hence we obtain the factorial of given number.

Using For Loop

# Python program for factorial
# of a number using for loop

number = 4

factorial = 1

# you can also choose to increment
# num from 1 to given number by using
# for num in range(1,num+1)

for num in range(number,0,-1):
    factorial = factorial * num;

print("Factorial of given number (using for loop) is ",end="")
print(factorial)

Output:

Factorial of given number (using for loop) is 24

Explanation:

We have used for loop in which num is decremented by 1 with each iteration. It starting value is the given number and for loop terminates when num becomes 0. range() function is used to get a range of values from start to end - 1, in our case it gives range of values from 4 to 1 (not 0) decrementing by 1 with each iteration. Rest of the code is same as that for while loop.

Find factorial using factorial() function in python

Above method is the native method of finding factorial but the more precise and accurate method will be to use the inbuilt function factorial(). It is defined in the math module of python. It is fast because of C type internal implementation.
math.factorial(x)
Parameters :
It takes only one parameter whose factorial is to be found.
Return value :
Returns the factorial of given number.
Exceptions : 
Raises ValueError if number passed is negative or non-integral.

Python code to find factorial using factorial() function


# Python program to demonstrate use of
# factorial() function from math module 

import math

print ("The factorial of 5 is ", end="") 

print (math.factorial(5))

Output:

The factorial of 5 is 120.

Exceptions in math.factorial()

If the a negative value is passed to the math.factorial() function it throws a ValueError as follows
print(math.factorial(-5))

Output:

Runtime Error :

Traceback (most recent call last):
  File "/home/gjf4744fh57g23bg3hv46ga.py", line 7, in 
    print (math.factorial(-5))
ValueError: factorial() not defined for negative values

Also if you passed a non-integral value it throws a ValueError.
print(math.factorial(4.9))

Output:

Runtime Error :

Traceback (most recent call last):
  File "/home/25271864937aghs5hsh56bgr54.py", line 7 in 
    print (math.factorial(4.9))
ValueError: factorial() only accepts integral values