[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

 

posted @ 2020-06-06 20:57  profesor  阅读(122)  评论(0编辑  收藏  举报