1
00:00:05,420 --> 00:00:08,970
So the last of Python's data
structures that we're gonna look at

2
00:00:08,970 --> 00:00:11,770
in this part of the course, is sets.

3
00:00:11,770 --> 00:00:14,800
Now if you're familiar with
mathematical set theory,

4
00:00:14,800 --> 00:00:17,140
then Python's sets will make
immediate sense to you.

5
00:00:17,140 --> 00:00:21,060
Now, a set in Python is unordered and
doesn't contain duplicates, so

6
00:00:21,060 --> 00:00:23,640
in that respect it's
similar to a dictionary.

7
00:00:23,640 --> 00:00:27,730
However, unlike a dictionary,
item's aren't accessed via a key.

8
00:00:27,730 --> 00:00:31,900
In fact, a set is probably more similar
to a collection of dictionary keys.

9
00:00:31,900 --> 00:00:35,630
And the set members are hashed in
the same ways dictionary keys are.

10
00:00:35,630 --> 00:00:38,380
Now, the elements in a set
must be immutable objects.

11
00:00:38,380 --> 00:00:40,210
Again, just like dictionary keys.

12
00:00:40,210 --> 00:00:41,670
They also support the union and

13
00:00:41,670 --> 00:00:44,090
intersection operations
that can be performed.

14
00:00:44,090 --> 00:00:45,110
On sets.

15
00:00:45,110 --> 00:00:48,320
Now sets are generally used less often
than the other data structures we've

16
00:00:48,320 --> 00:00:52,830
looked at so far, but that said, they
can be very useful for cleaning up data.

17
00:00:52,830 --> 00:00:56,520
And we'll look at that after we've seen
how to create and manipulate some sets.

18
00:00:56,520 --> 00:00:57,440
Here in Python 3,

19
00:00:57,440 --> 00:01:02,430
actually since Python 2.6, you can
define a set by using curly braces.

20
00:01:02,430 --> 00:01:04,069
Which is the same way
you define a dictionary.

21
00:01:04,069 --> 00:01:06,580
So let's go through it
doing it two ways so

22
00:01:06,580 --> 00:01:08,300
you can see the two ways to create sets.

23
00:01:08,300 --> 00:01:13,830
I'm gonna start with farm

24
00:01:13,830 --> 00:01:19,140
animals equals

25
00:01:19,140 --> 00:01:24,520
[NOISE]
Run that first of all.

26
00:01:26,800 --> 00:01:27,890
Nothing particularly new there.

27
00:01:29,430 --> 00:01:34,830
And, of course we can go for
animal in farm_animals,

28
00:01:34,830 --> 00:01:38,930
print(animal).

29
00:01:38,930 --> 00:01:46,140
Let's make a bit of a gap here, print And
let's create a set a different way.

30
00:01:46,140 --> 00:01:50,280
So we'll do wild_animals = set.

31
00:01:50,280 --> 00:01:55,970
It's a built-in function and
we'll add some, so a lion.

32
00:01:57,280 --> 00:02:03,540
Oops Lion, tiger, panther,

33
00:02:03,540 --> 00:02:09,020
elephant and hare.

34
00:02:09,020 --> 00:02:16,070
Hare print are animals and

35
00:02:16,070 --> 00:02:22,190
we can do the same for
animal in wildlife animals,

36
00:02:22,190 --> 00:02:24,960
print animals.

37
00:02:24,960 --> 00:02:25,570
So I can run that.

38
00:02:28,930 --> 00:02:30,820
You can see the two ways to create sets.

39
00:02:30,820 --> 00:02:33,900
The first one is just again
very similar to dictionary.

40
00:02:33,900 --> 00:02:39,050
The form for creating dictionary but
we're not actually Supply key there,

41
00:02:39,050 --> 00:02:44,190
so that's how Python knows that this
is in fact a set, and a second way,

42
00:02:44,190 --> 00:02:49,150
what we do is we actually pass the list
to the set built-in function, and

43
00:02:49,150 --> 00:02:51,670
created a set that way.

44
00:02:51,670 --> 00:02:55,290
And not surprisingly, as you saw on
the screen there using the For loop,

45
00:02:55,290 --> 00:02:57,530
we can iterate through
the members of a set and

46
00:02:57,530 --> 00:03:00,840
then ultimately print them out as we
saw on the example on the screen.

47
00:03:00,840 --> 00:03:04,090
Now, actually, adding a new member
to a set is also pretty easy,

48
00:03:04,090 --> 00:03:06,330
using the add method, so
let's go back up to here.

