1
00:00:08,000 --> 00:00:13,230
Welcome back. Now, remember that sequences like strings, lists,

2
00:00:13,230 --> 00:00:17,325
or tuples are ordered collections meaning that they have a first item,

3
00:00:17,325 --> 00:00:19,845
a second item, a third item, and so on.

4
00:00:19,845 --> 00:00:25,425
Now, often it can be useful to actually ask what is that first item or second item.

5
00:00:25,425 --> 00:00:29,835
Indexing allows us to access a specific element of the sequence.

6
00:00:29,835 --> 00:00:33,080
So, let's suppose that I have the sequence S,

7
00:00:33,080 --> 00:00:35,900
which is a string, again, a sequence of characters.

8
00:00:35,900 --> 00:00:38,955
Now, when I printout S then I get Python.

9
00:00:38,955 --> 00:00:42,080
Suppose that rather than getting the whole string,

10
00:00:42,080 --> 00:00:44,585
I only cared about the first character,

11
00:00:44,585 --> 00:00:49,545
in order to do that then I would index on S. In order to index,

12
00:00:49,545 --> 00:00:55,625
then I would say S and then I would say in square brackets the actual index that I want.

13
00:00:55,625 --> 00:00:58,505
So, if I wanted the first character of S,

14
00:00:58,505 --> 00:01:00,850
I would actually say index zero,

15
00:01:00,850 --> 00:01:03,670
because Python's what's called zero index,

16
00:01:03,670 --> 00:01:07,790
and what that means is that if I have a string like S,

17
00:01:07,790 --> 00:01:16,640
which is P-Y-T-H-O-N and

18
00:01:16,640 --> 00:01:19,850
then Python because this is a sequence,

19
00:01:19,850 --> 00:01:23,070
every character is almost in a little bin.

20
00:01:24,310 --> 00:01:28,115
The first character has index zero,

21
00:01:28,115 --> 00:01:30,560
the second character has index one,

22
00:01:30,560 --> 00:01:33,035
the third character has index two,

23
00:01:33,035 --> 00:01:36,540
three, four, and so on.

24
00:01:36,950 --> 00:01:39,305
So, you might be wondering why

25
00:01:39,305 --> 00:01:43,160
almost every programming language including Python is zero index,

26
00:01:43,160 --> 00:01:45,170
where this first index is zero.

27
00:01:45,170 --> 00:01:47,665
So, the creator of Python explain this,

28
00:01:47,665 --> 00:01:50,990
he said, "To anyone who prefers one-based indexing,

29
00:01:50,990 --> 00:01:54,300
you're the same people who screwed up the calendar starting at year one,

30
00:01:54,300 --> 00:01:57,500
in calling the 1900s the 20th century in

31
00:01:57,500 --> 00:02:01,360
arguing that the year 2001 ought to be the start of the next millennium.

32
00:02:01,360 --> 00:02:03,225
Haven't you done enough damage?"

33
00:02:03,225 --> 00:02:05,090
So, he said this in jest.

34
00:02:05,090 --> 00:02:08,760
But there is a reason that most programming languages are zero index.

35
00:02:08,760 --> 00:02:12,875
It can actually be somewhat convenient once you get used to it.

36
00:02:12,875 --> 00:02:17,760
So, indexing works on strings, lists and tuples.

37
00:02:17,760 --> 00:02:20,120
So, let's suppose that I have a list,

38
00:02:20,120 --> 00:02:28,880
myList and I set myList to be

39
00:02:28,880 --> 00:02:37,680
a list with three items, one, two, three.

40
00:02:37,680 --> 00:02:43,175
So, again, if I printout myList as a whole then I get the complete list.

41
00:02:43,175 --> 00:02:46,505
If I only cared about the second item in myList,

42
00:02:46,505 --> 00:02:50,140
then I would say myList and then open square brackets,

43
00:02:50,140 --> 00:02:51,735
and close square brackets.

44
00:02:51,735 --> 00:02:53,080
In between the square brackets,

45
00:02:53,080 --> 00:02:54,560
I would say the index that I want.

46
00:02:54,560 --> 00:02:56,960
So, if I cared about the second item,

47
00:02:56,960 --> 00:03:02,830
then I would say index one because in zero index languages that's the second item.

48
00:03:02,830 --> 00:03:05,745
So, when I printout myList sub one,

49
00:03:05,745 --> 00:03:07,425
then that gives me two.

50
00:03:07,425 --> 00:03:09,900
If I printout myList sub two,

51
00:03:09,900 --> 00:03:11,475
then that gives me three.

52
00:03:11,475 --> 00:03:14,255
If I printout myList sub zero,

53
00:03:14,255 --> 00:03:16,960
then that gives me one.

54
00:03:16,960 --> 00:03:20,405
So, another useful thing to do on sequences

55
00:03:20,405 --> 00:03:23,460
is to be able to ask what's the length of the sequence?

56
00:03:23,460 --> 00:03:26,195
In other words, how many characters are in this string?

57
00:03:26,195 --> 00:03:28,235
How many items are in this list?

58
00:03:28,235 --> 00:03:29,720
In order to do that,

59
00:03:29,720 --> 00:03:32,455
we have a function called, "Len."

60
00:03:32,455 --> 00:03:35,310
Len takes in a sequence like S on

