python 连续数字的符号

python symbol for consecutive numbers

我偶然发现了一个需要使用 "Unknown formula" 的问题。

此处引用:http://www.murderousmaths.co.uk/books/unknownform.htm

我四处寻找,没有找到与“!”等价的 python 符号。以作者使用它的方式。

# Example of authors use of '!'
5! == 5x4x3x2x1

我知道我可以使用循环来创建它,就像这样 post:Sum consecutive numbers in a list. Python

但我希望这成为一个学习的时刻。


编辑

关于阶乘 (Function for Factorial in Python) 有一个很棒的话题,但我更喜欢下面提供的解决方案答案。非常清晰简洁。

这是一个叫做 factorial, and is covered in depth in another question 的数学函数。

最简单的方法是:

import math
math.factorial(5)

函数式方法:

from functools import reduce
import operator
answer = reduce(operator.mul, range(1, 6))

循环方法:

answer = 1
for i in range(5):
    answer *= i+1

.