1
00:00:00,520 --> 00:00:02,190
Hi guys, and welcome back.

2
00:00:02,190 --> 00:00:04,940
In this video, we're going to learn about advanced set

3
00:00:04,940 --> 00:00:09,350 line:15% 
operations, that is, why sets are useful.

4
00:00:09,350 --> 00:00:13,420
So let's say we've got some friends, Bob, Rolf, and Anne,

5
00:00:13,420 --> 00:00:16,763
and then we have two friends that are abroad, Bob and Anne.

6
00:00:17,690 --> 00:00:20,500
If only Bob and Anne are abroad, that means that Rolf

7
00:00:20,500 --> 00:00:25,450
must not be abroad, so the local friends are actually

8
00:00:25,450 --> 00:00:28,140
a set with a single element, Rolf.

9
00:00:28,140 --> 00:00:30,480
This is how you define a set with a single element,

10
00:00:30,480 --> 00:00:32,670
exactly the same as with multiple elements,

11
00:00:32,670 --> 00:00:35,150
but you don't have a comma with more elements after it.

12
00:00:35,150 --> 00:00:37,160
So you've got just your single friend

13
00:00:37,160 --> 00:00:38,563
inside the curly braces.

14
00:00:39,640 --> 00:00:43,230
However, if you add more friends or you add Rolf

15
00:00:43,230 --> 00:00:45,620
to the abroad friends, of course this variable

16
00:00:45,620 --> 00:00:46,850
will not change.

17
00:00:46,850 --> 00:00:50,120
That's because you're saying that this variable contains

18
00:00:50,120 --> 00:00:52,970
a set with a string Rolf inside it.

19
00:00:52,970 --> 00:00:55,920
It is not related to these two sets.

20
00:00:55,920 --> 00:01:00,580
So instead of hard-coding these sets to be a single element,

21
00:01:00,580 --> 00:01:04,860
we're going to use these two sets to calculate who is local,

22
00:01:04,860 --> 00:01:07,000
which friends are not abroad.

23
00:01:07,000 --> 00:01:12,000
And the way to do that is by doing friends dot difference

24
00:01:12,210 --> 00:01:17,100
abroad, so what this does is it calls the difference

25
00:01:17,100 --> 00:01:20,520
function inside friends, and this function here takes in

26
00:01:20,520 --> 00:01:25,190
another set, and what this'll do is it'll take this set

27
00:01:25,190 --> 00:01:28,860
and remove from it the elements in this set.

28
00:01:28,860 --> 00:01:31,040
So we'll essentially calculate the difference between

29
00:01:31,040 --> 00:01:34,550
the two sets with this one as the starting point.

30
00:01:34,550 --> 00:01:38,293
So we'll take away Bob and Anne, leaving you just with Rolf.

31
00:01:39,500 --> 00:01:44,500
You can lead, print that out, and you'll see what I mean.

32
00:01:46,710 --> 00:01:49,541 line:15% 
So there we have our single set, Rolf.

33
00:01:49,541 --> 00:01:54,541
Notice that if you do abroad dot difference with friends,

34
00:01:55,230 --> 00:01:58,180
you're gonna get something different, because abroad

35
00:01:58,180 --> 00:02:01,970
has two elements, Bob and Anne, and what you're going to do

36
00:02:01,970 --> 00:02:04,900
is you're going to remove from this set

37
00:02:04,900 --> 00:02:06,880
the elements in this set.

38
00:02:06,880 --> 00:02:09,160
So you're gonna take away Bob and Anne, and Rolf as well,

39
00:02:09,160 --> 00:02:11,730
but Rolf is not even there, so you're gonna end up

40
00:02:11,730 --> 00:02:13,100
with an empty set.

41
00:02:13,100 --> 00:02:15,840
Indeed if we run this, you'll see that you get

42
00:02:15,840 --> 00:02:17,420 line:15% 
an empty set at the bottom.

43
00:02:17,420 --> 00:02:20,482 line:15% 
Now by the way, this is the notation for an empty set.

44
00:02:20,482 --> 00:02:23,420 line:15% 
If you want to create one that's empty with no elements

