Python lgamma

The Python lgamma math function is used to calculate the Natural logarithm of Gamma function at the given argument. Let us discuss how to use lgamma function in Python.

Before we step into the Python lgamma syntax, let us see the mathematical formula behind the logarithm lgamma function:

Math.lgamma(Number) = math.log( (Number – 1)! )

It means,

  • First, the lgamma function implements the gamma function on the given number. Please refer to the gamma function article for further reference.
  • Next, the lgamma function calculates the Natural logarithm of the gamma function output. Please refer to the log function article to understand the Python math.log() function.

For example, Math.gamma(6) = 5! = 120

Math.lgamma(6) = math.log(120) = 4.79

Syntax of a Python lgamma Function

The syntax of the lgamma Function in Python Programming Language is


Number: It can be a number or a valid numerical expression.

  • If the number argument is a positive integer, positive or negative decimals, then lgamma function returns the output.
  • If the number argument is Negative integer, lgamma function returns ValueError.
  • And if it is not a number, lgamma Math function returns TypeError.

Python lgamma Function Example

The Python lgamma function calculates the Natural logarithm of gamma function at the given argument. In this example, we use lgamma to find the same for different data types and display the output.

# Python lgamma Function

import math

Tup = (1, 2, 8, 4 , 5) # Tuple Declaration
Lis = [8, 3, 9, 5 , 7] # List Declaration

print('LGamma() Function on Positive Number = %.2f' %math.lgamma(2))
print('LGamma() Function on Positive Number = %.2f' %math.lgamma(6))

print('LGamma() Function on Positive Decimal = %.2f' %math.lgamma(4.5))
print('LGamma() Function on Negative Decimal = %.2f' %math.lgamma(-2.78))

print('LGamma() Function on Tuple Item = %.2f' %math.lgamma(Tup[2]))
print('LGamma() Function on List Item = %.2f' %math.lgamma(Lis[2]))

print('LGamma() Function on Multiple Number = %.2f' %math.lgamma(10 + 20 - 25))

print('LGamma() Function on String Value = ', math.lgamma('3'))
Python LGAMMA Function
  1. In the beginning, We declared List and Tuple with some random values.
  2. Within the first two statements, We passed the Positive integers as the lgamma Function argument. From the above screenshot, observe that lgamma Function is returning output.
  3. Within the next two statements, We passed both the Positive and negative decimals as the lgamma Function argument. As we said before, the lgamma Function is returning the output.
  4. Next two statements, We used the Tuple and List items as the argument. If you observe the above screenshot, the lgamma function is working perfectly on them.
  5. Next, we assigned multiple values as the argument, and the lgamma Function worked without any issue.
  6. Then, We tried lgamma Function on the String value. As we said before, it returns TypeError as output
  7. Within the Python IDE, we passed a negative integer as an argument for lgamma Function. As you can, it is returning ValueError as output.