Most sequences have a few useful methods that you can do on any sequence.
The way to call that method is to save the sequence name and then the method name.
So, I'm going to go over two methods on sequences, count and index.
So, suppose that we have a,
a string and we want to find out how many times
a given character or a given sequence of characters appears in the string a?
The way that we would do that is using the count method.
The way to call count is we say the name of the sequence whether it's a string,
integer, or tuple that we want to count on.
So here, we want to do a count on a.
So, I say a, and then the dot or period,
and then the method name that we want to call,
dot count, open parentheses.
In the case of count,
we pass in a string which is the sequence of characters that we actually want to count.
So, on line two,
we're counting the number of characters e in a,
and then we close parenthesis in order to finish that method call.
One thing to note here is that count is case sensitive.
Meaning that we don't count capital e's if there are any capital e's in a.
We would only count lowercase e's.
So here, when we say printout a.count("e"),
then we look for the number of e's that are in the string a.
So, in this case, we should get one,
two, three, four, five.
In addition to counting individual characters,
we can also count substrings.
So here, we're counting the number of times the substring ha appears.
So, we should get one, two.
So, let's run our code just to double check.
So, we can see that we get five e's and two times that ha appears in this string a.
Just like strings, count also works on lists.
So here, we have a list z that has 1,2,3,4,5,6,7,8,9,10 items.
So, on line two,
if I comment out the rest of these lines,
on line two, we're finding out
how many times does the string four appear as an item in z.
One thing to note here is that the string
four is an entirely different thing than the integer four,
which we have here, here, and here.
So, when we say print out z.count the string four,
then we're going to actually get zero because no item in z is the string four.
If we instead counted for the integer four,
then we would get what we might expect.
So, we'd get one, two,
three because there are three integers four in our list z.
So, if I hit save and run again,
then I'll see that I get three when I print out z.count the integer four.
Here's something that's a bit trickier.
So, let's suppose that I say z.count the string a.
Now, we can see that in our list z,
the letter a appears a few times.
So, in the first item of z,
a appears in atoms,
and then it also appears in the last item.
So, we might almost expect the result to be two.
But if we actually run our code,
then we'll see the result is zero.
Now, why is that?
Well, when we call z.count the character a,
what we're looking for our items in z where the item itself is the string a.
So, the fact that a just happens to be
a character in this larger string doesn't matter to Python.
What Python would care about is,
is the value actually the string a?
So, if I had one item here that was the string a,
then I would get count one.
But because this instead is the string atoms,
then I get zero when I look for the number of a's.
On line five, we do z.count("electron"),
and when I look, I see that there is one,
two electrons in z.
So, I should expect line five to print out two.
Now, let's go on to another useful method named index.
Index finds where an item is.
In the case of index,
it searches for the first time that a given string or item appears.
So, let's suppose that on line one,
we're creating this string,
"Pull out your music and dancing can begin".
Now on line two, we create this list that has 1,2,3,4,5,6,7,8,9,10 items.
Now, when we print out the value of music.index of m,
then we're printing out the first or leftmost index where lowercase m appears.
So, if I comment out the rest of these lines,
and only print out the value of music.index of m,
then we should get the index of the first m. Just to find out where that is,
we should get it at
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14.
So, this m, which is the first term leftmost m that appears is at index 14.
What that means is that the value of this overall expression,
music.index of m is going to be 14.
Let's run our code and make sure that that's the case.
So, we see that we get 14.
On line five, we print out the value of music.index of your.
So, what this is going to do is it's going to search for
the first time that this substring 'your' appears,
and it's going to print out the index where it starts.
So, we should expect that to be zero, one, two, three,
four, five, six, seven, eight, nine.
So, when we run this and we get nine as a result because 'your' starts at index nine.
Now, index works with lists as well as it does with strings.
So, if we print out bio.index of metatarsal,
then we get the first index where metatarsal appears.
So here, on bio,
that should be index zero.
Even though this appears multiple times at index zero and one,
index always gives us the first or leftmost index where it appears.
So, nine, seven should print out zero.
On line eight, we print out bio.index of an empty list.
This is going to search for the first empty list in this list bio.
So, the index should be zero,
one, two, and we find it at three.
So, when we run this,
we should expect three.
Then finally on line nine,
we print out bio.index of 43.
So, that's going to be zero, one, two,
three, four, five, six.
Now, one thing to note for index is that if something is actually not in the sequence,
then we get a runtime error.
So, if we create this list,
seasons and set its values to be winter,
spring, summer, and fall.
Then we search for autumn,
note that autumn is not in here because we instead wrote fall.
So, if we print out seasons.index of autumn,
then we're going to get a runtime error,
and that runtime error is going to print out that the item that we searched for,
in this case, autumn was not in our list, seasons.
Okay. So, let's answer some questions about count and index.
So, this question ask,
what will be stored in the variable ty below?
So, we say ty is qu,
which is this string dot index of we.
So, this is searching for the first time that we find the string we.
So, that's going to give us zero, one,
two, three, four, five.
Again, when we search for a substring,
we search for where it starts.
So, that's going to give us five.
Next, what will be stored in the variable ty now?
So, we say ty is the string qu.count of we.
So, here we're searching for how many times does
this substring we appear in this string qu?
So, we see we right here at the start of welcome,
here at the start of week.
We might think that we actually see it here,
but if you look closely,
this is actually a capital w and not a lowercase w. Now,
remember that Python is case sensitive.
So, capital w is a completely different thing than a lowercase w. So,
Python isn't actually going to count this.
So, Python isn't actually going to count this we because it contains a capital w. So,
the end result here is that we get two.
So here, we're asked, what will be stored in the variable ht below.
So, we have room set to a list with one, two, three, four,
five, six Items, and we asked for rooms.index of garden.
So, we're going to search for is garden in this list rooms.
So, we see it's not here,
nor here, nor here,
nor here, nor here, nor here.
Garden isn't anywhere in this list.
Remember that when we tried to do index on something that's actually not in the list,
then we get an error.
So, the answer here should be an error.
That's all for now, until next time.