WEBVTT

1
00:00:00.340 --> 00:00:01.900
<v Jose>Hi guys, and welcome back.</v>

2
00:00:01.900 --> 00:00:05.180
In this video, we're going to talk about tuples.

3
00:00:05.180 --> 00:00:07.220
Tuples are very similar to lists in that

4
00:00:07.220 --> 00:00:10.450
they are used to store multiple pieces of information.

5
00:00:10.450 --> 00:00:13.040
But there is a small, subtle difference

6
00:00:13.040 --> 00:00:15.340
between tuples and lists, let's jump right in.

7
00:00:16.820 --> 00:00:18.780
A tuple can be defined like this,

8
00:00:18.780 --> 00:00:21.000
let's imagine you've got a short tuple

9
00:00:21.000 --> 00:00:22.980
that contains two of your friend names,

10
00:00:22.980 --> 00:00:26.590
you can just say Rolf comma Bob.

11
00:00:26.590 --> 00:00:29.000
Notice that there are no square brackets now

12
00:00:29.000 --> 00:00:32.530
around these two strings, but there is a comma

13
00:00:32.530 --> 00:00:34.970
in between them so you've got one string

14
00:00:34.970 --> 00:00:36.440
and then another string.

15
00:00:36.440 --> 00:00:38.500
This is a tuple.

16
00:00:38.500 --> 00:00:42.300
You can think of it as almost the same thing as a list,

17
00:00:42.300 --> 00:00:45.120
but I'll show you the difference in just a moment.

18
00:00:45.120 --> 00:00:49.580
It's often good form to put brackets around this.

19
00:00:49.580 --> 00:00:51.913
So this is a bit clearer.

20
00:00:52.750 --> 00:00:57.750
If you put brackets around the two strings like that,

21
00:00:58.210 --> 00:01:00.610
this is a little bit clearer that it's a tuple,

22
00:01:00.610 --> 00:01:02.870
and almost all of the time you are going to see

23
00:01:02.870 --> 00:01:06.150
tuples defined with the brackets around them.

24
00:01:06.150 --> 00:01:07.480
But remember that the brackets

25
00:01:07.480 --> 00:01:10.300
are not required in many cases.

26
00:01:10.300 --> 00:01:12.800
Although there are some cases where they will be,

27
00:01:12.800 --> 00:01:15.280
and that is when Python cannot be sure

28
00:01:15.280 --> 00:01:18.920
if the comma is used to separate two things

29
00:01:18.920 --> 00:01:21.840
inside a tuple or something else.

30
00:01:21.840 --> 00:01:25.210
For example, here's an occasion

31
00:01:25.210 --> 00:01:26.850
where you will need to use the brackets,

32
00:01:26.850 --> 00:01:30.490
which is when you want to put a tuple inside a list.

33
00:01:30.490 --> 00:01:34.560
If you have a list, and you wanna put a tuple inside it,

34
00:01:34.560 --> 00:01:35.803
and you do it like this,

35
00:01:36.780 --> 00:01:39.010
you have not put a tuple inside the list.

36
00:01:39.010 --> 00:01:41.410
What you've done is you've created a list of two elements

37
00:01:41.410 --> 00:01:43.310
as we saw in the last video.

38
00:01:43.310 --> 00:01:46.060
If you wanna create a list with a tuple inside it,

39
00:01:46.060 --> 00:01:48.830
you will need the brackets so that Python knows

40
00:01:48.830 --> 00:01:51.670
that you intend this to evaluate first,

41
00:01:51.670 --> 00:01:55.080
which creates a tuple, and then the list to evaluate later.

42
00:01:55.080 --> 00:01:56.820
That's what brackets are normally used for

43
00:01:56.820 --> 00:02:00.783
in mathematics as well, to signal order of evaluation.

44
00:02:02.270 --> 00:02:04.050
So, most of the time you're going to see

45
00:02:04.050 --> 00:02:06.280
your tuples defined with brackets around them,

46
00:02:06.280 --> 00:02:07.920
and I would encourage you to get accustomed

47
00:02:07.920 --> 00:02:10.080
to that notation as well.

48
00:02:10.080 --> 00:02:12.150
Something very important is that while

49
00:02:12.150 --> 00:02:14.320
you can write your tuples like this,

50
00:02:14.320 --> 00:02:17.820
this thing is most definitely not a tuple

51
00:02:17.820 --> 00:02:19.570
if you just write Rolf.

52
00:02:19.570 --> 00:02:23.070
If you write Rolf on its own, that's just a string, right?

53
00:02:23.070 --> 00:02:25.920
But if you add Rolf and then a comma,

54
00:02:25.920 --> 00:02:28.070
Python knows that you want a tuple there,

55
00:02:28.070 --> 00:02:30.400
and this, adding a comma at the end,

56
00:02:30.400 --> 00:02:31.890
is something that a lot of my students

57
00:02:31.890 --> 00:02:34.010
make as a mistake when they're starting out,

58
00:02:34.010 --> 00:02:36.370
they just put a comma at the end because you know,

