Welcome back. Now, remember that sequences like strings, lists,
or tuples are ordered collections meaning that they have a first item,
a second item, a third item, and so on.
Now, often it can be useful to actually ask what is that first item or second item.
Indexing allows us to access a specific element of the sequence.
So, let's suppose that I have the sequence S,
which is a string, again, a sequence of characters.
Now, when I printout S then I get Python.
Suppose that rather than getting the whole string,
I only cared about the first character,
in order to do that then I would index on S. In order to index,
then I would say S and then I would say in square brackets the actual index that I want.
So, if I wanted the first character of S,
I would actually say index zero,
because Python's what's called zero index,
and what that means is that if I have a string like S,
which is P-Y-T-H-O-N and
then Python because this is a sequence,
every character is almost in a little bin.
The first character has index zero,
the second character has index one,
the third character has index two,
three, four, and so on.
So, you might be wondering why
almost every programming language including Python is zero index,
where this first index is zero.
So, the creator of Python explain this,
he said, "To anyone who prefers one-based indexing,
you're the same people who screwed up the calendar starting at year one,
in calling the 1900s the 20th century in
arguing that the year 2001 ought to be the start of the next millennium.
Haven't you done enough damage?"
So, he said this in jest.
But there is a reason that most programming languages are zero index.
It can actually be somewhat convenient once you get used to it.
So, indexing works on strings, lists and tuples.
So, let's suppose that I have a list,
myList and I set myList to be
a list with three items, one, two, three.
So, again, if I printout myList as a whole then I get the complete list.
If I only cared about the second item in myList,
then I would say myList and then open square brackets,
and close square brackets.
In between the square brackets,
I would say the index that I want.
So, if I cared about the second item,
then I would say index one because in zero index languages that's the second item.
So, when I printout myList sub one,
then that gives me two.
If I printout myList sub two,
then that gives me three.
If I printout myList sub zero,
then that gives me one.
So, another useful thing to do on sequences
is to be able to ask what's the length of the sequence?
In other words, how many characters are in this string?
How many items are in this list?
In order to do that,
we have a function called, "Len."
Len takes in a sequence like S on
myList and the value of this overall expression is called a Len.
Is going to be how many characters or items are in that sequence.
So, for example, if I printout the length of S,
by saying "print len of S",
then I get the number of characters in S which is going to give me six because I have
six characters in the string S. If I printout the length of myList,
then I get three because I have three items in myList.
One thing to note here is that,
even though the first and third item happen to be
strings and then myList itself only has three items.
The first item being a string,
the second item being an integer,
and the third item being its string.
So, its length is three.
So sometimes, it can also be useful to be able to get
the last item of a sequence as opposed to the first item.
So again, if we wanted to print the first item,
I would say S sub zero.
If I want to get the last item,
I would say "print S sub" and then I can write the length of
S. But then I actually have to subtract one from that because S is zero index.
So, if I run this, then I see that S printing out S sub zero
gives me the first character of S,
which is a capital P. In printing out S sub length of S minus one,
gives me the last character of S which is in N. So,
again just to illustrate why that is,
then I'm going to write out the indices of S. So,
the first character is item zero,
second character is item one,
third character is item two, three, four, and five.
So what that means is that, there's a total of one, two, three, four,
five, six items and so the length of S,
this part of the expression has value six.
But if I actually asked for S sub six,
then it would be looking for a character that S doesn't have.
Again, because S is zero index,
I need to subtract one from six to get five.
Now, it is really common to ask for the last item or the second to last item.
So, Python also includes in addition to these positive indices one,
two, three, four, five, and zero of course.
It also includes negative indices that count backwards from the end of the sequence.
So, I can ask for S sub negative one or negative two,
negative three, negative four,
negative five, or negative six.
So, if I replace this call,
to printout the last character of S by printing out S sub length of S minus one,
just to say," printout S sub negative one."
Then I get the exact same result.
If I printout S sub negative three,
then that's going to be the third-to-last character
in this case H which I can see you when I run my code.
If I printout S sub negative two,
then I get O and so on.
Of course, this negative indexing also works for
lists just as well as it works for strings.
So, if I printout myList, sub negative one,
then I'm going to get the string three
because that's the last item of this list, MyList.
Until next time.