0Pricing
Python Academy · Lesson

The yield Keyword

Write generator functions with yield for custom iterators.

Introduction

Generator functions use yield to produce a sequence of values lazily, pausing execution between each yield.

What is yield?

A function with yield is a generator function. Each call to next() resumes from the last yield.
def count_up(n):
    for i in range(n):
        yield i
for x in count_up(3):
    print(x)

All lessons in this course

  1. List Comprehensions
  2. Set and Dict Comprehensions
  3. Generator Expressions
  4. The yield Keyword
← Back to Python Academy