Using Generators and yield in Python
Real Python

EDITOR BRIEF
Learn how to create Python generator functions and generator expressions using the yield statement. The article also explains when lazy evaluation—producing values as needed—can be useful.
INSIGHTS
Generators are useful for working with values one at a time. Try rewriting a simple function as a generator and compare it with a generator expression.
Learn more with these courses
CodeFriends courses that build on this story. Practice in the browser with nothing to install.
- Python Programming 101Learn Python in just 20 hours! Kickstart your programming journey with this beginner-friendly course.Beginner20 Hours
- Introduction to Web Development (Light)Master HTML, CSS, and JavaScript in just 10 hours.Beginner10 Hours
- Mastering SQL FundamentalsIn the era of data and AI, numbers speak louder than words. Build a solid foundation in SQL to query, manage, and analyze data effectively.Beginner20 Hours
COMMENTS
Loading comments…