python如何求阶乘
python阶乘的方法:1、使用普通的for循环;2、使用【reduce()】函数,代码为【num = reduce(lambda x,y:x*y,range(1,7))】;3、使用【factorial()】函数;4、递归调用方法。
相关学习推荐:python-tutorials.html" target="_blank">python教程
python阶乘的方法:
第一种:普通的for循环
a = int(input('please inputer a integer:'))num = 1if a <p>第二种:reduce()函数</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><pre class="brush:php;toolbar:false">#从functools中调用reduce()函数from functools import reduce#使用lambda,匿名函数,迭代num = reduce(lambda x,y:x*y,range(1,7))print(num)
第三种:factorial()函数
import mathvalue = math.factorial(6)print(value)
第四种:递归调用
def num(n): if n == 0: return 1 else: return n * num(n - 1)print(num(6)