Exercise: Python

Questions for: Generators

How does the itertools.zip_longest() function differ from zip() when used with generators?
A:
They are equivalent in functionality
B:
itertools.zip_longest() fills in missing values with None, while zip() stops when the shortest iterable is exhausted
C:
zip() fills in missing values with None, while itertools.zip_longest() stops when the shortest iterable is exhausted
D:
itertools.zip_longest() raises a ValueError if iterables are of different lengths
Answer: B
itertools.zip_longest() fills in missing values with None for the shorter iterables, while zip() stops when the shortest iterable is exhausted. -PROGCODE-
import itertools

gen1 = (1, 2, 3)
gen2 = ('a', 'b')

zipped = zip(gen1, gen2)
zip_longest = itertools.zip_longest(gen1, gen2)

print(list(zipped))       # Output: [(1, 'a'), (2, 'b')]
print(list(zip_longest))  # Output: [(1, 'a'), (2, 'b'), (3, None)]
-PROGCODE-END-
What happens if the generator.throw(ValueError, "message") method is called with a non-existent exception type?
A:
It raises a TypeError
B:
It raises the specified exception with the provided message
C:
It has no effect on the running generator
D:
It raises a GeneratorExit exception
Answer: C
If the specified exception type does not exist, generator.throw() has no effect on the running generator.
What is the purpose of the itertools.compress() function when used with generators?
A:
It compresses the generator into a ZIP file
B:
It filters elements based on a boolean mask
C:
It stops the generator after one iteration
D:
It interleaves values from different generators
Answer: B
itertools.compress() filters elements from the generator based on a boolean mask, which is provided as the second iterable argument.
How does the itertools.islice() function differ from using generator[start:end] slicing?
A:
They are equivalent in functionality
B:
itertools.islice() can be used for any iterable, while slicing is specific to generators
C:
generator[start:end] can be used for any iterable, while itertools.islice() is specific to generators
D:
itertools.islice() modifies the original generator, while slicing creates a new generator
Answer: B
itertools.islice() can be used for any iterable, not just generators, while slicing with generator[start:end] is specific to generators and creates a new generator. -PROGCODE-
import itertools

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

# Using itertools.islice() for slicing
sliced_iterable = itertools.islice(my_generator(), 2, 7)

for value in sliced_iterable:
    print(value)
-PROGCODE-END-
What is the purpose of the itertools.cycle() function when used with generators?
A:
It creates an infinite sequence of repeated values from the generator
B:
It generates a single iteration from the generator
C:
It stops the generator after one complete cycle
D:
It shuffles the values produced by the generator
Answer: A
itertools.cycle() creates an infinite sequence of repeated values. When used with a generator, it continuously cycles through the values produced by the generator. -PROGCODE-
import itertools

def my_generator():
    yield 1
    yield 2
    yield 3

cycled_generator = itertools.cycle(my_generator())

for _ in range(10):
    print(next(cycled_generator))
-PROGCODE-END-
Ad Slot (Above Pagination)
Quiz