Python math.factorial() 方法
示例
查找一个数的阶乘
#导入 math 库
import math
#返回一个数的阶乘
print(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))
自己动手试一试 »
定义和用法
math.factorial()
方法返回一个数的阶乘。
注意:此方法只接受正整数。
一个数的阶乘是所有从该数到 1 的整数的乘积之和。例如,6 的阶乘是 6 x 5 x 4 x 3 x 2 x 1 = 720
语法
math.factorial(x)
参数值
参数 | 描述 |
---|---|
x | 必需。一个正整数。如果数字是负数,或不是整数,则返回 ValueError。如果值不是数字,则返回 TypeError |
技术详情
返回值 | 一个正 int 值 |
---|---|
Python 版本 | 2.6 |