Python | math.factorial() Funktion
In Python enthält das Mathematikmodul eine Reihe von mathematischen Operationen, die mit dem Modul problemlos ausgeführt werden können. math.factorial()
Funktion gibt die Fakultät der gewünschten Zahl zurück.
Syntax: math.factorial (x) Parameter: x: Dies ist ein numerischer Ausdruck. Rückgabe: Fakultät der gewünschten Anzahl.
Code # 1:
import
math
x
=
5
(
"The factorial of 5 is : "
, end
=
"")
(math.factorial(x))
Ausgabe:
Die Fakultät von 5 ist: 120
Code # 2:
import
math
x
=
5
y
=
15
z
=
8
(
"The factorial of 5 is : "
, math.factorial(x))
(
"The factorial of 15 is : "
, math.factorial(y))
(
"The factorial of 8 is : "
, math.factorial(z))
Ausgabe:
Die Fakultät von 5 ist: 120 Die Fakultät von 15 ist: 1307674368000 Die Fakultät von 8 ist: 40320
Code 3: Löst ValueError aus, wenn x keine Ganzzahl ist
import
math
(
"math.factorial(13.7) : "
, math.factorial(
13.7
))
Ausgabe:
ValueError: factorial() akzeptiert nur ganzzahlige Werte