45
00:02:23,420 --> 00:02:26,950 line:15% 
at all, you have to use set and then normal brackets,

46
00:02:26,950 --> 00:02:29,800 line:15% 
and that is how also Python tells you about an empty set.

47
00:02:30,960 --> 00:02:35,850
So again, difference takes one set and removes the elements

48
00:02:35,850 --> 00:02:37,393
in this other set from it.

49
00:02:38,590 --> 00:02:41,450
If instead of a list of friends and a list of friends

50
00:02:41,450 --> 00:02:44,540
who are abroad, we have a list of local friends

51
00:02:44,540 --> 00:02:48,376
and a list of abroad friends, we can also calculate

52
00:02:48,376 --> 00:02:53,100
the total friends using another set operation.

53
00:02:53,100 --> 00:02:58,100
So what we'll say is friends is now local dot union abroad.

54
00:02:59,080 --> 00:03:02,780
And what union does is it unites two sets

55
00:03:02,780 --> 00:03:05,810
and it gives you the total of both.

56
00:03:05,810 --> 00:03:10,400
Here we will add to Rolf, Bob, and Anne, and we will end up

57
00:03:10,400 --> 00:03:13,050
with a set that contains Rolf, Bob, and Anne.

58
00:03:13,050 --> 00:03:15,600 line:15% 
So if we print friends, you'll see

59
00:03:15,600 --> 00:03:17,593 line:15% 
that that is indeed what we get back.

60
00:03:18,945 --> 00:03:22,260
Now lets say we've got a bunch of friends, some of whom

61
00:03:22,260 --> 00:03:24,990
study art and some of whom study science.

62
00:03:24,990 --> 00:03:28,140
So the friends who study art are Bob, Jen, Rolf, and Charlie

63
00:03:28,140 --> 00:03:30,890
and the ones who study science are Bob, Jen, Adam, and Anne.

64
00:03:30,890 --> 00:03:33,560
So you can see that Bob and Jen actually study both art

65
00:03:33,560 --> 00:03:36,360
and science, Rolf and Charlie study art only,

66
00:03:36,360 --> 00:03:38,713
and Adam and Anne study science only.

67
00:03:40,010 --> 00:03:43,100
You already know how to calculate from these two sets

68
00:03:43,100 --> 00:03:45,273
the ones that study art only, and the ones

69
00:03:45,273 --> 00:03:48,680
that study science only, by using the previous operations.

70
00:03:48,680 --> 00:03:51,850
But what we don't know is how to find out which ones study

71
00:03:51,850 --> 00:03:55,823
both subjects, so that is where intersection comes in.

72
00:03:56,780 --> 00:03:58,070
We can see that another variable,

73
00:03:58,070 --> 00:04:02,103
both is art dot intersection science,

74
00:04:03,240 --> 00:04:05,510
and then if we print both,

75
00:04:05,510 --> 00:04:08,223 line:15% 
you'll see that the output will be Bob and Jen.

76
00:04:09,820 --> 00:04:11,690
I encourage you to have a play-around

77
00:04:11,690 --> 00:04:13,980
with these set operations, because they can be

78
00:04:13,980 --> 00:04:17,650
a little tricky to understand fully, but once

79
00:04:17,650 --> 00:04:19,650
you've given them a go and you've tried them out,

80
00:04:19,650 --> 00:04:22,300
I'm sure it'll all be much clearer.

81
00:04:22,300 --> 00:04:24,060 line:15% 
In the resources section of this lecture,

82
00:04:24,060 --> 00:04:26,230 line:15% 
or in the description below in the video, I'm going to link

83
00:04:26,230 --> 00:04:28,740 line:15% 
a couple of blog posts that we've written

84
00:04:28,740 --> 00:04:32,730 line:15% 
on these set operations that also add a few more

85
00:04:32,730 --> 00:04:36,300 line:15% 
that can be helpful as well, such as symmetric difference.

86
00:04:36,300 --> 00:04:37,770 line:15% 
Thanks for joining me in this video,

87
00:04:37,770 --> 00:04:39,420 line:15% 
and I'll see you in the next one.

