{"id":24409,"date":"2021-10-14T09:46:16","date_gmt":"2021-10-14T04:16:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=24409"},"modified":"2021-11-05T21:07:49","modified_gmt":"2021-11-05T15:37:49","slug":"python-program-for-gamma-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-gamma-function\/","title":{"rendered":"Python Program for gamma() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for tanh() Function<\/a> The gamma function at a number is returned by the math.gamma() method.<\/p>\n Tip:<\/strong> Use the math.lgamma() method to find the log gamma value of a number.<\/p>\n Gamma Function’s Mathematical Formula:<\/strong><\/p>\n Math.gamma(Number) = (Number \u2013 1)!<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x:<\/strong> 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.<\/p>\n Return Value:<\/strong><\/p>\n Returns a float value that represents the gamma function at x.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples<\/a> well explained step by step.<\/p>\n 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 \u2013 1)! Syntax: math.gamma(x) Parameters: x: …<\/p>\n
\ngamma() Function in Python:<\/strong><\/p>\nmath.gamma(x)<\/pre>\n
\n
Given first number = 6\r\nGiven second number = 2<\/pre>\n
The result after applying gamma() function on above given first number 6 = 120.0\r\nThe result after applying gamma() function on above given second number 2 = 1.0<\/pre>\n
Given first number = -0.5\r\nGiven second number = 3<\/pre>\n
The result after applying gamma() function on above given first number -0.5 = -3.544907701811032\r\nThe result after applying gamma() function on above given second number 3 = 2.0<\/pre>\n
Program for gamma() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [2, -1.5, 3, 0.4]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 6\r\n# Apply math.gamma() function to the given first number to get the gamma value\r\n# of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.gamma(gvn_numb1)\r\n# Print the above result\r\nprint(\"The result after applying gamma() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = 2\r\nprint(\"The result after applying gamma() function on above given second number\",\r\n gvn_numb2, \" = \", math.gamma(gvn_numb2))\r\n# Apply math.gamma() function to the given list element and print it.\r\nprint(\r\n \"The result after applying gamma() function on given list element gvnlst[1] = \", math.gamma(gvn_lst[1]))\r\n<\/pre>\n
The result after applying gamma() function on above given first number 6 = 120.0\r\nThe result after applying gamma() function on above given second number 2 = 1.0\r\nThe result after applying gamma() function on given list element gvnlst[1] = 2.3632718012073544<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the first number as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply math.gamma() function to the given first number to get the gamma value\r\n# of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.gamma(gvn_numb1)\r\n# Print the above result\r\nprint(\"The result after applying gamma() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\nprint(\"The result after applying gamma() function on above given second number\",\r\n gvn_numb2, \" = \", math.gamma(gvn_numb2))\r\n# Apply math.gamma() function to the given list element and print it.\r\nprint(\r\n \"The result after applying gamma() function on given list element gvnlst[2] = \", math.gamma(gvn_lst[2]))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 3 2 5 7\r\nEnter some random number = 1.5\r\nThe result after applying gamma() function on above given first number 1.5 = 0.886226925452758\r\nEnter some random number = 3\r\nThe result after applying gamma() function on above given second number 3.0 = 2.0\r\nThe result after applying gamma() function on given list element gvnlst[2] = 1.0<\/pre>\n
\n