Introduction

3 minutes
Share the link to this page
Copied
  Completed
An introduction of the topics to be covered in Python Generators |Yield with the list of all topics.

Transcript

Hi, this video is about generators in Python. generators are a simple way of creating iterators however it uses yield keyword. Now what purpose it serves. Let's take an example. We are looking to search for a word Python in the text file that has for example 20 or more lines. And the word Python is in line five, line eight line 12.

Now, if we use generator what it does, it is going to find it is an iterator it is going to find the word Python in line five and it is going to stop there. When we are going to call back again that generator it is going to start from line six. Normally what a function does, it starts from again line one. However, the generator has this unique feature. It starts from where it has left when it is called. Now, if the word Python in line five is not for what we are looking for we're going to call the generated function again and it is going to start from line six and from at line eight is going to stop again.

And similarly, if that is not the key word of Python that we are looking for, it is going to continue moving forwards starting from where it had stopped. So this makes it very useful and it produces the result very fast. Especially in a memory intensive application, where we are not looking to store the values generator can be very useful. Now, have you heard about yield statement? Just a hint. It is very much related to generators.

Yes, I think you have heard in the initial intro it is a keyword using generator it tells Python engine what output to produce and stop. For example, we tell in the code or plug in generator that if in the line the word Python is is locked, it has to stop there and it produce it has to produce the output of that line. So, how are we going to like write it in the form of Python, we can going to start with yield and the line number or the line that it that will tell Python engine that it has to stop there. This ends our intro of Python generators. Hope you have got some idea of what a Python generator does. Moving forward in our upcoming video we are going to discuss the definition of generator, the use case, one of the use cases we already discussed, but there are other use cases as well, how to create generators, examples and there are many examples it includes.

So there would be one simple example then another example that is also simple but it is an advanced version of the first example and the third example that is a bit more complex. I hope you will love these videos. It will give you a clearer idea of the generators and its overall functionality. Its use cases its example and where to use it. It is also one of the advanced topic in Python. However, I will try to make it as easy and as simple for you as possible.

Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.