python math.factorial()方法,计算阶乘

math.factorial()方法

python math模块的factorial()方法,可以用于求取参数指定的值的阶乘。什么阶乘呢?一个正整数的阶乘便是所有小于及等于该数的正整数的乘积。


factorial()语法

factorial(x, /)

python源码中对factorial()方法的介绍:

    Find x!.
    
    Raise a ValueError if x is negative or non-integral.

提示:Find x!中的“!”是阶乘的符号。


参数

参数描述
x必须参数,而且必须是正整数,如果是负数或非整数,python将抛出ValueError。

提示:0的阶乘等于1。


返回值

参数x的阶乘,python整型int值。


math.factorial()实例代码

>>> import math
>>> math.factorial(5)
120
>>> math.factorial(0)
1

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.