Skip to content

Yield vs return

Quality Score

Overall Score: 8.5/10 ✅ Excellent

  • Technical Accuracy: 32/35
  • Code Quality: 22/25
  • Educational Value: 19/25
  • Documentation: 12/15

Last reviewed: June 22, 2026

Return

return keyword implies the output of a function. It stops the function after running.

src.intermediate.yield_vs_return.return_even_numbers(n)

Return a list of all even numbers from 2 up to n (exclusive).

This function demonstrates the use of return to produce a complete list at once. All even numbers are collected in memory before returning, which can be memory-intensive for large values of n.

Parameters:

Name Type Description Default
n int

The upper limit (exclusive) for generating even numbers. Only even numbers less than n will be included.

required

Returns:

Type Description
list[int]

A list containing all even numbers from 2 up to (but not including) n.

list[int]

Returns an empty list if n <= 2.

Source code in src/intermediate/yield_vs_return/yield_vs_return.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def return_even_numbers(n: int) -> list[int]:
    """Return a list of all even numbers from 2 up to n (exclusive).

    This function demonstrates the use of return to produce a complete list
    at once. All even numbers are collected in memory before returning,
    which can be memory-intensive for large values of n.

    Args:
        n: The upper limit (exclusive) for generating even numbers.
            Only even numbers less than n will be included.

    Returns:
        A list containing all even numbers from 2 up to (but not including) n.
        Returns an empty list if n <= 2.
    """
    numbers: list[int] = []
    for number in range(2, n):
        if number % 2 == 0:
            numbers.append(number)
    return numbers

Yield

yield keyword also returns a value, but a function can yield multiple outputs, so it does not stop the entire function. Yield returns a generator object, which is an iterator. It can be used in loops.

src.intermediate.yield_vs_return.yield_even_numbers(n)

Yield even numbers from 2 up to n (exclusive) one at a time.

This function demonstrates the use of yield to create a generator. Numbers are produced on-demand rather than all at once, making this memory-efficient for large values of n. Each number is computed only when requested by the caller.

Parameters:

Name Type Description Default
n int

The upper limit (exclusive) for generating even numbers. Only even numbers less than n will be yielded.

required

Yields:

Type Description
Generator

Even numbers from 2 up to (but not including) n, one at a time.

Returns:

Type Description
Generator

A Generator that yields even numbers. The generator is lazy and

Generator

produces values on-demand.

Source code in src/intermediate/yield_vs_return/yield_vs_return.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def yield_even_numbers(n: int) -> Generator:
    """Yield even numbers from 2 up to n (exclusive) one at a time.

    This function demonstrates the use of yield to create a generator.
    Numbers are produced on-demand rather than all at once, making this
    memory-efficient for large values of n. Each number is computed only
    when requested by the caller.

    Args:
        n: The upper limit (exclusive) for generating even numbers.
            Only even numbers less than n will be yielded.

    Yields:
        Even numbers from 2 up to (but not including) n, one at a time.

    Returns:
        A Generator that yields even numbers. The generator is lazy and
        produces values on-demand.
    """
    for number in range(2, n):
        if number % 2 == 0:
            yield number

You can use yield instead of return when the data size is large, as it doesn't store in memory the entire result, only when function is called. It is an efficient way of producing data that is big or infinite.

src.intermediate.yield_vs_return.yield_fibonacci_numbers()

Yield Fibonacci numbers in an infinite sequence.

This function demonstrates an infinite generator using yield. It produces Fibonacci numbers indefinitely without storing them in memory. The caller controls how many numbers to consume, making this pattern ideal for potentially infinite sequences.

The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...

Yields:

Type Description
Generator

The next number in the Fibonacci sequence, starting from 0.

Returns:

Type Description
Generator

A Generator that yields Fibonacci numbers indefinitely. The generator

Generator

will continue producing values until explicitly stopped by the caller.

Source code in src/intermediate/yield_vs_return/yield_vs_return.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def yield_fibonacci_numbers() -> Generator:
    """Yield Fibonacci numbers in an infinite sequence.

    This function demonstrates an infinite generator using yield. It produces
    Fibonacci numbers indefinitely without storing them in memory. The caller
    controls how many numbers to consume, making this pattern ideal for
    potentially infinite sequences.

    The Fibonacci sequence starts with 0 and 1, and each subsequent number
    is the sum of the two preceding numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...

    Yields:
        The next number in the Fibonacci sequence, starting from 0.

    Returns:
        A Generator that yields Fibonacci numbers indefinitely. The generator
        will continue producing values until explicitly stopped by the caller.
    """
    c1, c2 = 0, 1
    count = 0
    while True:
        yield c1
        c3 = c1 + c2
        c1 = c2
        c2 = c3
        count += 1

Yield returns a generator object, which is an iterator. It can be used in loops. After you initialize the generator, you can use next() to get the next value. When there are no more values to yield, it raises StopIteration exception. The generator can be used in a loop, and it will automatically stop when there are no more values to yield.

Once a generator is exhausted, it cannot be reused. You need to create a new generator object to iterate again.