Exercise: Python

Questions for: Generators

What happens if a generator function contains a return statement followed by a yield statement?
A:
It raises a GeneratorExit exception
B:
It raises a StopIteration exception
C:
It returns the specified value and stops the generator
D:
It is a syntax error; return and yield cannot be used together
Answer: C
If a generator function contains both return and yield statements, it returns the specified value and stops the generator. -PROGCODE-
def my_generator():
    return 42
    yield  # This statement is never reached

# Example usage
gen = my_generator()
result = next(gen, None)
print(result)  # Output: 42
-PROGCODE-END-
How does the generator.close() method differ from generator.throw(GeneratorExit)?
A:
They are equivalent in functionality
B:
generator.close() raises a GeneratorExit exception, while generator.throw(GeneratorExit) terminates the generator
C:
generator.throw(GeneratorExit) raises a GeneratorExit exception, while generator.close() terminates the generator
D:
generator.throw(GeneratorExit) and generator.close() are not valid methods
Answer: B
generator.close() raises a GeneratorExit exception to signal that the generator should be closed. generator.throw(GeneratorExit) is not a common approach for closing generators.
What is the purpose of the itertools.takewhile() function when used with generators?
A:
It stops the generator after one iteration
B:
It allows for arbitrary slicing of the generator
C:
It filters elements based on a specified condition until the condition becomes false
D:
It interleaves values from different generators
Answer: C
itertools.takewhile() yields elements from the generator as long as the specified condition is true, stopping when the condition becomes false. -PROGCODE-
import itertools

def my_generator():
    for i in range(10):
        yield i

filtered_iterable = itertools.takewhile(lambda x: x < 5, my_generator())

print(list(filtered_iterable))  # Output: [0, 1, 2, 3, 4]
-PROGCODE-END-
How does the itertools.tee() function differ from itertools.cycle() when used with generators?
A:
They are equivalent in functionality
B:
itertools.tee() creates multiple independent iterators from a single generator, while itertools.cycle() repeats the elements of a generator indefinitely
C:
itertools.cycle() creates multiple independent iterators from a single generator, while itertools.tee() repeats the elements of a generator indefinitely
D:
itertools.tee() raises a ValueError when used with generators
Answer: B
itertools.tee() creates multiple independent iterators from a single generator, allowing each iterator to consume the elements separately. itertools.cycle() repeats the elements of a generator indefinitely.
How does the itertools.chain.from_iterable() function differ from itertools.chain() when used with generators?
A:
They are equivalent in functionality
B:
itertools.chain.from_iterable() flattens nested iterables produced by the generator, while itertools.chain() concatenates multiple generators
C:
itertools.chain() flattens nested iterables produced by the generator, while itertools.chain.from_iterable() concatenates multiple generators
D:
itertools.chain.from_iterable() raises a ValueError when used with generators
Answer: B
itertools.chain.from_iterable() is designed to handle nested iterables, flattening them into a single sequence. itertools.chain() concatenates multiple generators into a single sequence. -PROGCODE-
import itertools

nested_generator = ([1, 2, 3], [4, 5, 6], [7, 8, 9])
flattened_iterable = itertools.chain.from_iterable(nested_generator)
concatenated_iterable = itertools.chain(*nested_generator)

print(list(flattened_iterable))      # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(concatenated_iterable))   # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
-PROGCODE-END-
Ad Slot (Above Pagination)
Quiz