Python Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 79)
79.
How can you define a generator function that yields squares of numbers up to a given limit?
def square_generator(limit):
    for i in range(limit):
        yield i**2
generator square_generator(limit):
    for i in range(limit):
        return i**2
def square_generator(limit):
    return (i**2 for i in range(limit))
generator square_generator(limit):
    return [i**2 for i in range(limit)]
Answer: Option
Explanation:
The correct definition for a generator function that yields squares of numbers up to a given limit is shown in option A.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.