整数的阶乘(英语:factorial)是一切小于及即是该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。
起首导入math模块,然后挪用factorial()函数来盘算阶乘。
相干引荐:《Python基础教程》
1 math.factorial(x)
import math value = math.factorial(x)
2. reduce函数
def factorial(n): return reduce(lambda x,y:x*y,[1]+range(1,n+1))
3. 递归完成
def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)
以上就是python怎样盘算n的阶乘的细致内容,更多请关注ki4网别的相干文章!