Let's talk about generators.
A very important key term and quite an advanced topic.
What are they?
Generators are available in Python and it allows us to generate a sequence of values over time.
Hmm.
What does that mean?
Let's explore it.
You see, we've actually learned a generator before.
Do you remember range?
Well, range is a generator.
But what is that?
A generator is a special type of thing in Python that allows us to use a special keyword called yield
and it can pause and resume functions.
So let me demonstrate this with a nice little example.
You see when we use something like range.
Verse says something like list.
Let's say we convert a range into a list.
Like so.
A list.
What it does is actually create a giant list, let's say, of 100 items in memory, in our computer's
memory versus a range.
It creates them one by one.
Let me show you.
If I create a function and let's call it make list.
And this make list is going to accept.
A number.
And this make list, well, we'll say result equals an empty array then I'm going to say for I in range
of an.
We're going to add result dot append and let's just multiply it for fun.
I times to.
And then finally we return the result.
You see, when we make a list like we just did here, we're essentially doing this.
We're using range to create a list and return result which will live in memory.
So let's say that here with make list, I'm going to say my list is going to equal make list 100.
When I print.
My list.
And I click Run.
Oh.
And make sure my.
Parameter here is the right name.
And make sure I can spell append.
We get our list.
But this list over here lives in our memory.
My list is pointing to a location in memory.
So this is taking up space right now.
Now, range is a generator.
And a generator is a little bit different because this is not being held in memory when we do this for
loop.
This range doesn't create on its own a giant list of, lets say 0 to 99.
And then it starts iterating.
No, with the for loop it's going to say, hey, as a range I'm going to give you first the number zero,
then I'm going to give you the number one, then number two, the number three, the number four all
the way to 100.
So in memory, it never, ever creates this list like we have with, well, my list.
And this can get pretty hectic, right?
Let's say we want to create a giant list.
Now, let's say that I want to create a list with.
Well.
This entire array.
If I wanted to, let's say print here.
This giant list and I click run.
Oh, boy.
You see that?
That's a lot of memory that I'm using up.
And once it's done.
Which it's still gone.
Only then can I use this list, and it's going to access that list in memory.
And then we're able to use it a more efficient way is to use a generator and actually generate these
one at a time without taking space in memory.
Now in order for us to learn how to do that with generators and how to actually use them, we need to
go to the next lesson and revisit some old knowledge.
I'm going to let this complete.
In the meantime, I'll see you in the next video.
Bye bye.