← How-To Guides How-To How to use a generator Use yield instead of return in a function. Call it to get an iterator; iterate with for or next(). yield Lazy iteration. def nums(): yield 1 yield 2 for n in nums(): print(n)