Python Programming - Tricky Questions - Discussion

Discussion Forum : Tricky Questions - General Questions (Q.No. 43)
43.
Consider the following Python code:
def my_generator():
    for i in range(5):
        yield i * 2

result = list(my_generator())
print(result)
What will be the output of this code?
[0, 2, 4, 6, 8]
[0, 1, 4, 9, 16]
[0, 2, 4, 8, 16]
This code will result in an error.
Answer: Option
Explanation:
The generator yields values multiplied by 2 in the range [0, 2, 4, 6, 8].
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.