Exercise: Python

Questions for: Generators

What happens if a generator function contains a return value 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 value 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.throw(StopIteration) method differ from raise StopIteration when used with a generator?
A:
They have the same effect on the generator
B:
generator.throw(StopIteration) stops the generator and raises a StopIteration exception, while raise StopIteration is used outside the generator
C:
raise StopIteration stops the generator and raises a StopIteration exception, while generator.throw(StopIteration) is used outside the generator
D:
generator.throw(StopIteration) raises a ValueError
Answer: B
generator.throw(StopIteration) is used to stop the generator and raise a StopIteration exception inside the generator. raise StopIteration is used outside the generator to signal the end of the iteration.
How does the itertools.islice() function differ from itertools.takewhile() when used with generators?
A:
They are equivalent in functionality
B:
itertools.islice() allows for arbitrary slicing, while itertools.takewhile() stops based on a condition
C:
itertools.takewhile() allows for arbitrary slicing, while itertools.islice() stops based on a condition
D:
itertools.islice() modifies the original generator, while itertools.takewhile() creates a new generator
Answer: B
itertools.islice() allows for arbitrary slicing by specifying start, stop, and step parameters, while itertools.takewhile() stops yielding elements when a specified condition becomes false. -PROGCODE-
import itertools

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

sliced_iterable = itertools.islice(my_generator(), 2, 7)
takewhile_iterable = itertools.takewhile(lambda x: x < 5, my_generator())

print(list(sliced_iterable))      # Output: [2, 3, 4, 5, 6]
print(list(takewhile_iterable))   # Output: [0, 1, 2, 3, 4]
-PROGCODE-END-
What is the purpose of the itertools.accumulate() function when used with generators?
A:
It generates the cumulative sum of elements in the generator
B:
It filters elements based on a specified condition
C:
It stops the generator after one iteration
D:
It interleaves values from different generators
Answer: A
itertools.accumulate() generates the cumulative sum of elements in the generator, creating an iterator that yields the accumulated results.
How does the itertools.filterfalse() function differ from filter() when used with generators?
A:
They are equivalent in functionality
B:
itertools.filterfalse() filters elements based on a false condition, while filter() filters based on a true condition
C:
filter() filters elements based on a false condition, while itertools.filterfalse() filters based on a true condition
D:
itertools.filterfalse() raises a ValueError if the filtering function is not provided
Answer: B
itertools.filterfalse() filters elements from the generator for which the filtering function returns false, while filter() filters based on a true condition.
Ad Slot (Above Pagination)
Quiz