yield statement works with for loop
def gensquares(N):
for i in range(N):
yield i ** 2 # resume here later
for i in gensquares(5): # resume the function
print i, ':', # print last yielded value
x = gensquares(10)
x.next()
x.next()
x.next()
def buildsquares(n):
res = []
for i in range(n): res.append(i**2)
return res
for x in buildsquares(5): print x, ':',
for x in [n**2 for n in range(5)]:
print x, ':',
for x in map((lambda x:x**2), range(5)):
print x, ':',
Related examples in the same category