Instructor: Welcome back.
Let's keep going with some more string methods.
Now we've learned about adding and removing ones.
We learned about append, insert, and extend.
And then in removing,
we learned about pop, remove, and clear.
There's a few other important ones.
Let's go with the index.
So instead of clearing here, let's clean this up
and we'll do basket.index.
And index, as you can see,
we give it a value, a start and a stop.
What does that mean?
Well, we wanna say, Hey, what index is number two?
If we do index of two and I click run
it's going to give me one.
I know this is a little confusing
but it's asking for the index.
Where is number two?
Number two is at index of one.
Let's change these into letters just so it's more clear.
So we have A, B, C, D,
and then finally E.
And again, if I do D here and I click run
I get an index of three because 0, 1, 2, 3
it's on shelf three in our memory.
And again, also do a optional parameter here
which is where should we start looking?
So I want you to start looking
at index of zero and then finish looking at index of two.
If I do run here, I get an error.
It says, Hey, D is not in list because, well
we started at index of zero and stopped at index of two.
If I do three and I click run,
nope.
It stops right before three.
So if I do four and I click run,
there you go.
I get three.
Now, errors are not good in our programs.
Ideally, we don't wanna get errors.
So how can we avoid this?
What if we're looking for something
and we're not sure if it's in the list or not?
There's another nifty way of looking for things in a list
and this involves what we call a Python key word.
A key word is something that you use
in Python that will -- already has some meaning.
For example, a key word here if I go to Python keywords
are all these words that already mean something.
Now we're gonna go through them and we haven't seen a lot
of them yet, but you we might have seen true, right?
In boolean and values, true means something.
So in Python, we can't really use the word true
because well, it already means something.
There's a word called in
that as you can see, as soon as we type means something,
is a keyword in Python.
And this is quite nice.
We can say, Hey, is D
in basket?
And if I run this,
I get true.
Okay, is X in basket?
I get false because, well, it doesn't exist.
And this is actually quite useful.
And we can even do it for strings.
For example, if I'm looking for
let's say the letter I in a word, and let's say it's I in
"Hi, my name is the end."
If I click run,
I get true
because the letter I does exist in this string.
All right, let's learn another one.
Another one is basket.count.
And we can count how many times an item occurs.
So for example, if I do D and I click run
I get one because D only exists once in the list.
But if I add another D in here and I click run, we get two.
It counts how many time this value occurs in our list.
Very, very useful.
All right, a few more still to go.
Let's do some exercises and I'll see you in the next one.