AppDividend
Latest Code Tutorials

# Python lgamma() Function | Math.lgamma() in Python Example

Python lgamma() is an inbuilt method that is defined under the math module, which is used to find the natural logarithm of the absolute value, which is returned by the gamma function. For example, if x is passed as a parameter in lgamma function (lgamma(x)), it returns the natural log of the number. We can use the math module by importing it.

## Python lgamma()

Python lgamma function is one of the special Python Math functions used to calculate the Natural logarithm of Gamma function at the given argument.

The syntax for it would be import math; after importing, we use to call this function using the static object.

The gamma value is equal to factorial(x-1) of the number passed as a parameter. Hence lgamma() function returns the natural log of factorial(x-1) value.

Another important point to note is that math lgamma() function only takes parameter values of number type if any other type is passed; it returns TypeError.

### Syntax

`math.lgamma(var)`

Here var is the variable of which natural log of its gamma function we have to find.

### Parameters

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

### Return Value

It returns the natural log value of the gamma function value of the number in the float datatype.

See the following example.

```import math

var = 0.7
print(math.lgamma(var))```

### Example programs on lgamma() method in Python

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

See the following code.

```import math

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

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

#### Output

```Value for parameter  0.3  is  1.0957979948180752
Value for parameter  0.9  is  0.0663762397347431
Value for parameter  0.7  is  0.2608672465316669
Value for parameter  0.2  is  1.5240638224307843
```

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

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

See the following code.

```import math

x = 'b'
print(math.lgamma(x))
```

#### Output

```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.