Python generator is a special type of function that returns an iterator object. Instead of returning a value, it uses yield to give a sequence of values over time, which pauses after each yield statement while keeping its state between iterations.
Generators are memory efficient, which means making items one at a time, with the help of yield keyword.
Let us take a simple example to demonstrate the Generator in Python:
Output:
1 2 3 4 5
Explanation:
In the above code, a generate_numbers function is defined, which accepts a limit as argument, that executes a for loop in the function range from 1 to limit + 1, and produces the number. The function is called with a limit value of 5 and prints the yielded value.
It is easy to create a generator, simply define a function that has at least one yield statement in the function. When the function is called, instead of returning a single value, you will get back a generator object, which is an iterator.
Here is the standard syntax to define the generator in Python.
Let's take an example to demonstrate how to use generators in Python.
Output:
1 2 3
Explanation:
The above code in the example defines a generator function 'number_generator' which generates the values from a list [1, 2, 3] one at a time with the help of a for loop. Each call to the generator function provides the next value in sequence and can be more memory-efficient, which involves iterating over the data in a lazy manner.
The yield statement is used in a generator function to get a sequence of values over time. When the statement yield produces, it pauses the running of the function, returns the values, and saves the function statement, so when it is resumed later, it will continue from where it left off. This is especially useful for working efficiently with large or complicated data streams.
On the other hand, the return statement will give the result immediately and give you back a single final value. The function will end when the return statement is called, and the local state will not be preserved, which is helpful when the function is only needed to return one result.
Output:
6
Explanation:
The function calculate_total() makes a list of numbers based on Python's built-in sum() function to add the numbers. The function results in the total number and is displayed. This method is simple, easy to understand, and can be quickly updated to use more values.
Generator expressions enable generators to be more compact. They are similar to lists but use parentheses rather than square brackets, and are more memory efficient.
Let us take an example to demonstrate the generator expression in Python.
Output:
1 4 9 16 25
Explanation:
The code uses a generator expression to generate the squares of a given number. The generator yields a single square at any time, which is then displayed at the time of the loop. This is efficient regarding memory as it does not have the whole list in memory.
Several applications of generators in Python are as follows:
In Python, generators are an excellent method for working on large datasets and for generating complex sequences of results. The yield statement enables a function to generate one result while maintaining its state between iterations. This is very useful when working on large files or generating infinite sequences like the Fibonacci numbers. Generators are beneficial when compared with a function that simply returns, as a generator can generate more data for less memory, generating more values at a time in a given time interval.
We request you to subscribe our newsletter for upcoming updates.