1
00:00:04,280 --> 00:00:09,720
Alright, so let's get back to the documentation
about mutable sequence types.

2
00:00:09,720 --> 00:00:13,120
We'll place the link into the browser.

3
00:00:13,120 --> 00:00:17,900
So looking at this table again, it might
look quite confusing. The first five

4
00:00:17,900 --> 00:00:21,960
operations are using indexing and
slicing to change items in the sequence.

5
00:00:21,960 --> 00:00:25,230
Make sure that you're happy using
indexes and slices. Review the videos in

6
00:00:25,230 --> 00:00:30,180
section five if necessary, because we'll
be using slices in this video. So the

7
00:00:30,180 --> 00:00:35,780
first operation in this table uses
indexing to replace an item in the list.

8
00:00:35,780 --> 00:00:39,620
So we're going to continue with lists
_intro.py for this example.

9
00:00:39,620 --> 00:00:46,160
So I'm going to go back and open that up:
lists_intro.py.

10
00:00:46,160 --> 00:00:51,320
Now we're going to delete everything here
except the list on lines one through six.

11
00:00:52,190 --> 00:00:57,930
On line seven we're going to print out
our lists our print parentheses computer

12
00:00:57,930 --> 00:01:04,220
underscore parts. Let's now replace the
mouse in our list with a trackball.

13
00:01:04,220 --> 00:01:09,260
The mouse is at index position three, so
let's modify it and then print it out again.

14
00:01:09,260 --> 00:01:12,580
So on line nine I'm going to type
computer_ parts,

15
00:01:12,580 --> 00:01:18,000
left square bracket, 3, right square
bracket, equals, then what we want to set it to,

16
00:01:18,000 --> 00:01:21,440
in this case, to trackball. So trackball, surrounded in double quotes

17
00:01:21,440 --> 00:01:24,899
because it's a string. Let's now print
the list again, so print computer

18
00:01:24,899 --> 00:01:32,219
underscore parts. Let's run the program.
You can see mouse is in the first list,

19
00:01:32,220 --> 00:01:36,240
printed out on line seven. And now
that we've made the change on line nine,

20
00:01:36,240 --> 00:01:40,580
when we print out the second time, we
see trackball replacing mouse. When we're

21
00:01:40,580 --> 00:01:44,520
dealing with strings, we could get the
character at a particular index position.

22
00:01:44,520 --> 00:01:49,140
Strings are immutable, so we can't
replace one character with another. When

23
00:01:49,140 --> 00:01:52,439
working with a mutable sequence type
such as a list, though, we can also

24
00:01:52,439 --> 00:01:56,670
replace items. So let's go back to
the browser, and that's what this first

25
00:01:56,670 --> 00:02:01,740
operation in that table's saying. If you've
got a mutable sequence called s, we can

26
00:02:01,740 --> 00:02:06,659
assign a new value to the item at index
position i. Now the second operation is

27
00:02:06,659 --> 00:02:11,310
similar, but this time it refers to a
slice. Notice that it's referring to an

28
00:02:11,310 --> 00:02:15,140
iterable this time. X on the line above is an object.

29
00:02:15,140 --> 00:02:18,560
We used a string in our example,
but any suitable object would also have

30
00:02:18,560 --> 00:02:22,640
worked. The second operation is talking
about an iterable.

31
00:02:22,640 --> 00:02:25,360
Also notice the subtle difference in the
two descriptions for those two

32
00:02:25,360 --> 00:02:29,380
operations. The description for the first one, s,

33
00:02:29,380 --> 00:02:35,300
i in square brackets equals x, says that
item i is replaced by x. For the second

34
00:02:35,300 --> 00:02:39,350
operation, the description says that the
slice will be replaced by the contents

35
00:02:39,350 --> 00:02:43,610
of t. So it's easy to miss subtle things
like that, when you're just starting to

36
00:02:43,610 --> 00:02:47,030
read the documentation. But don't worry
because as you get more experience with

37
00:02:47,030 --> 00:02:51,170
the documentation, it soon starts to make
sense, and as you progress through this

38
00:02:51,170 --> 00:02:55,700
course, you'll start paying attention to
the way things are phrased. When you get

39
00:02:55,700 --> 00:02:59,380
strange results, as we're about to do,
it's easy to get frustrated.

40
00:02:59,380 --> 00:03:03,640
In fact, it's perfectly normal to get
frustrated. Frustration is a natural

