Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

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 113 views
1 answer 211 views
2 answers 265 views
3 answers 111 views
111 views asked Nov 12, 2024 by avibootz
...