編程新手
- def factorial(x):
- if x == 0:
- return 1
- else:
- return x * factorial(x - 1)
- print factorial(6)
一年編程經驗(Python)
- def Factorial(x):
- res = 1
- for i in xrange(2, x + 1):
- res *= i
- return res
- print Factorial(6)
更懶的Python程序員
- f = lambda x: x and x * f(x - 1) or 1
- print f(6)
Python 專家
- fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)
- print fact(6)
Python 黑客
- import sys
- @tailcall
- def fact(x, acc=1):
- if x: return fact(x.__sub__(1), acc.__mul__(x))
- return acc
- sys.stdout.write(str(fact(6)) + '\n')
最近搜python相關問題的時候,看到這個挺有意思的文章,在這分享給大家,希望大家學習python的時候有所領悟