61
00:03:35,310 --> 00:03:40,715
myList and the value of this overall expression is called a Len.

62
00:03:40,715 --> 00:03:45,490
Is going to be how many characters or items are in that sequence.

63
00:03:45,490 --> 00:03:51,015
So, for example, if I printout the length of S,

64
00:03:51,015 --> 00:03:54,730
by saying "print len of S",

65
00:03:55,750 --> 00:04:01,370
then I get the number of characters in S which is going to give me six because I have

66
00:04:01,370 --> 00:04:07,490
six characters in the string S. If I printout the length of myList,

67
00:04:07,490 --> 00:04:12,400
then I get three because I have three items in myList.

68
00:04:12,400 --> 00:04:14,425
One thing to note here is that,

69
00:04:14,425 --> 00:04:17,090
even though the first and third item happen to be

70
00:04:17,090 --> 00:04:20,825
strings and then myList itself only has three items.

71
00:04:20,825 --> 00:04:23,015
The first item being a string,

72
00:04:23,015 --> 00:04:24,755
the second item being an integer,

73
00:04:24,755 --> 00:04:26,755
and the third item being its string.

74
00:04:26,755 --> 00:04:29,295
So, its length is three.

75
00:04:29,295 --> 00:04:32,030
So sometimes, it can also be useful to be able to get

76
00:04:32,030 --> 00:04:35,375
the last item of a sequence as opposed to the first item.

77
00:04:35,375 --> 00:04:38,750
So again, if we wanted to print the first item,

78
00:04:38,750 --> 00:04:42,450
I would say S sub zero.

79
00:04:42,450 --> 00:04:44,850
If I want to get the last item,

80
00:04:44,850 --> 00:04:50,955
I would say "print S sub" and then I can write the length of

81
00:04:50,955 --> 00:04:58,490
S. But then I actually have to subtract one from that because S is zero index.

82
00:04:58,490 --> 00:05:03,770
So, if I run this, then I see that S printing out S sub zero

83
00:05:03,770 --> 00:05:05,665
gives me the first character of S,

84
00:05:05,665 --> 00:05:10,500
which is a capital P. In printing out S sub length of S minus one,

85
00:05:10,500 --> 00:05:15,150
gives me the last character of S which is in N. So,

86
00:05:15,150 --> 00:05:17,385
again just to illustrate why that is,

87
00:05:17,385 --> 00:05:25,020
then I'm going to write out the indices of S. So,

88
00:05:25,020 --> 00:05:27,465
the first character is item zero,

89
00:05:27,465 --> 00:05:29,040
second character is item one,

90
00:05:29,040 --> 00:05:34,630
third character is item two, three, four, and five.

91
00:05:44,780 --> 00:05:52,090
So what that means is that, there's a total of one, two, three, four,

92
00:05:52,090 --> 00:05:56,305
five, six items and so the length of S,

93
00:05:56,305 --> 00:05:59,900
this part of the expression has value six.

94
00:05:59,900 --> 00:06:03,255
But if I actually asked for S sub six,

95
00:06:03,255 --> 00:06:06,460
then it would be looking for a character that S doesn't have.

96
00:06:06,460 --> 00:06:08,730
Again, because S is zero index,

97
00:06:08,730 --> 00:06:14,160
I need to subtract one from six to get five.

98
00:06:14,570 --> 00:06:20,570
Now, it is really common to ask for the last item or the second to last item.

99
00:06:20,570 --> 00:06:24,950
So, Python also includes in addition to these positive indices one,

100
00:06:24,950 --> 00:06:27,675
two, three, four, five, and zero of course.

101
00:06:27,675 --> 00:06:32,570
It also includes negative indices that count backwards from the end of the sequence.

102
00:06:32,570 --> 00:06:37,085
So, I can ask for S sub negative one or negative two,

103
00:06:37,085 --> 00:06:40,345
negative three, negative four,

104
00:06:40,345 --> 00:06:44,355
negative five, or negative six.

105
00:06:44,355 --> 00:06:47,460
So, if I replace this call,

106
00:06:47,460 --> 00:06:52,920
to printout the last character of S by printing out S sub length of S minus one,

107
00:06:52,920 --> 00:06:56,655
just to say," printout S sub negative one."

108
00:06:56,655 --> 00:06:58,965
Then I get the exact same result.

109
00:06:58,965 --> 00:07:02,365
If I printout S sub negative three,

110
00:07:02,365 --> 00:07:05,855
then that's going to be the third-to-last character

111
00:07:05,855 --> 00:07:09,185
in this case H which I can see you when I run my code.

112
00:07:09,185 --> 00:07:11,330
If I printout S sub negative two,

113
00:07:11,330 --> 00:07:13,880
then I get O and so on.

114
00:07:13,880 --> 00:07:16,970
Of course, this negative indexing also works for

115
00:07:16,970 --> 00:07:19,810
lists just as well as it works for strings.

116
00:07:19,810 --> 00:07:23,860
So, if I printout myList, sub negative one,

117
00:07:23,860 --> 00:07:26,090
then I'm going to get the string three

118
00:07:26,090 --> 00:07:29,270
because that's the last item of this list, MyList.

119
00:07:29,270 --> 00:07:32,160
Until next time.