1
00:00:05,200 --> 00:00:09,440
We'll finish this section by looking at the last 
of Python's built-in data structures – sets. 

2
00:00:09,440 --> 00:00:14,000
A set is an unordered collection 
with no duplicate entries. Python 

3
00:00:14,000 --> 00:00:19,200
sets work the same way as they do in set theory.
If you don't know about set theory, don't worry. 

4
00:00:20,080 --> 00:00:22,480
We're not going to go into the 
mathematics of set theory here. 

5
00:00:23,200 --> 00:00:26,960
Set theory is an interesting, and 
important, branch of mathematics. 

6
00:00:27,760 --> 00:00:30,960
But we're going to use Python sets to 
solve some common programming problems. 

7
00:00:31,680 --> 00:00:33,680
I won't be getting all 
mathematical in these videos. 

8
00:00:36,080 --> 00:00:40,160
If you are familiar with set theory, then 
you'll find that Python sets behave exactly 

9
00:00:40,160 --> 00:00:43,840
how you'd expect. You can perform 
all the usual set operations on them. 

10
00:00:44,720 --> 00:00:48,480
We'll be looking at those operations in a 
non-mathematical context, but you'll have 

11
00:00:48,480 --> 00:00:52,720
no problem applying them to mathematical uses 
of sets, if that's something you need to do. 

12
00:00:53,520 --> 00:00:56,640
I'll be using slides to explain things 
like set union and intersection. 

13
00:00:57,520 --> 00:01:01,680
Fast forward those parts of the videos, if 
you're already familiar with those operations. 

14
00:01:04,000 --> 00:01:10,000
The keys of a dictionary are very similar to 
a set. The main difference, since Python 3.7, 

15
00:01:10,000 --> 00:01:13,360
is that dictionary keys are ordered.
Sets have no ordering. 

16
00:01:14,080 --> 00:01:17,920
That's an important point, and explains 
why you can't do certain things with sets. 

17
00:01:18,800 --> 00:01:22,080
For example, there's no way to 
access individual elements of a set. 

18
00:01:22,800 --> 00:01:26,640
We'll see that sets are unordered, when we 
iterate over some sets in the next video. 

19
00:01:27,440 --> 00:01:30,160
Before that, I'll introduce 
the basic set operations. 

20
00:01:32,560 --> 00:01:36,960
Here, we've got two sets. They 
represent a collection of farm animals, 

21
00:01:36,960 --> 00:01:39,840
on the left, and wild animals, on the right. 

22
00:01:40,400 --> 00:01:43,600
The sets are over-simplified, to 
keep them easier to understand. 

23
00:01:44,400 --> 00:01:49,040
You'd struggle to find wild cows these days, but 
there are wild sheep in many parts of the world. 

24
00:01:49,840 --> 00:01:53,840
We're keeping things simple: sheep 
is only in the farm_animals set. 

25
00:01:55,680 --> 00:02:00,240
When we represent sets with circles, like these, 
it's easier to see that they are unordered. 

26
00:02:00,960 --> 00:02:06,000
A set is just a collection of items. Our 
animals could wander about inside their circles, 

27
00:02:06,000 --> 00:02:08,080
but the set would still contain the same items. 

28
00:02:10,400 --> 00:02:14,160
The most common operation we can perform 
on a set, is to test for membership. 

29
00:02:14,960 --> 00:02:18,960
Looking at the farm_animals set, we can 
see that cow is a member of farm_animals. 

30
00:02:20,240 --> 00:02:24,560
hen, sheep, goat, and horse are also 
members of the farm_animals set. 

31
00:02:26,880 --> 00:02:29,680
In Python, we test for 
membership using the in keyword. 

32
00:02:30,400 --> 00:02:34,720
We've seen that with lists, tuples and 
dictionaries, and it works the same with sets. 

33
00:02:35,600 --> 00:02:40,800
tiger is a member of the wild_animals set, so the 
condition 'tiger' in wild_animals will be True. 

34
00:02:43,120 --> 00:02:47,920
Testing if something is in a set can make our code 
more efficient. It's also a good way to fix a bug 

35
00:02:47,920 --> 00:02:53,040
that we had, in our very early menu programs.
We'll have a look at that, in the next video. 

36
00:02:55,280 --> 00:02:59,360
The union of two or more sets, is the set 
of all items that exist in all the sets. 

