Python 3.2+Numbers & mathfunction
math.gamma
special functions
The gamma function: a generalization of factorial (gamma(n) = (n−1)!).
Syntax
math.gamma(...)
For whole numbers reach for math.factorial instead: it returns an exact integer, while gamma always gives a float with rounding error baked in. The function has poles at zero and at every negative integer (ValueError there), and it grows so fast that somewhere past 172 the result no longer fits in a float and you get OverflowError.
Examples
import math
print(math.gamma(5)) # → 24.0
print(math.gamma(1)) # → 1.0
print(round(math.gamma(0.5), 6)) # → 1.772454
print(math.gamma(6) == math.factorial(5)) # → True
print(math.gamma(0)) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy