Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / Python

math.factorial

Calcula el factorial de un entero no negativo.

Versión: Python 3.14; repository image, production runtime not attested

Sintaxis

math.factorial(n, /)

Calcula el factorial de un entero no negativo.

Para n > 0 se cumple n! = n · (n−1)!, así que 5 · 4 · 3 · 2 · 1 da 120. Para n = 0 el producto vacío vale 1.

Un entero negativo provoca ValueError y un float provoca TypeError.

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

Ejemplos

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

Resultado

120
1

Un entero negativo provoca ValueError y un float provoca TypeError.

Fuentes