49
00:03:07,420 --> 00:03:09,160
So what we'll do is we'll start the int.

50
00:03:09,160 --> 00:03:11,090
So we'll type farm_animals.

51
00:03:11,090 --> 00:03:14,240
Let's add one for
every set that we've created.

52
00:03:14,240 --> 00:03:18,770
Let's add a horse to our
[NOISE] Animal set and then for

53
00:03:18,770 --> 00:03:21,430
our wild animals let's add
a horse to that as well.

54
00:03:21,430 --> 00:03:28,300
[NOISE]
Okay then we can just oops

55
00:03:30,800 --> 00:03:35,210
print farm animals print wild animals.

56
00:03:37,160 --> 00:03:41,550
Sign we can run that And,
not surprisingly, you can see we now have

57
00:03:41,550 --> 00:03:46,070
the horse added both to the farm animal
set and also to the wild animal set.

58
00:03:46,070 --> 00:03:50,500
And the other thing I want to show you
there as well, if we just check the order

59
00:03:50,500 --> 00:03:54,000
because there is no inherent
ordering with sets, as I mentioned.

60
00:03:54,000 --> 00:04:00,640
So if we run it again, Notice how
the order of these are changing each time.

61
00:04:00,640 --> 00:04:05,260
And that's because of course
the sets are actually un ordered.

62
00:04:05,260 --> 00:04:08,720
And they can't really
confirm a particular order

63
00:04:08,720 --> 00:04:11,530
with sets because they are by
their very nature un ordered.

64
00:04:11,530 --> 00:04:16,910
Now incidentally you work in an era to
use [...] in the right hand margin.

65
00:04:16,910 --> 00:04:18,810
Set, function, method, call.

66
00:04:18,810 --> 00:04:22,000
That the set function call is
used to create the wild set.

67
00:04:22,000 --> 00:04:22,710
The wild animals.

68
00:04:22,710 --> 00:04:24,480
I'll have you click on that and select it.

69
00:04:24,480 --> 00:04:27,230
It's basically saying you
could use a literal for that.

70
00:04:27,230 --> 00:04:30,430
So we're just going or more information
he'll be replaced with a literal.

71
00:04:30,430 --> 00:04:32,430
So it's just basically saying
if use is set here but

72
00:04:32,430 --> 00:04:33,820
you could do something like that and

73
00:04:33,820 --> 00:04:37,530
replace it with that now there are cases
where we have to use the set function.

74
00:04:37,530 --> 00:04:39,500
And one is if we want
to create an empty set.

75
00:04:39,500 --> 00:04:44,350
We can't actually create an empty set
using just the empty braces because that

76
00:04:44,350 --> 00:04:48,780
actually creates an empty dictionary So
in that instance, we have to use set.

77
00:04:48,780 --> 00:04:53,810
And just to show you what I mean,
we'll comment this code out for now.

78
00:04:53,810 --> 00:05:01,420
So we can start off by typing empty_set
= set() and that creates an empty set.

79
00:05:01,420 --> 00:05:06,990
But if we try and
do it this way, empty_set_2 =

80
00:05:08,790 --> 00:05:12,450
Like so, and we do empty_set.add, A,

81
00:05:12,450 --> 00:05:17,850
empty_set_2.add, noting
there's no method there.

82
00:05:17,850 --> 00:05:21,990
If we run this, we get an error,

83
00:05:21,990 --> 00:05:26,700
and just to confirm,
we can see the error should be on line 22.

84
00:05:26,700 --> 00:05:29,100
You can see it's on line 22 there.

85
00:05:29,100 --> 00:05:33,700
And that's because the line on 20,
it actually created a dictionary.

86
00:05:33,700 --> 00:05:35,930
Which, of course,
doesn't have an add method.

87
00:05:35,930 --> 00:05:37,460
So that's why you need
to use the set there.

88
00:05:37,460 --> 00:05:41,350
If you were creating an empty set,
you'd actually physically need to call

89
00:05:41,350 --> 00:05:44,460
the in-built function for
set to create that for you.

90
00:05:44,460 --> 00:05:47,670
And when we used the set constructor,
when we used it earlier.

91
00:05:47,670 --> 00:05:49,760
But you can see online 9
which I've commented out.

92
00:05:49,760 --> 00:05:55,260
We passed the list parameter but
you can actually use any [.] object

93
00:05:55,260 --> 00:06:00,650
such as a [.] or a range to do the same
thing so what we do is something like and

94
00:06:00,650 --> 00:06:04,470
even equals set, range, 042.

