WEBVTT

1
00:00:01.080 --> 00:00:02.580
<v ->Hi, guys and welcome back.</v>

2
00:00:02.580 --> 00:00:05.690
In this video, we're going to learn about lists, tuples,

3
00:00:05.690 --> 00:00:09.610
and sets. Three different collections in Python that

4
00:00:09.610 --> 00:00:13.470
allows us to store multiple values in a single variable.

5
00:00:13.470 --> 00:00:15.090
So that we can use them all at once

6
00:00:15.090 --> 00:00:17.383
or maybe extract some of them individually.

7
00:00:18.550 --> 00:00:19.970
Let's get started.

8
00:00:19.970 --> 00:00:22.370
We're going to define one of each just so you

9
00:00:22.370 --> 00:00:24.780
know how they are defined.

10
00:00:24.780 --> 00:00:29.493
So, l is going to be a list of three elements.

11
00:00:31.290 --> 00:00:34.620
And here I've created a variable called l for lists

12
00:00:34.620 --> 00:00:36.720
and I've made it equal to a list.

13
00:00:36.720 --> 00:00:40.340
The list is defined by using square brackets

14
00:00:40.340 --> 00:00:42.830
and inside the square brackets you can put different

15
00:00:42.830 --> 00:00:46.150
values as long as they are separated by a comma.

16
00:00:46.150 --> 00:00:49.630
So what I've got here is one string, then I've got a comma,

17
00:00:49.630 --> 00:00:51.990
then I've got another string, then I've got another comma,

18
00:00:51.990 --> 00:00:54.820
and then I've got another string. So three strings separated

19
00:00:54.820 --> 00:00:58.703
by commas inside square brackets. This defines a list.

20
00:00:59.620 --> 00:01:02.680
If you didn't have lists or tuples or sets,

21
00:01:02.680 --> 00:01:04.720
you would have to define three variables,

22
00:01:04.720 --> 00:01:06.010
one for each friend.

23
00:01:06.010 --> 00:01:07.780
And that would make it difficult to interact

24
00:01:07.780 --> 00:01:09.360
with all the values at once.

25
00:01:09.360 --> 00:01:10.640
You'll understand what I mean

26
00:01:10.640 --> 00:01:12.633
as we progress through the video.

27
00:01:14.120 --> 00:01:18.640
A tuple is very similar to a list but instead of using

28
00:01:18.640 --> 00:01:21.750
square brackets you use normal brackets.

29
00:01:21.750 --> 00:01:24.370
The key difference between lists and tuples is that

30
00:01:24.370 --> 00:01:28.470
you can't modify a tuple, whereas you can add and remove

31
00:01:28.470 --> 00:01:31.300
elements from a list, you can't add and remove

32
00:01:31.300 --> 00:01:32.480
elements from a tuple.

33
00:01:32.480 --> 00:01:35.040
That's the key difference between the two.

34
00:01:35.040 --> 00:01:39.390
Finally, a set is, again very similar, but you use curly

35
00:01:39.390 --> 00:01:42.703
braces instead of square or normal brackets.

36
00:01:45.490 --> 00:01:48.230
And the key difference between a set and the other two

37
00:01:48.230 --> 00:01:50.460
is that while you can add and remove elements from

38
00:01:50.460 --> 00:01:53.440
a set you can't have duplicate elements.

39
00:01:53.440 --> 00:01:57.070
So you couldn't have Bob twice in the set.

40
00:01:57.070 --> 00:02:01.290
Also, lists and tuples keep the order of the elements.

41
00:02:01.290 --> 00:02:04.220
So whenever you print out this list, it will always have

42
00:02:04.220 --> 00:02:07.120
Bob then Rolf and then Anne in that order,

43
00:02:07.120 --> 00:02:09.800
but sets don't necessarily keep the order,

44
00:02:09.800 --> 00:02:11.500
rather the order is not guaranteed.

45
00:02:11.500 --> 00:02:13.740
So the order could change at any moment.

46
00:02:13.740 --> 00:02:16.920
Sets are useful in a few scenarios that we will learn

47
00:02:16.920 --> 00:02:18.523
about in the coming videos.

48
00:02:19.520 --> 00:02:23.490
You can access individual elements of a list or a tuple

49
00:02:23.490 --> 00:02:25.840
by using subscript notation.

50
00:02:25.840 --> 00:02:27.653
So for example, you can print l.

51
00:02:30.050 --> 00:02:33.060
So what we've got here is our list variable

52
00:02:33.060 --> 00:02:35.487
and then we've got [0].

53
00:02:37.050 --> 00:02:39.400
This notation here is called subscript notation

54
00:02:39.400 --> 00:02:41.860
and you can apply it to many things in Python

55
00:02:41.860 --> 00:02:43.970
but you can use it on lists and tuples

56
00:02:43.970 --> 00:02:48.823
and what it does is give you that element with index zero.

57
00:02:49.830 --> 00:02:52.890
Elements in programming usually start counting at zero,

58
00:02:52.890 --> 00:02:57.120
so the first element here, Bob has an index of zero.

59
00:02:57.120 --> 00:02:58.700
Then this one has an index of one,

60
00:02:58.700 --> 00:03:00.320
and this one has an index of two.

61
00:03:00.320 --> 00:03:05.120
So you could access Anne by doing l[2].

62
00:03:05.120 --> 00:03:08.040
This also works for tuples so you can use your tuple

63
00:03:08.040 --> 00:03:11.210
variable there instead and that's totally find as well.

64
00:03:11.210 --> 00:03:14.340
However, it doesn't make sense to do that in sets

65
00:03:14.340 --> 00:03:16.240
because they don't have any order.

