Python Certification Training for Data Scienc ...
- 53k Enrolled Learners
- Live Class
Generating iterables or objects that allow stepping over them is considered to be a burdensome task. But, in Python, the implementation of this painful task just gets really smooth. So let’s go ahead and take a closer look at Generators in Python.
Generators are basically functions that return traversable objects or items. These functions do not produce all the items at once, rather they produce them one at a time and only when required. Whenever the for statement is included to iterate over a set of items, a generator function is run. Generators have a number of advantages as well.
Without Generators in Python, producing iterables is extremely difficult and lengthy.
Generators easy to implement as they automatically implement __iter__(), __next__() and StopIteration which otherwise, need to be explicitly specified.
Memory is saved as the items are produced as when required, unlike normal Python functions. This fact becomes very important when you need to create a huge number of iterators. This is also considered as the biggest advantage of generators.
Can be used to produce an infinite number of items.
They can also be used to pipeline a number of operations
Normal Functions vs Generator Functions:
Generators in Python are created just like how you create normal functions using the ‘def’ keyword. But, Generator functions make use of the yield keyword instead of return. This is done to notify the interpreter that this is an iterator. Not just this, Generator functions are run when the next() function is called and not by their name as in case of normal functions. Consider the following example to understand it better:
def func(a): yield a a=[1,2,3] b=func(a) next(b)
OUTPUT: [1, 2, 3]
As you can see, in the above output, func() is making use of the yield keyword and the next function for its execution. But, for normal function you will need the following piece of code:
def func(a): return a a=[1,2,3] func(a)
OUTPUT: [1, 2, 3]
If you look at the above example, you might be wondering why to use a Generator function when the normal function is also returning the same output. So let’s move on and see how to use Generators in Python.
As mentioned earlier, Generators in Python produce iterables one at a time. Take a look at the following example:
def myfunc(a): while a>=3: yield a a=a+1 b = myfunc(a) print(b) next(b)
When you execute the following function, you will see the following output:
Here, one iterable object has been returned satisfying the while condition. After execution, the control is transferred to the caller. In case more items are needed, the same function needs to be executed again by calling the next() function.
On further executions, the function will return 6,7, etc. Generator functions in Python implement the __iter__() and __next__() methods automatically. Therefore, you can iterate over the objects by just using the next() method. When the item generation should terminate, Generator functions implement the StopIteration internally without having to worry the caller. Here is another example of this:
a=2 def myfunc(a): while a >= 0: yield a a -= 1 b = myfunc(a) print(b) next(b)
The above image shows the execution of our program required number of times. If you try to call the next function again, it returns a message depicting StopIteration has been implemented. If you try to do this with normal functions, the values returned will not change or iterate. Take a look at the example below:
def z(): n=1 yield n n=n+3 yield n p=z() next(p)
In case you want to execute the same function at once, you can make use of the ‘for’ loop. This loop helps iterate over the objects and after all implementations it executes StopIteration.
def z(): n=1 yield n n=n+3 yield n for x in z(): print(x)
You can also specify expressions to generate iterable objects.
You can also use expressions along with the for loop to produce iterators. This usually makes the generation iterables much easy. Generator expression resemble list comprehensions and like lambda functions, generator expressions create anonymous generator functions.
Take a look at the example below:
a=range(6) print("List Comprehension", end=':') b=[x+2 for x in a] print(b) print("Generator expression", end=':n') c=(x+2 for x in a) print(c) for y in c: print(y)
List Comprehension:[2, 3, 4, 5, 6, 7]
<generator object <genexpr> at 0x0000016362944480>
As you can see, in the above output, the first expression is a list comprehension which is specified within  brackets. List comprehension produces the complete list of items at once. The next is a generator expression which returns the same items but one at a time. It is specified using () brackets.
a=range(6) print("Generator expression", end=':n') c=(x+2 for x in a) print(c) print(min(c))
The above program prints the min value when the above expression as applied to the values of a.
Let us use Generators in Python to:
Fibonacci series as we all know is a series of numbers wherein each number is a sum of preceding two numbers. The first two numbers are 0 and 1. Here is a generator program to generate Fibonacci series:
def fibo(): first,second=0,1 while True: yield first first,second=second,first+second for x in fibo(): if x>50: break print(x, end=" ")
0 1 1 2 3 5 8 13 21 34
The above output shows the Fibonacci series with values less than 50. Let’s now take a look at how to generate a list of numbers.
In case you want to generate specified list numbers, you can do it using generator functions. Take a look a look at the following example:
a=range(10) b=(x for x in a) print(b) for y in b: print(y)
<generator object <genexpr> at 0x000001CBE1602DE0>
a=range(2,10,2) b=(x for x in a) print(b) for y in b: print(y)
<generator object <genexpr> at 0x000001CBE1623138>
The above program has returned even numbers from 2 to 10. This brings us to the end of this article on Generators in Python. I hope you have understood all the topics.
Got a question for us? Please mention it in the comments section of this “Generators in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python Certification Training with 24/7 support and lifetime access.