-: Hi guys, and welcome back.
In this video, we're going to learn about advanced set
operations, that is, why sets are useful.
So let's say we've got some friends, Bob, Rolf, and Anne,
and then we have two friends that are abroad, Bob and Anne.
If only Bob and Anne are abroad, that means that Rolf
must not be abroad, so the local friends are actually
a set with a single element, Rolf.
This is how you define a set with a single element,
exactly the same as with multiple elements,
but you don't have a comma with more elements after it.
So you've got just your single friend
inside the curly braces.
However, if you add more friends or you add Rolf
to the abroad friends, of course this variable
will not change.
That's because you're saying that this variable contains
a set with a string Rolf inside it.
It is not related to these two sets.
So instead of hard-coding these sets to be a single element,
we're going to use these two sets to calculate who is local,
which friends are not abroad.
And the way to do that is by doing friends dot difference
abroad, so what this does is it calls the difference
function inside friends, and this function here takes in
another set, and what this'll do is it'll take this set
and remove from it the elements in this set.
So we'll essentially calculate the difference between
the two sets with this one as the starting point.
So we'll take away Bob and Anne, leaving you just with Rolf.
You can lead, print that out, and you'll see what I mean.
So there we have our single set, Rolf.
Notice that if you do abroad dot difference with friends,
you're gonna get something different, because abroad
has two elements, Bob and Anne, and what you're going to do
is you're going to remove from this set
the elements in this set.
So you're gonna take away Bob and Anne, and Rolf as well,
but Rolf is not even there, so you're gonna end up
with an empty set.
Indeed if we run this, you'll see that you get
an empty set at the bottom.
Now by the way, this is the notation for an empty set.
If you want to create one that's empty with no elements
at all, you have to use set and then normal brackets,
and that is how also Python tells you about an empty set.
So again, difference takes one set and removes the elements
in this other set from it.
If instead of a list of friends and a list of friends
who are abroad, we have a list of local friends
and a list of abroad friends, we can also calculate
the total friends using another set operation.
So what we'll say is friends is now local dot union abroad.
And what union does is it unites two sets
and it gives you the total of both.
Here we will add to Rolf, Bob, and Anne, and we will end up
with a set that contains Rolf, Bob, and Anne.
So if we print friends, you'll see
that that is indeed what we get back.
Now lets say we've got a bunch of friends, some of whom
study art and some of whom study science.
So the friends who study art are Bob, Jen, Rolf, and Charlie
and the ones who study science are Bob, Jen, Adam, and Anne.
So you can see that Bob and Jen actually study both art
and science, Rolf and Charlie study art only,
and Adam and Anne study science only.
You already know how to calculate from these two sets
the ones that study art only, and the ones
that study science only, by using the previous operations.
But what we don't know is how to find out which ones study
both subjects, so that is where intersection comes in.
We can see that another variable,
both is art dot intersection science,
and then if we print both,
you'll see that the output will be Bob and Jen.
I encourage you to have a play-around
with these set operations, because they can be
a little tricky to understand fully, but once
you've given them a go and you've tried them out,
I'm sure it'll all be much clearer.
In the resources section of this lecture,
or in the description below in the video, I'm going to link
a couple of blog posts that we've written
on these set operations that also add a few more
that can be helpful as well, such as symmetric difference.
Thanks for joining me in this video,
and I'll see you in the next one.