Exercise: Python

Questions for: Generators

What does the yield from statement do when used in a generator?
A:
It yields a single value from the generator
B:
It raises a TypeError
C:
It delegates part of the generator's operation to another generator or iterable
D:
It stops the generator and raises a StopIteration exception
Answer: C
yield from is used to delegate part of the generator's operation to another generator or iterable. It simplifies the syntax for generators that yield from another iterable. -PROGCODE-
def generator_delegator():
    yield from range(3)
    yield from 'abc'

result = list(generator_delegator())
print(result)  # Output: [0, 1, 2, 'a', 'b', 'c']
-PROGCODE-END-
How does the itertools.zip_longest() function behave when used with generators of unequal lengths?
A:
It raises a TypeError
B:
It stops when the shortest generator is exhausted
C:
It raises a ValueError
D:
It continues until the longest generator is exhausted, filling in missing values with a specified fillvalue
Answer: D
itertools.zip_longest() continues until the longest iterable is exhausted, filling in missing values with a specified fillvalue. -PROGCODE-
import itertools

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

zipped_iterable = itertools.zip_longest(gen1, gen2, fillvalue=None)

print(list(zipped_iterable))  # Output: [(1, 'a'), (2, 'b'), (3, None)]
-PROGCODE-END-
How does the itertools.count() function behave when used with a specified step value?
A:
It raises a TypeError
B:
It creates an infinite sequence of integers with the specified step value
C:
It stops after reaching the specified step value
D:
It raises a ValueError
Answer: B
itertools.count() creates an infinite sequence of integers, and if a step value is specified, it increments each subsequent element by that step value. -PROGCODE-
import itertools

count_by_twos = itertools.count(start=0, step=2)

result = [next(count_by_twos) for _ in range(5)]
print(result)  # Output: [0, 2, 4, 6, 8]
-PROGCODE-END-
How does the itertools.tee() function behave when used with a generator that contains mutable objects?
A:
It raises a TypeError
B:
It creates independent iterators, and changes in one iterator affect the others
C:
It creates independent iterators, and changes in one iterator do not affect the others
D:
It raises a ValueError
Answer: C
itertools.tee() creates independent iterators, and changes in one iterator do not affect the others. Each iterator maintains its own state. -PROGCODE-
import itertools

def generator_with_mutable():
    mutable_list = [1, 2, 3]
    for item in mutable_list:
        yield item

gen1, gen2 = itertools.tee(generator_with_mutable(), 2)

# Modify gen1 without affecting gen2
next(gen1)          
gen1_list = list(gen1)
gen2_list = list(gen2)

print(gen1_list)   # Output: [2, 3]
print(gen2_list)   # Output: [1, 2, 3]
-PROGCODE-END-
What is the purpose of the itertools.chain() function when used with generators?
A:
It concatenates multiple generators
B:
It applies a specified function cumulatively to the elements of a generator
C:
It interleaves values from different generators
D:
It yields elements from multiple generators in sequence
Answer: D
itertools.chain() is used to chain multiple iterables (including generators) together, producing a single iterable that yields elements from each iterable in sequence. -PROGCODE-
import itertools

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

chained_iterable = itertools.chain(gen1, gen2)

print(list(chained_iterable))  # Output: [1, 2, 3, 'a', 'b']
-PROGCODE-END-
Ad Slot (Above Pagination)
Quiz