95
00:06:04,470 --> 00:06:08,460
Remember that from our
discussion of ranges.

96
00:06:08,460 --> 00:06:08,960
Print even.

97
00:06:10,150 --> 00:06:10,730
We can run that.

98
00:06:11,880 --> 00:06:13,200
Whoops, I'll need to get
rid of that other error,

99
00:06:13,200 --> 00:06:17,780
so I'll just comment that second add out,
and then run that again.

100
00:06:17,780 --> 00:06:22,820
You can see that's created a set based on
those numbers, based on the range there.

101
00:06:22,820 --> 00:06:30,070
And we're gonna do something
like squares_tuple.

102
00:06:30,070 --> 00:06:34,910
= (4,6,9, 16, 25) and squares = set

103
00:06:34,910 --> 00:06:39,970
(squares_turple) then print (squares), so

104
00:06:39,970 --> 00:06:45,990
I can do it that way,

105
00:06:45,990 --> 00:06:51,190
and you can see me correctly get
a set there as well So when you're

106
00:06:51,190 --> 00:06:55,730
creating sets from tuples, ranges, lists,
or literally any other type of object,

107
00:06:55,730 --> 00:06:59,630
you have to use the set constructor,
rather than using curly braces.

108
00:06:59,630 --> 00:07:02,170
Now obviously in this simple
example I've got on screen,

109
00:07:02,170 --> 00:07:05,580
we could do away with the tuple and just
enter the numbers within curly braces, but

110
00:07:05,580 --> 00:07:09,260
the example is showing you that
a tuple can be used to generate a set.

111
00:07:09,260 --> 00:07:10,960
And it's most likely you'd probably,
at some point,

112
00:07:10,960 --> 00:07:12,600
be wanting to do that when
you're programming in Path.

113
00:07:12,600 --> 00:07:16,650
So hopefully, now you're getting an idea,
seeing these numbers consistently come up.

114
00:07:16,650 --> 00:07:19,950
There really is no guarantee that any
item's gonna be printed in order,

115
00:07:19,950 --> 00:07:23,660
and it's really emphasized the unordered
nature of sets in general.

116
00:07:23,660 --> 00:07:26,560
And as you saw in the first
example with the even numbers

117
00:07:26,560 --> 00:07:28,480
They're not even necessarily
appearing in order,

118
00:07:28,480 --> 00:07:30,440
even though they were
actually generated in order.

119
00:07:30,440 --> 00:07:31,920
A very important point there.

120
00:07:31,920 --> 00:07:35,360
So, unfortunately their lack of an order
makes it slightly harder to see

121
00:07:35,360 --> 00:07:35,950
what's going on.

122
00:07:35,950 --> 00:07:39,720
But, let's continue on anyway, and have a
look at it, now using union, intersection,

123
00:07:39,720 --> 00:07:41,480
and difference, as well as subsets.

124
00:07:41,480 --> 00:07:44,290
So, what we're gonna start
with is using union.

125
00:07:44,290 --> 00:07:49,680
So, I'm just gonna comment this other
code out Union being a function of sets.

126
00:07:51,690 --> 00:07:59,440
So we'll put our even in again so even
equals set range, so we'll write 42 again.

127
00:08:00,890 --> 00:08:06,650
Print even, print len even to
establish how many entries there are,

128
00:08:06,650 --> 00:08:08,470
then we'll put squares.

129
00:08:09,960 --> 00:08:15,900
Probably should of just copied and
pasted some of these.

130
00:08:15,900 --> 00:08:19,320
Squares underscore tuple equals 4,
6, 9, 16, 25 again.

131
00:08:19,320 --> 00:08:26,100
Squares = set(squares_tuple).

132
00:08:26,100 --> 00:08:29,320
And we'll just do a print(squares) and

133
00:08:29,320 --> 00:08:32,370
you'll see why we're
doing this in a minute.

134
00:08:32,370 --> 00:08:36,380
Squares_tuple, print(squares) and

135
00:08:36,380 --> 00:08:42,370
print(len(squares)) because the number
of elements is going to make sense and

136
00:08:42,370 --> 00:08:43,400
it's going to be important shortly.

137
00:08:45,650 --> 00:08:49,090
Now, if we go ahead and introduce the
union commands, we're gonna talk print,

138
00:08:52,100 --> 00:08:56,920
even dot union squares, and

139
00:08:56,920 --> 00:09:05,280
print len Even.union(squares).

140
00:09:05,280 --> 00:09:10,390
So if you run that, and
scroll this across, so

