+ 3

Can you please explain the output of this code?

def fun(): for x in range(10,0,-1): yield(x) x = fun() for a in range(6): next(x) print(next(x))

14th Mar 2020, 1:07 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
1 Answer
+ 6
fun() is a generator function. It yields values from 10 to 1. x initializes the generator. next(x) gets the following value (which comes from yield). The for loop exhausts 6 values (10 9 8 7 6 5) Then the next value 4 is printed. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2462/
14th Mar 2020, 1:19 PM
Tibor Santa
Tibor Santa - avatar