Python | yield Keyword

Python | yield Keyword

In Python, the yield keyword is used in a function to create a generator. Generators are a type of iterable that can produce a sequence of values, one at a time, instead of generating all the values at once. The yield keyword is what makes a function a generator function.

When a generator function is called, it returns a generator object. This object can be iterated over using a loop or consumed using other iterable methods. Each time the generator’s yield keyword is encountered, the function’s execution is paused, and the value specified in the yield keyword is returned. The function’s state is preserved, allowing it to resume execution from where it left off the next time the generator is iterated or the next() function is called.

The yield keyword can appear multiple times within a generator function, allowing it to produce a sequence of values over multiple iterations. Each time the function encounters a yield keyword, it returns the specified value and pauses its execution until the next iteration.

Here’s a simple example to illustrate the usage of yield:

# creating the generator function
def countdown(n):
    while n > 0:
        yield n
        n -= 1

# return a generator object
generator = countdown(5)

# Iterating over the generator object
for value in generator:
    print(value)
	

In the example above, the countdown() function is a generator function that generates a countdown sequence starting from the given number n and going down to 1. When the generator is iterated over, it produces each value in the countdown sequence using the yield keyword. The function’s execution is paused after each yield keyword, and the value is returned to the caller.

When running the code, the output will be:

5
4
3
2
1

Same above example implemented using next() function.

# creating the generator function
def countdown(n):
    while n > 0:
        yield n
        n -= 1

# Creating a generator object
generator = countdown(5)

# print values one by one 
# from generator object
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))

It’s important to note that generators are lazily evaluated, meaning that the values are generated on-the-fly as they are needed, rather than all at once. This makes generators memory-efficient and suitable for handling large datasets or infinite sequences.

In summary, the yield keyword in Python is used in a generator function to produce a sequence of values. It suspends the function’s execution, returns a value to the caller, and preserves the function’s internal state. Generators, created using the yield keyword, provide an efficient way to generate values on-the-fly and are particularly useful for dealing with large datasets or infinite sequences.

Leave a Reply

Your email address will not be published.