What is the expected behavior of the following code?
It will -
What is the expected behavior of the following code?
It will -
B
Technically all choices are wrong since the code will cause an IndentationError just like some other question here but B if lined up correctly.
if indentation was correct: B. print 1 2
There is no correct answer because of IndentationError. Assume this code is right indent the answer will be B. print 1 2
// This is equivalent to for i in range(1,3): print(i, end=' ') // Any function that includes "yield" is called a generator and it works like range(), so that you can use them both in a loop like "for", but there are differences, e.g. you can use a generator inside next() but you cannot use range() inside next() because range() is stateless
If the indentation of Line 2 & 3 is correct, then 1, 2 is the correct answer.
Otherwise it will cause a runtime exception.
I run it but I don't understand why yield return 1,2
With f(2), n = 2, so the range will be range(1, {2+1}) or range(1,3). The iterations of i would then be 1 and then 2 with yield sending each iteration back to "for i in f(2)". Hope this explains it.