# factorial() in Python

## factorial() in Python

Not many people know, but python offers a direct function that can compute the factorial of a number without writing the whole code for computing factorial.

Naive method to compute factorial

 `# Python code to demonstrate naive method` `# to compute factorial` `n ``=` `23` `fact ``=` `1` ` ` `for` `i ``in` `range``(``1``,n``+``1``):` `    ``fact ``=` `fact ``*` `i` `     ` `print` `(``"The factorial of 23 is : "``,end``=``"")` `print` `(fact)`

Output :

```The factorial of 23 is : 25852016738884976640000
```

Using math.factorial()

This method is defined in “math” module of python. Because it has C type internal implementation, it is fast.

```math.factorial(x)
Parameters :
x : The number whose factorial has to be computed.
Return value :
Returns the factorial of desired number.
Exceptions :
Raises Value error if number is negative or non-integral.
```
 `# Python code to demonstrate math.factorial()` `import` `math` ` ` `print` `(``"The factorial of 23 is : "``, end``=``"")` `print` `(math.factorial(``23``))`

Output :

```The factorial of 23 is : 25852016738884976640000
```

Exceptions in math.factorial()

• If give number is Negative :
 `# Python code to demonstrate math.factorial()` `# Exceptions ( negative number )` ` ` `import` `math` ` ` `print` `(``"The factorial of -5 is : "``,end``=``"")` ` ` `# raises exception` `print` `(math.factorial(``-``5``))`

Output :

```The factorial of -5 is :
```

Runtime Error :

```Traceback (most recent call last):
File "/home/f29a45b132fac802d76b5817dfaeb137.py", line 9, in
print (math.factorial(-5))
ValueError: factorial() not defined for negative values
```
• If given number is Non – Integral Value :
 `# Python code to demonstrate math.factorial()` `# Exceptions ( Non-Integral number )` ` ` `import` `math` ` ` `print` `(``"The factorial of 5.6 is : "``,end``=``"")` ` ` `# raises exception` `print` `(math.factorial(``5.6``))`

Output :

```The factorial of 5.6 is :
```

Runtime Error :

```Traceback (most recent call last):
File "/home/3987966b8ca9cbde2904ad47dfdec124.py", line 9, in
print (math.factorial(5.6))
ValueError: factorial() only accepts integral values
```

Last Updated on October 24, 2021 by admin

## Python – Add image on a Tkinter buttonPython – Add image on a Tkinter button

Python | Add image on a Tkinter button Tkinter is a Python module which is used

## Python – datetime.tzinfo()Python – datetime.tzinfo()

Python – datetime.tzinfo() The DateTime class provides various functions to manipulate the date and time

## String Slicing in PythonString Slicing in Python

String Slicing in Python Python slicing is about obtaining a sub-string from the given string by slicing it

## How to print without newline in Python?How to print without newline in Python?

Print without newline in Python Generally people switching from C/C++ to Python wonder how to

## Python – Convert number to list of integersPython – Convert number to list of integers

Python | Convert number to list of integers The interconversion of data types is a

## Multi-dimensional lists in PythonMulti-dimensional lists in Python

Multi-dimensional lists in Python There can be more than one additional dimension to lists in Python.

## Python Projects – Beginner to AdvancedPython Projects – Beginner to Advanced

Python Projects – Beginner to Advanced Python has become one of the most popular languages.

## Python – Convert String to bytesPython – Convert String to bytes

Python | Convert String to bytes Inter conversions are as usual quite popular, but conversion