41
00:03:03,650 --> 00:03:07,520
state for a programmer. There are very
few things that are as frustrating as

42
00:03:07,520 --> 00:03:11,150
learning to program, and that's
especially true when you're trying to

43
00:03:11,150 --> 00:03:15,710
make sense of documentation like this,
without an example. For an example, going

44
00:03:15,710 --> 00:03:19,550
back to our code, note that we don't
really need a mouse mat if we're

45
00:03:19,550 --> 00:03:24,480
using a trackball. We can replace both
items at once by specifying a slice

46
00:03:24,480 --> 00:03:28,400
instead of an index. So what I'm going to
do is start out by printing out the

47
00:03:28,400 --> 00:03:32,150
slice, so that we can see what we're
changing. I'm also going to comment out

48
00:03:32,150 --> 00:03:36,290
the trackball on line 9, and we're going
to change line 10. Instead of me just

49
00:03:36,290 --> 00:03:40,070
printing out the list of computer parts,
we're going to add a square bracket to

50
00:03:40,070 --> 00:03:45,300
the end of computer parts, 3: in those square brackets.

51
00:03:45,300 --> 00:03:48,500
So basically, we're printing out the
items starting at position 3, extending

52
00:03:48,500 --> 00:03:52,560
up to the end of the list, and that
should be the mouse and the mouse mat.

53
00:03:52,560 --> 00:03:58,300
So let's run the program and confirm that
is the case. We can see that the slice,

54
00:03:58,300 --> 00:04:02,660
3: in square brackets, is the mouse and the
mat. So we've got the correct slice here.

55
00:04:02,660 --> 00:04:06,980
At this point we can now replace the
slice with a trackball, so let's try

56
00:04:06,980 --> 00:04:12,380
to do that. So our first attempt
might be something along the lines of

57
00:04:12,380 --> 00:04:17,360
computer_parts square brackets 3:

58
00:04:17,360 --> 00:04:22,620
In those square brackets equals
trackball in double quotes,

59
00:04:22,620 --> 00:04:28,040
print computer parts on the next line. Now
that looks reasonable. Let's see what

60
00:04:28,040 --> 00:04:33,760
happens when I run that. Of course you
knew that something strange was going to happen.

61
00:04:33,760 --> 00:04:36,600
I gave that away when we were
looking at the documentation.

62
00:04:36,600 --> 00:04:40,540
What's happened, looking at the final line of
the output, is that each of the

63
00:04:40,540 --> 00:04:44,620
characters in the string trackball, have
been added to the list individually.

64
00:04:44,620 --> 00:04:48,200
And that's what the documentation means
when it says that the slice is replaced

65
00:04:48,200 --> 00:04:52,760
by the contents of the iterable. To fix
the problem, we want the iterable

66
00:04:52,760 --> 00:04:56,990
containing a single item - the string
trackball. Now we're working with an

67
00:04:56,990 --> 00:05:01,280
iterable type at the moment - lists are
iterables. In fact, all sequence types are

68
00:05:01,280 --> 00:05:07,040
iterables. To get the result we want, we
can put the trackball string into a list.

69
00:05:07,040 --> 00:05:11,460
So I'm gonna make the right-hand side of
the assignment on line 12 slightly different.

70
00:05:11,460 --> 00:05:15,620
So let's make that, well let's
surround that, in square brackets,

71
00:05:15,620 --> 00:05:21,320
effectively making it a list. So this
time when I run the program, we get the

72
00:05:21,320 --> 00:05:27,180
result we wanted. The two items - mouse and mouse mat - have now been replaced with trackball.

73
00:05:27,180 --> 00:05:29,720
The main thing I want you to
learn from this video isn't about using

74
00:05:29,720 --> 00:05:33,050
a slice in a list. The really important part is about the

75
00:05:33,050 --> 00:05:36,560
documentation. Be aware of subtle
differences in the way things are

76
00:05:36,560 --> 00:05:41,479
phrased. If two descriptions are
different, like the first two back here,

77
00:05:41,479 --> 00:05:45,410
in the list of operations on mutable
sequences, then you'll know that their

78
00:05:45,410 --> 00:05:49,260
behavior will be different. Alright, so
let's end the video here and in the next one,

79
00:05:49,260 --> 00:05:55,760
we'll look at an example of deleting items
from a list. See you in the next video.

