Latest Code Tutorials

Python gamma: How to Use gamma() Function


Python gamma() is an inbuilt method that is defined under the math module, which is used to find the gamma value of the number parameter passed. For instance, if x is passed as a parameter in gamma function (gamma(x)), it returns the gamma value of the number. We can use the math module by importing it. After importing, we use to call this function using the static object.

Python gamma()

Python gamma() function computes the gamma value of the number that is passed in the function. The gamma value is equal to the factorial(x-1) of the number passed as a parameter. Another important point to note is that the math gamma() function only takes parameter values of number type if any other type is passed; it returns TypeError.



Here var is the variable of which gamma value we have to find.


It takes one parameter var, which takes values of numeric datatype and throws TypeError if the argument is of any other data type is passed.

Return Value

It returns the gamma value of the number in the float datatype.

See the following sample code.

import math

var = 0.7

Example programs on gamma() method in Python

Example 1: Write a program to show the working of the gamma() method in Python.

import math

a1 = 0.3
b1 = 0.9
c1 = 0.7
d1 = 0.2

print("Value for parameter ", a1, " is ", math.gamma(a1))
print("Value for parameter ", b1, " is ", math.gamma(b1))
print("Value for parameter ", c1, " is ", math.gamma(c1))
print("Value for parameter ", d1, " is ", math.gamma(d1))


Value for parameter  0.3  is  2.991568987687591
Value for parameter  0.9  is  1.068628702119319
Value for parameter  0.7  is  1.298055332647558
Value for parameter  0.2  is  4.5908437119988035

In this example, we have seen that by passing a vaild parameter, which is different for different examples, we get the desired gamma() method solution, which is the gamma value of the parameter.

Example 2: Write a program to pass a value out of range from the gamma() function and display the output.

See the following code.

import math

x = 'b'


TypeError: must be real number, not str

In this example, we’ve seen that by passing a parameter which is not of number type, the function throws an error.

See also

Python erfc()

Python erf()

Python radians()

Python tanh()

Python sinh()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.