Practice: yield Keyword
Q1. Countdown generator
Write a generator function countdown(n) that yields n, n-1, ... 1, then print its values with a for loop for n = 3.
Goal: Write a simple generator.
Q2. Even numbers
Write a generator evens(limit) that yields the even numbers from 2 up to limit, then print list(evens(10)).
Goal: Use yield inside a for loop.
Q3. Fibonacci stream
Write a generator fib() that yields the Fibonacci numbers forever. Print the first 8 using a loop with a counter.
Goal: Create an infinite generator.
Q4. Squares with yield from
Write a generator that yields the squares of 1 to 3 and then the squares of 7 to 8, using yield from with two helper generators. Print the list.
Goal: Delegate with yield from.
Q5. Filter words
Write a generator long_words(words, size) that yields only words with at least size letters. Print the list for ['sun','planet','sky','galaxy'] and size 5.
Goal: Filter items lazily.
Q6. Running total
Write a generator running_total(numbers) that yields the total so far. Print the list for [1, 2, 3, 4].
Goal: Keep state inside a generator.
Q7. Order of prints
What does this code print?
def g():
print("A")
yield 1
print("B")
yield 2
it = g()
print("made")
print(next(it))Goal: Understand lazy execution.
Q8. Two next calls
What is printed?
def g():
yield "x"
yield "y"
yield "z"
it = g()
next(it)
print(next(it))Goal: Trace next() calls.
Q9. Generator used twice
What is printed?
gen = (n * 2 for n in range(3))
print(list(gen))
print(list(gen))Goal: Know that generators are single-use.
Q10. Sum of generator
What is printed?
def small():
yield 1
yield 2
yield 3
print(sum(small()))Goal: Consume a generator with sum.
Q11. yield from string
What is printed?
def f():
yield from "hi"
yield "!"
print("".join(f()))Goal: Predict yield from output.
Q12. Return inside generator
What is printed?
def f():
yield 1
return
yield 2
print(list(f()))Goal: Know how return stops a generator.
Q13. Fix the return
This function should give all squares but gives only one value. Fix it so it prints [1, 4, 9].
def squares(n):
for i in range(1, n + 1):
return i * i
print(list(squares(3)))Goal: Replace return with yield.
Q14. Fix the reuse
The program should print the numbers twice, but the second loop prints nothing. Fix it so both loops print 1 2 3 on separate lines.
def nums():
yield 1
yield 2
yield 3
g = nums()
for n in g:
print(n)
for n in g:
print(n)Goal: Avoid reusing exhausted generators.
Q15. Why lazy?
Explain in one or two sentences why a generator can use less memory than a list.
Goal: Explain the memory advantage.
Q16. Ending signal
Which exception tells a caller that a generator has no more values, and who usually handles it?
Goal: Name StopIteration.
Q17. Large file reader
A file has millions of lines and you need only lines that contain ERROR. Write a generator errors(lines) and test it on a short list. Print the result list.
Goal: Apply generators to log processing.
Q18. Id maker
Write a generator make_ids(prefix) that yields prefix-1, prefix-2 and so on forever. Print the first three ids.
Goal: Build an infinite id source.