Let me show you another useful function that we'll use, just like a range.
Mind you, it's not as common as range range.
You'll see it a lot in Python code, but this other one is still very useful and it's called enumerate.
Now, what does it do?
Let's have a look.
I can do a for loop and I'll say four and we'll say character in enumerate and then enumerate.
We wrap around something that, well, we want to enumerate.
So let's see what that means.
It returns and enumerate to object.
So iterable must be an other object that supports iteration.
So we have to give it something that enumerate can iterate over.
So this time around let's just do a a string that says hello.
Now enumerate is special because what it's going to give us is it's going to take an iterable object
and gives you an index counter.
And the item that index.
What I mean by that, well, you can actually say I CAAMA character.
So we're unpacking this and we're saying, hey, print I and character, if I click run.
I get hello iterated over again, if we don't have enumerate, then we would have just gotten hello.
But we also because we use enumerate, we get this eye or index of each item and where it is in the
index so that if I had a list here, let's say one, two, three.
Again, I'm able to use and access the index number of the item as well.
What if we had a couple?
Again, we get the index number.
So innumerate is very useful if you need the index counter of the item that you're looping through.
So let's do a little exercise.
Let's say that we want to create a script that enumerates a list of numbers one through ten, so we'll
use range to create that I'm going to say list and then I'm going to say range.
I know there's a lot of functions here.
Range that is one hundred actually list to that.
And I want to be told what the index of the number 50 is.
So pause the video if you need to do that.
Now, here, I want it so that when you make this a little bit bigger, I want it so that we only print
the index of, let's say, the value fifty.
I want to know where 50 is in the list.
Now, again, this is a simple example, so you can probably already guess what the answer is, but
how would you code this so that only in here we get the index of fifty or it says the index of fifty
is whatever.
Pause the video if you want to give it a go.
Otherwise, let's try this.
I'm going to do a conditional and says if character is equal to 50.
Then I'm going to print.
Index or I'm going to do an F string, and I will say that the index of 50 is and then I'll add the
index in here.
So that if I run.
I get index of 50 is 50.
And just to confirm if, for example, I just print here and let's say print I and character, I click
run.
We see that map, we have the index is the same as the numbers, again, not too difficult of a question,
but you see here that we're able to extract this information again, not as useful as range, but you
will see it occasionally in code.
So I wanted to make sure that you're not surprised by it.
As you can see, it's not that difficult.