Chapters
Python114 chapters

ReferenceKeywords

yield

Turns a function into a generator that produces values one at a time.

Signature

yield value

Returns

Each yielded value, on demand.

Example

Python
def countdown(n):
    while n > 0:
        yield n
        n -= 1

print(list(countdown(3)))

Output

[3, 2, 1]

See also