代码
#iter next
# list = [1, 2, 3]
# it = iter(list)
# print(next(it))
# print(next(it))
# print(next(it))
# # print(next(it)) #输出结束,抛出错误
#生成器
# #之前使用过的range函数
# for i in range(10,20,2):
# print(i)
#
# for i in range(10,20,0.5): #抛出错误,range -- 步长只能为整数 (TypeError: 'float' object cannot be interpreted as an integer)
# print(i)
#那么我们自己写一个迭代器实现小数的增长
def frange(start, stop, step):
x = start
while x <= stop:
yield (x) #调用一次后暂停,并记录当前位置,等待下次调用
x += step
for i in frange(10, 20, 0.5):
print(i)
运行
...