Site icon DataFlair

Generators in Python with Examples

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

def myfunction():
    i=1
    while(i<=5000):
        yield i
        i=i+1
   
#print(myfunction())
m=myfunction()
print(next(m)*10)
print(next(m)*10)
print(next(m)*10)
print(next(m)*10)
print(next(m)*10)
#print(list(m))

# Without Generator
# import sys
# def myfunction():
#     mylist=[]
#     i=1
#     while(i<=500):
#         mylist.append(i)
#         i=i+1
#     print("------------Size of List------------")  
#     print(sys.getsizeof(list))    
#     return mylist

# print(myfunction())      
# print("------------Size of List------------")  
# #print(sys.getsizeof(list))
# print("------------------------------------")  
# print([n*10 for n in myfunction()])

 

Exit mobile version