1
00:00:04,700 --> 00:00:09,110
Let's have a look at some examples,
to understand how tuples work. So I'm

2
00:00:09,110 --> 00:00:13,140
going to go back to tuples_intro.py from the last video,

3
00:00:13,140 --> 00:00:19,430
and we're going to delete the old code. 
Alright, so I pasted the code in there to

4
00:00:19,430 --> 00:00:22,970
save some time. Pause the video, then come
back when you've caught up with the

5
00:00:22,970 --> 00:00:29,689
typing. Alright, so run the program,
which I'll do now. We should see the

6
00:00:29,689 --> 00:00:33,380
Metallica tuple printed out, and you
can see that on the screen down there.

7
00:00:33,380 --> 00:00:38,280
So it's a tuple containing three items;
We've got an album, Ride the Lightning,

8
00:00:38,280 --> 00:00:44,680
the artist, Metallica, and the year that
it was released, 1984. Tuples are a

9
00:00:44,680 --> 00:00:50,440
sequence type, which means we could use
indexing, to access the individual items in

10
00:00:50,440 --> 00:00:57,400
the tuple. Let's have a go doing that. So
below line 7, I'm going to type print

11
00:00:57,640 --> 00:01:05,180
metallica, then in square brackets, 0. I'm
going to do the same for the next two lines.

12
00:01:05,180 --> 00:01:14,440
I'm going to change the index
number to one and two; 1, 2, like so.

13
00:01:14,440 --> 00:01:19,759
So the item in index position zero is the
album name, and let's actually run this, so we

14
00:01:19,759 --> 00:01:23,659
can actually see all of them. So again,
the item at index position zero is the

15
00:01:23,660 --> 00:01:27,920
album name. We can see that printed on
the second line of the output.

16
00:01:27,920 --> 00:01:32,480
Next we have the artist on the third line
then the year on the last line of the output.

17
00:01:32,480 --> 00:01:35,860
Indexing a tuple works the same
as indexing a string or a list.

18
00:01:35,860 --> 00:01:40,760
You provide the index position in square
brackets. Now that's caused some

19
00:01:40,760 --> 00:01:45,950
confusion in the past. Even though tuples
are created using parentheses, you still

20
00:01:45,950 --> 00:01:50,390
use square brackets to index them, and
you can see that, if I close down the run

21
00:01:50,390 --> 00:01:55,729
window now, on lines 8 through 10. When
you're indexing something, use square

22
00:01:55,729 --> 00:02:01,840
brackets. That's the same whether the
thing you're indexing is a list, a string or a tuple.

23
00:02:01,840 --> 00:02:07,219
Unlike a list, a tuple is immutable.
If you attempt to change an item in a

24
00:02:07,219 --> 00:02:11,600
tuple, you'll get an error.
So let's actually check that. On line 12

25
00:02:11,600 --> 00:02:19,690
I'm going to type metallica square
brackets 0, is equal to Master of Puppets.

26
00:02:20,980 --> 00:02:25,940
Now before I run the program, notice that my IDE's
highlighted that line.

27
00:02:25,940 --> 00:02:30,709
If we hover over it, you'll see a message
that says here, something similar along

28
00:02:30,709 --> 00:02:35,860
the lines of Tuples don't support item
assignment. Now not all IDE's will do this,

29
00:02:35,860 --> 00:02:42,040
but most modern ones will. Now when we run
the program, you can see that we've got

30
00:02:42,049 --> 00:02:47,300
an error there; Tuple object does not
support item assignment.

31
00:03:29,860 --> 00:03:34,980
Python provides ways to create sequences of a different type.
You may have checked out the

32
00:03:34,990 --> 00:03:39,400
list and tuple functions, at the built-in
functions table in the documentation, so

33
00:03:39,400 --> 00:03:42,100
let's have a look at that.

34
00:03:43,320 --> 00:03:49,160
So we can see the two functions list and tuple there.
I'll just move this up the screen a little bit so we can see it.

35
00:03:49,160 --> 00:03:51,180
Now the documentation may be confusing

36
00:03:51,180 --> 00:03:55,760
because these aren't really functions.
They're really Python's equivalent

37
00:03:55,760 --> 00:04:00,220
of a class constructor. Don't worry about that
because it will make sense in the

38
00:04:00,220 --> 00:04:04,240
Object-Oriented Programming section of
this course. For now, though, you can

39
00:04:04,240 --> 00:04:08,220
pretend that they are functions. The
documentation has included them

40
00:04:08,220 --> 00:04:13,640
in a table of built-in functions, so that's not
unreasonable to pretend that they are.

41
00:04:13,640 --> 00:04:18,480
So the list function returns a list, using
the values in the iterable that's passed

42
00:04:18,488 --> 00:04:23,800
to it. So let's go back to our code, and
what I'm going to do is delete that line

43
00:04:23,800 --> 00:04:29,720
that gave us the error. Instead what
we're going to do, is type metallica2

44
00:04:29,720 --> 00:04:39,880
is equal to list metallica. We're going to print metallica2.

45
00:04:39,880 --> 00:04:45,000
The list function returns a list using the
values in the iterable that's passed to it.

46
00:04:45,000 --> 00:04:50,980
As I mentioned, if we run the program,
you can see that we've now got a list

47
00:04:50,980 --> 00:04:55,930
containing the same items as our
tuple. The last line of the output is the

48
00:04:55,930 --> 00:05:01,320
same as the first, except it uses square
brackets, which indicates a list.

49
00:05:01,320 --> 00:05:05,300
Because we've got a list, we can now
change the title of the album.

50
00:05:05,300 --> 00:05:16,200
So let's have a go at doing that. So metallica2
then left square bracket 0 equals,

51
00:05:16,200 --> 00:05:27,240
in double quotes, Master of Puppets. Then
we'll print Metallica 2 again.

52
00:05:27,240 --> 00:05:31,860
So we'll run this, and there shouldn't really be any surprises there.

53
00:05:31,860 --> 00:05:36,260
We've covered changing values in
lists earlier. So looking at the last line

54
00:05:36,260 --> 00:05:41,260
of the output, we've got an album called
Master of Puppets by Metallica,

55
00:05:41,260 --> 00:05:46,260
released in 1984. So when might a tuple be more appropriate?

56
00:05:46,260 --> 00:05:48,960
At this point, I'd like to
thank one of the students on this course,

57
00:05:48,960 --> 00:05:52,500
Toni Peralta, for pointing out that the data

58
00:05:52,500 --> 00:05:59,300
is now incorrect. The Master of Puppets
album was released in 1986, not 1984.

59
00:05:59,300 --> 00:06:03,420
The original video did mention that tuples
help protect the integrity of your data,

60
00:06:03,420 --> 00:06:08,280
but didn't link back to this example, so
thanks for that Toni.

61
00:07:00,780 --> 00:07:05,130
You'll see some other examples of tuples
being used, when we read data from a

62
00:07:05,130 --> 00:07:11,260
database in section 13. It's important
that the data in memory, matches the data

63
00:07:11,260 --> 00:07:16,860
in the database, and using an immutable
tuple can guard against unintended changes.

64
00:07:16,860 --> 00:07:22,540
We'll look at another reason, for
favoring a tuple, in the next video.

