#!/usr/bin/env python3 # -*- coding: utf-8 ‘‘‘循环(loop) 迭代(iterate) 递归(recursion) 遍历(traversal) 斐波那契数列 ‘‘‘ a, b = 0, 1 for i in range(4): a, b = b, a+b print(a) def fibs(n): ‘‘‘这是一个Fibonacci 序列 ‘‘‘ result = [0, 1] for i in range(n-2): result.append(result[-2] + result[-1]) return result if __name__ == "__main__": lst = fibs(10) print(lst) def fibs_tigui(n): if n == 0: return 0 elif n == 1: return 1 else: return fibs_tigui(n-1) + fibs_tigui(n-2) if __name__ == "__main__": f = fibs_tigui(10) print(f) ‘‘‘递归优化 ‘‘‘ meno = {0:0, 1:1} #初始化 def fib_better(n): if not n in meno: #如果不在初始化范围内 meno[n] = fib_better(n-1) + fib_better(n-2) return meno[n] if __name__ == "__main__": f_b = fib_better(10) print(f)