All right.
This massive list finally finished being entered in memory.
But remember our key terms.
Remember how I said a list is an iterable?
What was an iterable?
Well, an iterable is any object in Python which we're able to loop through underneath the hood.
It has the dunder enter method.
So when the object is created, this editor allows us to have an iterable object that can be iterated
over to iterate something or iteration is the act of taking an item from an iterable doing something
to it, then going to the next one.
What we use loops like for loops or while loops we call that iteration.
It's the process itself.
And generators.
Are actually iterable.
That is everything that is a generator is iterable.
You can iterate over them, but not everything that is iterable is a generator.
For example, a range is a generator.
So that is always going to be iterable.
But a list is an iterable, but it's not a generator.
So a generator is a subset of an iterable.
I know those are a lot of words.
Hopefully you're still with me.
The difference between a generator and a regular iterable is the way we implement them.
So let me show you a general way to create a generator.
Let's clear this code and define our own generator.
And generators are usually functions just like range is a function.
Generators are usually created this way.
So let's say generator function.
And this generator function is going to have a four loop that is going to loop over a range just like
we had before.
But the difference here, let's actually bring back the code that we have previously.
She's going to comment this out so we can see it in terms of difference.
The generator function is going to.
Create, let's say, these many items.
But instead of returning like we would in a regular function, we're going to use a keyword called yield.
And here we're going to yield the I so the item.
So notice the difference here.
Now, if we want, just like here, we can pass a number.
Like this.
But instead of returning.
And appending this list and creating all this data we're going to use eald.
Now what does Hield do?
You see, Hield pauses the function and comes back to it when we do something to it, which is called
next, something that we'll cover shortly.
So Hield says, Hey, pause the function, just Hield I give I and when you tell me to keep going again,
then I'll keep going.
So how can we do this?
Well, I can say for item in the generator function.
I'm going to.
Print.
The item.
Now here, what's going to happen is we're going to loop over the generator function and we'll give
it to the generator function, let's say 1000 for now.
And we're going to print each item in the generator function because this is an iterable.
It's going to run and it's going to loop.
And for every item in this number so that it's range, we're going to yield Y so it's going to keep
looping and looping.
And when we come back, we loop over.
We're going to run this and the yield.
Comes back and runs again.
Let me show you what I mean.
I'm going to comment this out, comment this out as well.
And just click run here.
Did you see that?
What we just did here is similar to down here, but instead of having to create that list in memory,
it just kept going one by one.
We only held one item in memory.
And we used it however we wanted to.
In our case, all we did was print item.
Let's explore this keyword a little bit more.
What does it actually do?
Let's say that we want to multiply this by two, just like we did here in our original example with
lists.
But now, instead of doing the four loop, let's just call the function.
And say, gee, equals generator function and we're going to give it 100.
Now if I print G and I click Run.
You see that?
I get a generator object.
And remember, this is just the standard way in Python that we can create a generator function using
the range and the yield keyword.
But this keyword gives us some power because if I just return here.
We're not going to get anything special.
As a matter of fact, we're just going to get a value instead by using the yield keyword.
What we're able to do is to turn this into a generator function that can do well.
This I can say next g.
Now if I print next G in here and I click Run.
I get zero.
But here's a cool part.
If I do next G twice and then one time here and print it.
I get for.
Why is that?
Remember the key word pauses the function.
So what just happened here is I ran the function with 100.
The first item in range 100 was zero.
Right.
Now here before I was even able to run this, it said, Hey, this is a generator function, but the
first time you run it.
Give the output zero times two so that the very first time we run this function and I click Run.
We'll make sure we change that to I.
This is just zero.
If I run it again like this, then we have.
Now I want you to iterate and go to one.
So one times two.
What is that?
Well, one times two.
Let me fix that.
Like this.
Make sure this is I, I get two because one times two is two.
If I run it again and I click run.
I get for.
You see.
Hield, like I said before, pauses the function and comes back to it when next is called.
So if it has a yield keyword, it becomes a generator.
It keeps track of this state, what we call the value, and it only keeps the most recent data in memory,
for example, for it remembers that because on let's say line 14 if I do print next G.
It remembers that previously this I was.
Well for.
So now, even though this piece of code already ran, if I run it online, line 14.
I get six?
Because three times two well is six, remember, because it gets multiply by two.
So let me ask you this.
If let's say I only do a range of one here.
And you know what?
Let's just remove multiply by two.
If I do this and I run next more than well, more than once, what is going to happen if I click Run
here?
I get a stop.
Iteration.
You see, next can be called as many times as we want.
As many times until this range expires.
And when we exceed the number of items in the range.
Well, we get a stop iteration error.
To let us know that, hey, you can't call next anymore.
We've reached the end of line and when we use for loops with ranges, it actually detects this underneath
the hood for you.
So we're able to run a function like this where we use range, even though range is a generator, because
the for loop automatically says, Hey, I'm going to stop looping as soon as I encounter this stop iteration,
but that's hidden away from us as users.
Now, this concept of generators is pretty hard to understand, especially at first.
So you might have to look at this a couple of times, but over the next couple of videos, I want to
convince you how powerful they are.
I'll see you in the next one by.