# Python math.gamma() Method

Python `math.gamma()` method numerically computes a `gamma` value of the number that is passed in the function.

## Syntax

``````math.gamma(x)
``````

## Parameters

`x` A positive number whose gamma value is needed.

## Returns

This method’s return value is a floating point value representing the `gamma` value of a number, which is equal to the `factorial(x-1)`.

## Example Codes

Following are a few code snippets to learn the use of the `math.gamma()` method.

### Use of the `math.gamma()` Method

Example Code:

``````import math

x = 65
value= math.gamma(x)
print(f"The gamma value of {x} is {value}")

x = 3
value= math.gamma(x)
print(f"The gamma value of {x} is {value}")

x = math.inf
value= math.gamma(x)
print(f"The gamma value of {x} is {value}")
``````

Output:

``````The gamma value of 65 is 1.2688693218588412e+89
The gamma value of 3 is 2.0
The gamma value of inf is inf
``````

Note that the values entered in the function must be positive only. If the number entered is a negative integer, it will return a `ValueError.` If the entered parameter is not a number, it produces a `TypeError.`

### Demonstration of the `math.gamma()` Method’s Formula

Example Code:

``````import math

x = 18
value= math.factorial(x-1)
print(f"The gamma value of {x} is {value}")

x = 3
value= math.factorial(x-1)
print(f"The gamma value of {x} is {value}")
``````

Output:

``````The gamma value of 18 is 355687428096000
The gamma value of 3 is 2
``````

Note that `math.gamma()` numerically computes the `gamma` value `factorial(x-1)` of the given `x`. We have used `factorial(x-1)` instead, which returns the value as an integer.

### Demonstrate `TypeError` & `ValueError` Using the `math.gamma()` Method

Example Code:

``````import math

x = -2
print("The gamma value is ",math.gamma(x))

x = 'h'
print("The gamma value is ",math.gamma(x))
``````

Output:

``````Traceback (most recent call last):
File "main.py", line 5, in <module>
print("The gamma value is ",math.gamma(x))
ValueError: math domain error
Traceback (most recent call last):
File "main.py", line 9, in <module>
print("The gamma value is ",math.gamma(x))
TypeError: must be a real number, not str
``````

Note that the following errors might occur when entering the wrong parameters.

### Find Time Complexity of the `math.gamma()` Method

Example Code:

``````import math
import time

x = 3
startFact = time.time()
fact = math.factorial(x-1)
print ("The gamma value calculated using factorial is "+ str(fact))
print ("The time taken to compute the value using the factorial is "
+ str(time.time() - startFact))

start = time.time()
gamma = math.gamma(x)
print ("The gamma value calculated using the gamma() function is "+ str(gamma))
print ("The time taken to compute the value using the function is "
+ str(time.time() - start))
``````

Output:

``````The gamma value calculated using factorial is 2
The time taken to compute the value using the factorial is 0.0009968280792236328
The gamma value calculated using the gamma() function is 2.0
The time taken to compute the value using the function is 0.0
``````

In the above code, we have used the `time()` function to find out the time complexities of the `gamma()` function and `factorial()`. So, we can say that the `gamma()` function computes the value faster.

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.