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()])

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *