How to use yield in Python

3 Answers

0 votes
def func(): 
    yield 1
    yield 2
    yield 3
  
for n in func():  
    print(n) 
    
    

'''
run:

1
2
3

'''

 



answered Jun 18, 2020 by avibootz
edited Jun 18, 2020 by avibootz
0 votes
def func():
    i = 1
    while True:
        yield i * i          
        i += 1
  
for n in func(): 
    if n > 200: 
       break    
    print(n) 



'''
run:

1
4
9
16
25
36
49
64
81
100
121
144
169
196

'''

 



answered Jun 18, 2020 by avibootz
0 votes
def func(): 
    print('A')
    yield 1
    print('B')
    yield 2
    print('C')
    yield 3
   
for n in func():  
    print(n) 
     
     
 
'''
run:
 
A
1
B
2
C
3
 
'''

 



answered Jun 18, 2020 by avibootz

Related questions

1 answer 139 views
1 answer 233 views
2 answers 290 views
3 answers 135 views
135 views asked Nov 12, 2024 by avibootz
...