python中计算阶乘的math.factorial()方法
迪丽瓦拉
2024-06-02 12:41:01
0

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全栈
原文:python math.factorial()方法,计算阶乘

相关内容