Python-100-Days/Day01-15/code/Day13/generator2.py

20 lines
232 B
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
生成器 - 使用yield关键字
Version: 0.1
Author: 骆昊
Date: 2018-03-21
"""
def fib(num):
2019-05-03 21:17:36 +08:00
n, a, b = 0, 0, 1
while n < num:
yield b
a, b = b, a + b
n += 1
2018-04-27 00:00:22 +08:00
for x in fib(20):
2019-05-03 21:17:36 +08:00
print(x)