Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

math.factorial

Calcula o fatorial de um inteiro não negativo.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

math.factorial(n, /)

Calcula o fatorial de um inteiro não negativo.

Para n > 0 vale n! = n · (n−1)!, pelo que 5 · 4 · 3 · 2 · 1 dá 120. Para n = 0 o produto vazio vale 1.

Um inteiro negativo gera ValueError e um float gera TypeError.

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

Exemplos

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

Resultado

120
1

Um inteiro negativo gera ValueError e um float gera TypeError.

Fontes