python generater yeild

#list
L = [x * x for x in range(10)]

#generrater
g = (x * x for x in range(10))

g
Out[4]: <generator object <genexpr> at 0x7f3d143048e0>

L
Out[5]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

for i in g:
    print(i)
1
4
9
16
25
36
49
64
81
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'

c=fib(6)

for k in c :
    print(k)
1
1
2
3
5
8
posted @ 2018-10-28 11:26  luoganttcc  阅读(106)  评论(0编辑  收藏  举报