 # 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):
print (math.factorial(5.6))
ValueError: factorial() only accepts integral values
```

Last Updated on October 24, 2021 by admin

## Python program to add two MatricesPython program to add two Matrices

Python program to add two Matrices Aim : Program to compute the sum of two matrices and then print it in Python. Examples: Input : X= [[1,2,3], [4 ,5,6], [7 ,8,9]] Y = [[9,8,7], [6,5,4], [3,2,1]] Output : result= [[10,10,10],

## How to Convert Floats to Strings in Pandas DataFrame?How to Convert Floats to Strings in Pandas DataFrame?

Convert Floats to Strings in Pandas DataFrame In this post, we’ll see different ways to Convert Floats to Strings in Pandas Dataframe? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from

## Python islower() methodPython islower() method

Python islower() method   In Python, islower() is a built-in method used for string handling. The islower() method returns True if all characters in the string are lowercase, otherwise, returns “False”. This function is used to check if the argument

## Python 3 – input() functionPython 3 – input() function

Python 3 – input() function In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into

## Count Values in Pandas DataframeCount Values in Pandas Dataframe

Count Values in Pandas Dataframe   In this article, we are going to count values in Pandas dataframe. First, we will create a data frame, and then we will count the values of different attributes. Syntax: DataFrame.count(axis=0, level=None, numeric_only=False) Parameters:

## Command Line Arguments in PythonCommand Line Arguments in Python

Command Line Arguments in Python The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of

## Python ord() functionPython ord() function

ord() function in Python Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument. In other words, given a string of length 1, the ord()

## Import module in PythonImport module in Python

Import module in Python Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking