Predict Generator Output
Predict Output Medium
Question
What does this code print?
Code
def count_up_to(n):
i = 1
while i <= n:
yield i
i += 1
print(list(count_up_to(3)))
Hint
The generator yields 1, 2, 3 in sequence.
Explanation
count_up_to(3) yields 1, 2, 3 (while i <= 3). list() consumes all yielded values into a list. Output: [1, 2, 3]
Related Resources
Related Lessons
- Generators
Consuming generators.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.