37
00:03:00,240 --> 00:03:04,080
Here, we have the union of 
the farm_animals and wild_animals sets. 

38
00:03:06,480 --> 00:03:11,040
In Python, the union of these two sets is written:
farm_animals.union(wild_animals) 

39
00:03:13,440 --> 00:03:17,360
or
farm_animals | wild_animals 

40
00:03:18,960 --> 00:03:23,600
One important point to notice, looking at our 
union set, is that items only appear once. 

41
00:03:24,400 --> 00:03:28,320
The elements of a set are unique. 
That's part of the definition of a set. 

42
00:03:29,120 --> 00:03:32,880
goat and horse appear in both sets, but 
they only appear once in the union set. 

43
00:03:35,120 --> 00:03:37,600
It doesn't make sense for something 
to be in a set more than once. 

44
00:03:38,480 --> 00:03:42,160
That property of sets – that the 
items are unique (or distinct, 

45
00:03:42,160 --> 00:03:44,960
in mathematical terms) – 
can be useful in our code. 

46
00:03:45,760 --> 00:03:50,320
Converting a list to a set, for example, will 
automatically remove any duplicate values. 

47
00:03:52,640 --> 00:03:55,440
Once again, we'll see some 
examples of this, later. 

48
00:03:56,240 --> 00:03:59,840
At the moment, we're just learning the 
basic terminology that applies to sets. 

49
00:04:02,080 --> 00:04:06,160
The intersection of two or more sets, is 
the set of items that appear in all sets. 

50
00:04:06,960 --> 00:04:10,640
That's quite easy to remember – it's the 
items that appear in the area where our 

51
00:04:10,640 --> 00:04:16,079
circles intersect. We can see that horse and goat 
are in both sets, and appear in the orange area. 

52
00:04:18,480 --> 00:04:22,320
If we had a third set, of animals 
that can be ridden, for example, 

53
00:04:22,320 --> 00:04:25,680
then horse would probably be the only 
animal in the intersection of all 3 sets. 

54
00:04:26,480 --> 00:04:30,840
It may be possible to ride a goat, 
but it's certainly not very common . 

55
00:04:32,160 --> 00:04:34,480
The Python code representing 
this intersection would be: 

56
00:04:35,280 --> 00:04:36,880
farm_animals.intersection(wild_animals) 

57
00:04:39,120 --> 00:04:41,840
or
farm_animals & wild_animals 

58
00:04:44,560 --> 00:04:50,320
You can also subtract one set from another. Here, 
we've subtracted wild_animals from farm_animals. 

59
00:04:51,600 --> 00:04:56,480
That leaves cow, sheep and hen. Any 
items from wild_animals are removed, 

60
00:04:56,480 --> 00:04:58,000
when we subtract the two sets. 

61
00:05:00,400 --> 00:05:04,960
In Python, we'd write that as either
farm_animals - wild_animals 

62
00:05:06,160 --> 00:05:08,160
or
farm_animals.difference(wild_animals) 

63
00:05:12,400 --> 00:05:14,720
Set theory also defines a symmetric difference. 

64
00:05:15,520 --> 00:05:19,600
The symmetric difference is the set of items 
that are in one set or the other, but not both. 

65
00:05:21,920 --> 00:05:25,440
In this example, the symmetric difference 
doesn't include goat and horse. 

66
00:05:26,320 --> 00:05:29,440
All the animals in the orange areas are 
included in the symmetric difference. 

67
00:05:30,240 --> 00:05:32,720
It's the opposite of the 
intersection of 2 or more sets. 

68
00:05:35,120 --> 00:05:39,040
In Python, we'd write that as:
farm_animals.symmetric_difference(wild_animals) 

69
00:05:42,160 --> 00:05:45,040
or
farm_animals ^ wild_animals 

70
00:05:47,760 --> 00:05:51,920
One set can also be a subset of another set.
We use the normal comparison 

71
00:05:51,920 --> 00:06:01,440
operators; <, <=, > and >=, to check for subsets.
But that's enough slides, and theory, for now. 

72
00:06:02,240 --> 00:06:05,840
We'll leave subsets and supersets until 
we've had some practice with using sets. 

73
00:06:08,160 --> 00:06:11,840
See you in the next video.

