Python math.gamma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.2+

Definition

math.gamma(x) returns the continuous Gamma function at x as a float, added in Python 3.2.

For any positive integer n, math.gamma(n) equals (n - 1)!, and for non-integers it interpolates smoothly—for example math.gamma(0.5) equals sqrt(math.pi). Zero and negative integers are poles that raise ValueError.

math.gamma() is a function in Python's standard math module: import it with import math and call math.gamma(...), or import the name directly with from math import gamma.

Syntax

Import math and pass a non-pole real number.

python
import math

val = math.gamma(x)
CallWhat it does
math.gamma(5.0)Returns 24.0 (4!).
math.gamma(1.0)Returns 1.0 (0!).
math.gamma(0)Raises ValueError: pole at zero.
math.gamma(172)Raises OverflowError: exceeds float range.

Because gamma(x) grows like factorial and overflows past x ~ 171.6, compute ratios or large values in log space with math.lgamma().

Function signatures
python
math.gamma(x, /)
# added in Python 3.2; returns float Gamma(x)

Parameters

ParameterDescription
xRequired positional real number (int or float), except 0, negative integers, and -inf.

Return Value

Return ValueDescription
floatValue of Gamma(x); inf for inf and nan for nan.

Python Examples

Example 1 — Verify math.gamma(n) Equals float(factorial(n - 1))

Compare math.gamma(n) against math.factorial() across integers 1 through 6.

python
import math

for n in range(1, 7):
    g = math.gamma(n)
    f = float(math.factorial(n - 1))
    print(f"n={n}: gamma={g:>5.1f}  (n-1)!={f:>5.1f}  equal={g == f}")

How It Works

  1. Shifted by one. For positive integers, Gamma evaluates (n - 1)! rather than n!.
  2. Exact float integers. Up through n = 23, factorials fit exactly in IEEE 754 floats, so == holds.
  3. Zero factorial is one. Both gamma(1) and gamma(2) equal 1.0.

Example 2 — Verify gamma(0.5)**2 == pi and Non-Integer Negatives

Check Euler's half-integer identity and compute gamma(1.5) and gamma(-0.5).

python
import math

g_half = math.gamma(0.5)
print("gamma(0.5)**2 isclose pi:", math.isclose(g_half ** 2, math.pi))
print(f"gamma(1.5)  = {math.gamma(1.5):.6f}")
print(f"gamma(-0.5) = {math.gamma(-0.5):.6f}")

How It Works

  1. Square root of pi. gamma(0.5) equals sqrt(pi), verified by squaring with math.isclose().
  2. Recurrence relation. gamma(1.5) equals 0.5 * gamma(0.5) (0.886227).
  3. Negative fractions work. Unlike negative integers, non-integer -0.5 evaluates cleanly to -2 * sqrt(pi).

Example 3 — Catch ValueError at Negative Poles and OverflowError at 172

Handle zero, negative integers, and large inputs exceeding float max.

python
import math

for pole in [0, -2]:
    try:
        math.gamma(pole)
    except ValueError:
        print(f"gamma({pole}) raises ValueError")

try:
    math.gamma(172)
except OverflowError:
    print("gamma(172) raises OverflowError")

How It Works

  1. Poles at zero and negative ints. Passing 0 or -2 hits vertical asymptotes and raises ValueError.
  2. Float overflow past 171. Because 171! overflows double precision, gamma(172) raises OverflowError.
  3. Use lgamma for huge n. Switch to math.lgamma() when working with large inputs.

Frequently Asked Questions

Python 3.2.
By standard mathematical convention, Gamma(n) equals (n - 1)!, so gamma(5) is 4! = 24.0.
Yes for non-integers like -0.5 or -1.5. Zero and negative integers (-1, -2, ...) are poles and raise ValueError.
Around x = 171.624 for IEEE 754 binary64 floats; math.gamma(172) raises OverflowError.
Use math.lgamma(1000), which returns ln(Gamma(1000)) as a normal finite float.

Next: math.gcd()

Return the greatest common divisor of integer arguments.

gcd() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful