Exercise: Python

Questions for: Generators

How does the itertools.cycle() function differ from using a loop with yield from to repeat elements of a generator?
A:
They are equivalent in functionality
B:
itertools.cycle() raises a ValueError when used with generators
C:
itertools.cycle() continues indefinitely, while a loop with yield from stops when the generator is exhausted
D:
A loop with yield from raises a StopIteration exception
Answer: C
itertools.cycle() continues indefinitely, repeating elements of a generator. A loop with yield from stops when the generator is exhausted. -PROGCODE-
def repeat_elements():
    while True:
        yield from [1, 2, 3]

gen_cycle = itertools.cycle([1, 2, 3])

# Equivalent behavior
result_cycle = [next(gen_cycle) for _ in range(10)]
result_loop = [next(repeat_elements()) for _ in range(10)]

print(result_cycle)  # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
print(result_loop)   # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
-PROGCODE-END-
What happens if the generator.close() method is called on a generator?
A:
It stops the generator and raises a StopIteration exception
B:
It raises a GeneratorExit exception inside the generator
C:
It has no effect on the running generator
D:
It raises a ValueError
Answer: C
generator.close() has no effect on the running generator. It is typically used to signal that the generator can be closed.
How does the itertools.dropwhile() function differ from itertools.takewhile() when used with generators?
A:
They are equivalent in functionality
B:
itertools.dropwhile() skips elements until a specified condition becomes false, while itertools.takewhile() yields elements as long as the condition is true
C:
itertools.takewhile() skips elements until a specified condition becomes false, while itertools.dropwhile() yields elements as long as the condition is true
D:
itertools.dropwhile() and itertools.takewhile() both raise a ValueError
Answer: B
itertools.dropwhile() skips elements until a specified condition becomes false, while itertools.takewhile() yields elements as long as the condition is true.
How does the itertools.cycle() function behave when used with an empty generator?
A:
It raises a ValueError
B:
It returns an empty iterable
C:
It raises a StopIteration exception
D:
It enters an infinite loop without producing any elements
Answer: C
itertools.cycle() raises a StopIteration exception when used with an empty generator, as there are no elements to repeat.
How does the itertools.islice() function differ from using slice() with a generator?
A:
They are equivalent in functionality
B:
itertools.islice() allows for arbitrary slicing of a generator, while slice() is used for indexing
C:
slice() allows for arbitrary slicing of a generator, while itertools.islice() is used for indexing
D:
itertools.islice() and slice() both raise a ValueError
Answer: B
itertools.islice() is specifically designed for slicing iterables, including generators. slice() is used for indexing sequences, not for creating a sliced iterable.
Ad Slot (Above Pagination)
Quiz