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 the gamma function in this Programming language with an example. Before we step into the Python gamma function syntax, let us see the mathematical formula behind this:

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

It means the 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


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

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

Python gamma Function Example

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

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 List and Tuple with some random values.
  2. We passed the Positive integers within the first two statements as the argument. The above screenshot shows that the Python gamma Function is returning output.
  3. We passed both positive and negative decimals within the next two statements. As we said before, it is returning the output.
  4. Next two statements, we used the Tuple and List items as the math functions argument. If you observe the above screenshot, it is working correctly on them.
  5. Next statement, We assigned multiple values as the argument, and it worked without any issues.
  6. We tried it 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. As you can, it is returning ValueError as output.