Generators
Creating a Generator
# A simple generator function
def myGen():
n = 1
print('This is printed first')
# Generator function contains yield statements
yield n
n += 1
print('This is printed second')
yield n
n += 1
print('This is printed at last')
yield n
# Using for loop
for item in myGen():
print(item)Advantages of using generator
Last updated