Python gamma

The Python gamma math function is used to calculate the Gamma value of the given argument. In this section, we discuss how to use gamma function in Python Programming language with example. Before we step into the gamma function syntax, let us see the mathematical formula behind the gamma function:

Math.gamma(Number) = (Number – 1)!

It means the Python gamma function subtracts one from the Given number, and then it finds the factorial. For example, Math.gamma(6) = 5!

The syntax of the gamma Function in Python Programming Language is

math.gamma(Number);

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

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

Python gamma Function Example

The Python gamma function calculates the Gamma value of the given argument. In this example, we use the gamma function to find the gamma value of different data types and display the output.

# Python gamma Function

import math

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

print('Gamma() Function on Positive Number = %d' %math.gamma(2))
print('Gamma() Function on Positive Number = %d' %math.gamma(6))

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

print('Gamma() Function on Tuple Item = %d' %math.gamma(Tup[2]))
print('Gamma() Function on List Item = %d' %math.gamma(Lis[2]))

print('Gamma() Function on Multiple Number = %d' %math.gamma(10 + 20 - 25))

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