66
00:03:16.240 --> 00:03:18.780
So if you access the third element of a set,

67
00:03:18.780 --> 00:03:20.290
you don't know what you're going to get.

68
00:03:20.290 --> 00:03:22.510
That's why sets don't allow you

69
00:03:22.510 --> 00:03:24.313
to use subscript notation on them.

70
00:03:25.520 --> 00:03:28.900
You can modify individual items in a list by accessing

71
00:03:28.900 --> 00:03:31.870
the item itself using subscript notation

72
00:03:31.870 --> 00:03:34.140
and then you treating it like a variable.

73
00:03:34.140 --> 00:03:37.790
So for example, you can say l[0] equals Smith

74
00:03:37.790 --> 00:03:39.900
and then if you print your list and you run it,

75
00:03:39.900 --> 00:03:43.320
you will see that the first element is now Smith,

76
00:03:43.320 --> 00:03:45.470
second one's Rolf and the third one's Anne.

77
00:03:47.020 --> 00:03:51.150
However, if you're trying to modify a tuples element

78
00:03:51.150 --> 00:03:54.510
and you run this file, you will actually get an error

79
00:03:54.510 --> 00:03:58.060
because tuples cannot be modified after they're created

80
00:03:58.060 --> 00:04:01.643
so you can't use the equal sign on anything inside a tuple.

81
00:04:02.700 --> 00:04:04.630
Of course you can not do this on a set

82
00:04:04.630 --> 00:04:07.583
because a set does not allow for this subscript notation.

83
00:04:08.650 --> 00:04:12.900
You can add elements to a list by doing l.append

84
00:04:12.900 --> 00:04:16.480
and what append does is it will add an element

85
00:04:16.480 --> 00:04:17.930
to the end of the list.

86
00:04:17.930 --> 00:04:21.220
So if we append, for example, Smith to this list

87
00:04:21.220 --> 00:04:23.300
and then print it out you'll see that this list

88
00:04:23.300 --> 00:04:25.320
will now have four elements.

89
00:04:25.320 --> 00:04:27.413
That's Bob, Rolf, Anne, and Smith.

90
00:04:28.460 --> 00:04:31.910
But again because tuples to not allow modification,

91
00:04:31.910 --> 00:04:34.440
you can't change them, then if you do something like this

92
00:04:34.440 --> 00:04:36.220
you're also going to get an error and it's going to tell you

93
00:04:36.220 --> 00:04:40.030
that the tuple object has no attribute append.

94
00:04:40.030 --> 00:04:43.410
What that means is that we tried to access something

95
00:04:43.410 --> 00:04:47.257
called append inside, and that's what this full stop means

96
00:04:47.257 --> 00:04:51.170
so we're trying to access append inside of our tuple

97
00:04:51.170 --> 00:04:54.810
but our tuple doesn't have anything called append inside it.

98
00:04:54.810 --> 00:04:57.950
We're going to learn more about what I mean by inside

99
00:04:57.950 --> 00:05:00.460
in the next few videos. It is a little bit

100
00:05:00.460 --> 00:05:03.980
of a more complicated topic but rest assured you can't

101
00:05:03.980 --> 00:05:06.633
add things or remove things from a tuple.

102
00:05:08.160 --> 00:05:12.890
Talking about removal, you can remove things from a list.

103
00:05:12.890 --> 00:05:16.430
We're doing l.remove and then passing in the element

104
00:05:16.430 --> 00:05:17.300
that you want to remove.

105
00:05:17.300 --> 00:05:19.550
For example, if you wanted to remove Bob,

106
00:05:19.550 --> 00:05:21.420
then this would do that and then if you

107
00:05:21.420 --> 00:05:24.770
print(l) then you'll see that Bob has been removed

108
00:05:24.770 --> 00:05:27.203
and you are only left with Rolf and Anne.

109
00:05:28.830 --> 00:05:31.970
Going back to adding, you can add things to a set.

110
00:05:31.970 --> 00:05:35.880
So you can do s.add("Smith") then you can print your set

111
00:05:38.480 --> 00:05:39.500
and here you can see you that you get

112
00:05:39.500 --> 00:05:42.660
Anne, Bob, Smith, and Rolf. Notice that the order is

113
00:05:42.660 --> 00:05:45.930
not the same as when we created this, but all the elements

114
00:05:45.930 --> 00:05:49.860
are there. However, if you do the same thing twice,

115
00:05:49.860 --> 00:05:52.430
you'll see that Smith will only be here once.

116
00:05:52.430 --> 00:05:54.240
That's because, even though we told the set

117
00:05:54.240 --> 00:05:57.760
to add Smith twice there can be no duplicate elements

118
00:05:57.760 --> 00:06:01.043
inside a set, so this second Smith is actually ignored.

119
00:06:02.160 --> 00:06:05.850
This was a very brief primer or refresher regarding what

120
00:06:05.850 --> 00:06:08.140
lists, tuples and sets are.

121
00:06:08.140 --> 00:06:12.290
So just remember that lists are the most standard collection

122
00:06:12.290 --> 00:06:15.250
where you add and remove elements as well as modify them.

123
00:06:15.250 --> 00:06:18.370
In a tuple you have those elements and you can't modify them

124
00:06:18.370 --> 00:06:20.270
and you can't add or remove elements.

125
00:06:20.270 --> 00:06:23.480
And finally, a set, has no duplicate elements

126
00:06:23.480 --> 00:06:25.803
and it also has no order.

127
00:06:27.000 --> 00:06:28.300
That's everything for this video.

128
00:06:28.300 --> 00:06:29.400
Thank you for joining me.

129
00:06:29.400 --> 00:06:31.050
And I'll see you in the next one.

