Python Glossary Русский

all Python 3mathterm

math.factorial

combinatorics

Returns the factorial of a non-negative integer. factorial(5) → 120. A negative number raises ValueError.

Syntax

math.factorial(n)

Floats with integral values used to be accepted, but that was removed in Python 3.10, so a float argument now raises TypeError and you must convert division results with int() yourself. The value grows faster than any exponential, so never build combinations or permutations out of factorials by hand: math.comb and math.perm give the same answer orders of magnitude faster and without thousand-digit intermediates.

Examples

import math
print(math.factorial(0))  # → 1
print(math.factorial(5))  # → 120
print(math.factorial(10)) # → 3628800
print(math.factorial(20)) # → 2432902008176640000
print(math.factorial(1))  # → 1
print(math.factorial(6))  # → 720

See also

Open in the full glossarydocs.python.orgReport an inaccuracy