141
00:09:10,390 --> 00:09:14,940
that you can see that the new set
that we've created on line 38 and

142
00:09:14,940 --> 00:09:18,240
39 now contains the numbers 9 and 25.

143
00:09:18,240 --> 00:09:21,350
And they obviously weren't in
the original set of even numbers.

144
00:09:21,350 --> 00:09:25,060
And incidentally that's exactly
the same set of squares dot union even.

145
00:09:25,060 --> 00:09:27,490
So we're working back the other
way just to confirm that.

146
00:09:29,260 --> 00:09:33,430
We can also do a print squares.union

147
00:09:33,430 --> 00:09:38,750
even You can see everything exactly
the same output by doing that.

148
00:09:38,750 --> 00:09:40,290
So that's using a union.

149
00:09:40,290 --> 00:09:44,870
Now the intersection of two sets includes
members that are present in both sets.

150
00:09:44,870 --> 00:09:47,660
And again they only appear in
the intersection set once.

151
00:09:47,660 --> 00:09:51,700
Now you can perform intersection using the
and instead of calling the intersection

152
00:09:51,700 --> 00:09:54,560
method Well I wouldn't have used the and
on sets part and

153
00:09:54,560 --> 00:09:58,540
just calls intersection, the in-built
intersection function for you.

154
00:09:58,540 --> 00:10:01,780
So just to see how that would work, let's
just close down the run window again.

155
00:10:03,150 --> 00:10:07,360
So let's just put a bit
of a print here to put

156
00:10:07,360 --> 00:10:10,990
a bit of a gap between the other output,
times 40.

157
00:10:10,990 --> 00:10:18,000
So if we do print Even .intersection

158
00:10:18,000 --> 00:10:21,660
squares, print.

159
00:10:21,660 --> 00:10:22,700
Again, we can do it this way, as well.

160
00:10:22,700 --> 00:10:25,910
We can, using the ampersand, squares.

161
00:10:27,340 --> 00:10:29,570
But again, using the ampersand internally,

162
00:10:29,570 --> 00:10:32,860
Python is just using
the .intersection function anyway.

163
00:10:32,860 --> 00:10:35,080
And likewise, if you do a print squares.

164
00:10:35,080 --> 00:10:41,130
Thought intersection.

165
00:10:41,130 --> 00:10:46,230
Even print squares.

166
00:10:46,230 --> 00:10:50,530
So if you run that code.

167
00:10:50,530 --> 00:10:51,350
combination.

168
00:10:51,350 --> 00:10:52,680
We'll run that again.

169
00:10:55,160 --> 00:10:57,180
You can see we've got
exactly the same output.

170
00:10:57,180 --> 00:10:59,900
These are the numbers that
are present in both sets.

171
00:10:59,900 --> 00:11:05,420
And again looking up here and our original
even set, the number 16 appeared there and

172
00:11:05,420 --> 00:11:08,490
in the second output number 16
appeared and likewise 4 and 4 and 6.

173
00:11:08,490 --> 00:11:16,460
And all the other numbers only appeared
in 1 but not both of the original sets.

174
00:11:16,460 --> 00:11:18,410
So, bottom line here is
using the ampersand.

175
00:11:18,410 --> 00:11:21,880
It really doesn't matter what you
call the intersection method.

176
00:11:21,880 --> 00:11:23,190
You still get the same result either way.

177
00:11:23,190 --> 00:11:27,920
Now, sets can also be subtracted, and
there's two ways to specify a subtraction.

178
00:11:27,920 --> 00:11:30,230
Which way you use is really
just a matter of preference.

179
00:11:30,230 --> 00:11:33,090
Now, we'll cheat a little bit before we
check that because all though Python's

180
00:11:33,090 --> 00:11:35,330
quite happily dealing
with unordered lists,

181
00:11:35,330 --> 00:11:39,230
Us humans find them hard to read when of
course there's more than a few members.

182
00:11:39,230 --> 00:11:43,080
So to make it easier to see what's going
on, what we're gonna do is use the sorted

183
00:11:43,080 --> 00:11:46,210
function to sort the results
before printing them out.

184
00:11:46,210 --> 00:11:48,410
Now this is not necessary
when dealing with sets.

185
00:11:48,410 --> 00:11:49,810
I just wanna make that clear.

186
00:11:49,810 --> 00:11:52,490
We're only doing it here to
make it easier for us to see

187
00:11:52,490 --> 00:11:56,310
what the various methods are actually
doing with the actual sets themselves.

