-: Let me show you another useful function that we'll use
just like 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 for
and we'll say character in enumerate.
and then enumerate, we wrap around something
that, well, we wanna enumerate.
So let's see what that means.
It returns an enumerate 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 at that index.
What do I mean by that?
Well, you can actually say i, character.
So we're unpacking this and we're saying, hey
print i and character.
If I click run, look at that
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 i or index of each item and where it is in the index.
So that if I had a list here
let's say 1, 2, 3.
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 enumerate 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 wanna create a script
that enumerates a list
of numbers 1 through 10.
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 100.
Actually, let's do 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,
let me make this a little bit bigger.
I want it so that we only print the index
of, let's say, the value 50.
I wanna 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 50.
Or it says the index of 50 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 that 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'll 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, yep
we have the indexes 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.