斐波拉契数列(Fibonacci)

来源:互联网 发布:ug4轴编程视频 编辑:程序博客网 时间:2024/05/31 20:51
列表生成式法:
def fib(max):    n, a, b = 0, 0, 1    while n < max:        print(b)        a, b = b, a + b        n = n + 1    return 'done'

生成器:generator法

def fib(max):    n, a, b = 0, 0, 1    while n < max:        yield b        a, b = b, a + b        n = n + 1    return 'done'
列表生成器输出:

g = fib(6)while True:     try:         x = next(g)         print('g:', x)     except StopIteration as e:         print('Generator return value:', e.value)         break



原创粉丝点击