[Python]reduce function & lambda function & factorial

from functools import reduce
i = int(input("input a number 1-10: "))
result = reduce(lambda a, b: a*b, [item for item in range(1,i+1)])
print(f‘factorial of {i+1} is {result}‘)

运行结果

input a number 1-10: 5
factorial of 6 is 120

相关推荐