Python Program for gamma() Function

In the previous article, we have discussed Python Program for tanh() Function
gamma() Function in Python:

The gamma function at a number is returned by the math.gamma() method.

Tip: Use the math.lgamma() method to find the log gamma value of a number.

Gamma Function’s Mathematical Formula:

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

Syntax:

math.gamma(x)

Parameters:

x: This is required. It is a number for which the gamma function must be found. If the number is a negative integer, a ValueError is returned. It throws a TypeError if it is not a number.

Return Value:

Returns a float value that represents the gamma function at x.

  • The gamma function returns the output if the number argument is a positive integer, positive or negative decimal.
  • The gamma function returns ValueError if the number argument is a negative integer.
  • If it’s not a number, the gamma function throws a TypeError.

Examples:

Example1:

Input:

Given first number = 6
Given second number = 2

Output:

The result after applying gamma() function on above given first number 6  =  120.0
The result after applying gamma() function on above given second number 2  =  1.0

Example2:

Input:

Given first number = -0.5
Given second number = 3

Output:

The result after applying gamma() function on above given first number -0.5  =  -3.544907701811032
The result after applying gamma() function on above given second number 3  =  2.0

Program for gamma() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Apply math.gamma() function to the given first number to get the gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.gamma() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, -1.5, 3, 0.4]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 6
# Apply math.gamma() function to the given first number to get the gamma value
# of a given number.
# Store it in another variable.
fnl_rslt = math.gamma(gvn_numb1)
# Print the above result
print("The result after applying gamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 2
print("The result after applying gamma() function on above given second number",
      gvn_numb2, " = ", math.gamma(gvn_numb2))
# Apply math.gamma() function to the given list element and print it.
print(
    "The result after applying gamma() function on given list element gvnlst[1] = ", math.gamma(gvn_lst[1]))

Output:

The result after applying gamma() function on above given first number 6  =  120.0
The result after applying gamma() function on above given second number 2  =  1.0
The result after applying gamma() function on given list element gvnlst[1] =  2.3632718012073544

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Apply math.gamma() function to the given first number to get the gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.gamma() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.gamma() function to the given first number to get the gamma value
# of a given number.
# Store it in another variable.
fnl_rslt = math.gamma(gvn_numb1)
# Print the above result
print("The result after applying gamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying gamma() function on above given second number",
      gvn_numb2, " = ", math.gamma(gvn_numb2))
# Apply math.gamma() function to the given list element and print it.
print(
    "The result after applying gamma() function on given list element gvnlst[2] = ", math.gamma(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 1 3 2 5 7
Enter some random number = 1.5
The result after applying gamma() function on above given first number 1.5 = 0.886226925452758
Enter some random number = 3
The result after applying gamma() function on above given second number 3.0 = 2.0
The result after applying gamma() function on given list element gvnlst[2] = 1.0

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.