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

24 lines
342 B
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
生成器 - 生成器语法
Version: 0.1
Author: 骆昊
Date: 2018-03-21
"""
seq = [x * x for x in range(10)]
print(seq)
gen = (x * x for x in range(10))
print(gen)
for x in gen:
2019-05-03 21:17:36 +08:00
print(x)
2018-04-27 00:00:22 +08:00
num = 10
gen = (x ** y for x, y in zip(range(1, num), range(num - 1, 0, -1)))
print(gen)
n = 1
while n < num:
2019-05-03 21:17:36 +08:00
print(next(gen))
n += 1