Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

math.factorial

Multiplies all positive integers up to the supplied nonnegative integer.

Version: Python 3.14; repository image, production runtime not attested

Syntax

math.factorial(n, /)

Multiplies all positive integers up to the supplied nonnegative integer.

Zero has factorial one, the multiplicative identity for an empty product.

For positive n, the factorial equals n times the factorial of n−1. Multiplying 5 through 1 gives 120. For zero there are no factors, so the product is one. A negative integer raises ValueError and a float raises TypeError.

import math
assert math.factorial(5) == 5 * math.factorial(4) == 120
assert math.factorial(0) == math.factorial(1) == 1

Examples

import math
print(math.factorial(5))
print(math.factorial(0))

Result

120
1

For positive n, the factorial equals n times the factorial of n−1. Multiplying 5 through 1 gives 120. For zero there are no factors, so the product is one. A negative integer raises ValueError and a float raises TypeError.

Sources