Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,936 questions

14,844 answers

573 users

How to create generator function using yield in Python

2 Answers

0 votes
def generator():
    n = 1
    print('1')
    yield n

    n += 1
    print('2')
    yield n

    n += 1
    print('3')
    yield n
 

g = generator()

next(g) 
next(g)
next(g)



'''
run:
    
1
2
3

'''

 




answered May 12 by avibootz
0 votes
def generator():
    n = 1
    print('print 1')
    yield n

    n += 1
    print('print 2')
    yield n

    n += 1
    print('print 3')
    yield n
 

for s in generator():
    print(s)    



'''
run:
    
print 1
1
print 2
2
print 3
3

'''

 




answered May 12 by avibootz
...