188
00:11:56,310 --> 00:12:01,570
So subtracting set b from set
a removes any item that exists in

189
00:12:01,570 --> 00:12:04,130
set b from set a, if that makes sense.

190
00:12:04,130 --> 00:12:07,470
Just to say that again, so
subtracting set b from set

191
00:12:07,470 --> 00:12:12,900
a removes any item that exists in
set b from The first set said a.

192
00:12:12,900 --> 00:12:15,610
So with our example, so
let's just comment this code out.

193
00:12:15,610 --> 00:12:18,520
And type some code in.

194
00:12:18,520 --> 00:12:21,770
And what I'll do is I'll just
copy this first bit of code.

195
00:12:24,270 --> 00:12:26,210
We do need a little bit of that, and

196
00:12:26,210 --> 00:12:28,640
we'll just make a little space down the
end here, so we can see what we're doing.

197
00:12:28,640 --> 00:12:32,740
So we'll un-comment that.

198
00:12:35,600 --> 00:12:37,300
So we're gonna have our even set again,
but

199
00:12:37,300 --> 00:12:43,140
this time we're gonna do print sorted
even to see what it looks like sorted.

200
00:12:43,140 --> 00:12:47,770
Then we'll leave our square tuple
in again, and we'll put Squares =

201
00:12:47,770 --> 00:12:53,790
setsquares_tuple and
then we'll also print.

202
00:12:58,300 --> 00:13:00,820
Source it squares.

203
00:13:00,820 --> 00:13:03,600
So, you've got a handle
on what it looks like.

204
00:13:03,600 --> 00:13:05,930
All right.
So, let's now do a,

205
00:13:05,930 --> 00:13:07,980
we'll put a heading here just
to show what we're about to do.

206
00:13:07,980 --> 00:13:09,230
So, even.

207
00:13:09,230 --> 00:13:10,060
Money squares.

208
00:13:12,810 --> 00:13:15,200
So do that first, print, sorted.

209
00:13:16,320 --> 00:13:21,530
Even dot difference, since this is what's
subtracted, is difference squares.

210
00:13:21,530 --> 00:13:27,650
So, and print sorted even takes squares,

211
00:13:27,650 --> 00:13:33,550
so the .difference and the minus,
the subtract, does the same thing.

212
00:13:33,550 --> 00:13:39,580
And we'll try squares,
cuz we'll do a heading first, so squares

213
00:13:42,950 --> 00:13:47,800
Minus even, so first, why don't we print

214
00:13:47,800 --> 00:13:54,520
squares.difference even,

215
00:13:54,520 --> 00:13:59,780
and print Squares tack even.

216
00:13:59,780 --> 00:14:03,550
The same deal here using
the dot difference or

217
00:14:03,550 --> 00:14:07,600
using the subtract, the difference
methods exactly the same as using minus.

218
00:14:07,600 --> 00:14:12,120
So which way you use depends on which you
find more readable, in general probably

219
00:14:12,120 --> 00:14:15,040
the recommendation would be that
the difference method is better because it

220
00:14:15,040 --> 00:14:18,290
makes it clear that you're working
with sets which you may not know.

221
00:14:18,290 --> 00:14:19,670
If you're just looking at, say, line 62,

222
00:14:19,670 --> 00:14:21,740
you wouldn't know those
variables are sets.

223
00:14:21,740 --> 00:14:23,210
Or if you saw a .difference,

224
00:14:23,210 --> 00:14:26,380
you'd know that that method
is only applicable to sets.

225
00:14:26,380 --> 00:14:27,250
So let's run this.

226
00:14:28,540 --> 00:14:33,040
So what you can see in the first example
is the subtracting Squares from even,

227
00:14:33,040 --> 00:14:37,390
removes the value 4, 16 and
26 from even, leaving only 9 and 25 and

228
00:14:37,390 --> 00:14:41,800
go the other way subtracting even
from squares, remove 4,16 and

229
00:14:41,800 --> 00:14:44,630
36 from squares, again leaving only 9 and

230
00:14:44,630 --> 00:14:48,500
25 So I'm going to stop the video here
cuz it's getting a little bit long.

231
00:14:48,500 --> 00:14:51,640
In the next video we'll continue our
discussion, and we'll move forward and

232
00:14:51,640 --> 00:14:55,940
we'll talk about the update difference
method as well, which is very similar

233
00:14:55,940 --> 00:15:00,790
to the difference method and the minus,
that we've talked about just now.

234
00:15:00,790 --> 00:15:01,920
So, we'll see you in the next video.