59
00:02:36.370 --> 00:02:38.550
maybe they come from a different programming language,

60
00:02:38.550 --> 00:02:41.850
or maybe it's a title or a small mistake,

61
00:02:41.850 --> 00:02:43.210
and this can cause some trouble,

62
00:02:43.210 --> 00:02:46.190
so make sure to not put commas at the end of your statements

63
00:02:46.190 --> 00:02:49.700
in Python, because that turns strings, or anything else,

64
00:02:49.700 --> 00:02:51.950
into tuples, and that can be a bit confusing.

65
00:02:52.820 --> 00:02:55.290
So I've got this tuple here with my friend names,

66
00:02:55.290 --> 00:02:57.090
Rolf, Bob, and Anne, and let's say

67
00:02:57.090 --> 00:02:59.520
I wanna add something to this tuple.

68
00:02:59.520 --> 00:03:04.393
Like we learned with lists, friends.append, and then Jen.

69
00:03:05.400 --> 00:03:09.773
So if we run this now, you'll see that we'll get an error,

70
00:03:10.770 --> 00:03:14.840
and it says, attribute error, this is the name of the error,

71
00:03:14.840 --> 00:03:17.880
tuple object has no attribute append.

72
00:03:17.880 --> 00:03:20.830
And here it tells you the line in which the error occurred,

73
00:03:20.830 --> 00:03:23.343
which is line two of our code.

74
00:03:24.680 --> 00:03:29.680
What this means is that you can't do dot append on a tuple.

75
00:03:31.110 --> 00:03:33.860
Okay, so how do you add to a tuple

76
00:03:33.860 --> 00:03:35.593
if you can't append to it?

77
00:03:36.700 --> 00:03:40.253
Well, that's the thing, you cannot add to a tuple.

78
00:03:41.160 --> 00:03:43.780
Here we've got a tuple of three elements,

79
00:03:43.780 --> 00:03:47.350
and you cannot insert an extra element on this tuple.

80
00:03:47.350 --> 00:03:50.260
All you can do is say friends equal

81
00:03:50.260 --> 00:03:53.720
friends plus Jen, like that.

82
00:03:53.720 --> 00:03:56.730
So you can add two tuples together,

83
00:03:56.730 --> 00:04:00.693
in this case, the friends tuple and the Jen tuple

84
00:04:00.693 --> 00:04:03.190
with a single element, and those two,

85
00:04:03.190 --> 00:04:04.720
and say that friends is now equal

86
00:04:04.720 --> 00:04:06.800
to the result of adding them.

87
00:04:06.800 --> 00:04:08.650
Remember when you do an equal sign,

88
00:04:08.650 --> 00:04:11.350
the right side evaluates first,

89
00:04:11.350 --> 00:04:15.117
so this thing here uses the current value of friends

90
00:04:15.117 --> 00:04:18.460
and this new tuple, and then you give it the name friends,

91
00:04:18.460 --> 00:04:20.950
so you can reassign while using

92
00:04:20.950 --> 00:04:22.350
a variable in the assignment,

93
00:04:22.350 --> 00:04:24.770
that's totally fine, it just uses the last value

94
00:04:24.770 --> 00:04:27.200
of the variable if it existed.

95
00:04:27.200 --> 00:04:29.890
You can do this, if you want,

96
00:04:29.890 --> 00:04:33.540
and that will result in a tuple of four elements.

97
00:04:33.540 --> 00:04:36.040
But what's important, and an important distinction,

98
00:04:36.040 --> 00:04:38.580
is that the tuple itself did not change,

99
00:04:38.580 --> 00:04:42.580
you created a new one, which contains now four elements.

100
00:04:42.580 --> 00:04:45.960
And while that's relatively unimportant right now

101
00:04:45.960 --> 00:04:48.830
it will become important as you learn more Python,

102
00:04:48.830 --> 00:04:52.320
so that is the key difference between lists and tuples,

103
00:04:52.320 --> 00:04:57.010
lists, you can add and remove elements, tuples, you cannot.

104
00:04:57.010 --> 00:04:58.680
So that's the key difference.

105
00:04:58.680 --> 00:05:03.460
Tuples are useful for when you wanna keep them unchanged.

106
00:05:03.460 --> 00:05:07.470
Most of the time, I'd recommend using tuples over lists.

107
00:05:07.470 --> 00:05:10.220
And only use lists when you specifically want

108
00:05:10.220 --> 00:05:13.410
to allow modification or changes.

109
00:05:13.410 --> 00:05:15.880
Again, at the moment this may not make much sense,

110
00:05:15.880 --> 00:05:18.860
but you will find more meaning in that as you learn more.

111
00:05:18.860 --> 00:05:20.670
Trust me on that one.

112
00:05:20.670 --> 00:05:21.930
That's everything for this video though,

113
00:05:21.930 --> 00:05:23.090
we've learned about tuples,

114
00:05:23.090 --> 00:05:25.053
I'll see you on the next one.

