Yield Keyword
Question
What does yield do in Python?
Hint
yield produces values lazily.
Explanation
yield turns a function into a generator. Instead of returning once, the function yields values one at a time, pausing between each. The caller gets values with next() or a for loop. Generators are memory-efficient for large sequences because they produce values on demand, not all at once. Use yield for streaming data, infinite sequences, or large datasets.
Related Resources
Related Lessons
- Generators
Learn Python 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.