Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

math.factorial

Calcola il fattoriale di un intero non negativo.

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

Sintassi

math.factorial(n, /)

Calcola il fattoriale di un intero non negativo.

Per n > 0 vale n! = n · (n−1)!, quindi 5 · 4 · 3 · 2 · 1 dà 120. Per n = 0 il prodotto vuoto vale 1.

Un intero negativo solleva ValueError e un float solleva TypeError.

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

Esempi

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

Risultato

120
1

Un intero negativo solleva ValueError e un float solleva TypeError.

Fonti