Python Tricks(二十)—— 阶乘的极简实现

使用 reduce

# 比如计算 9 的阶乘
>> reduce(lambda x, y: x*y, range(1, 9+1))
362880

当然这里的 reduce 直接返回具体的数值,是 Python 2.7 中的用法。Python 3.x 环境,对 reduce 进行较大的改变,应当注意其中的转变。

原文地址:https://www.cnblogs.com/mtcnn/p/9423907.html