-: Welcome back.
The methods we're gonna talk about
when it comes to sets are right here.
It looks like a lot,
but they're quite similar,
so hang in there.
I have two sets for us,
our my set, which is one through five,
and then your set, which is four through ten.
Now let's go one by one
and see what these methods do.
And if you've ever seen venn diagrams,
in your school for example,
two circles that are overlapping each other,
what we're about to do is quite similar to this.
The first one is difference,
and sets are very useful when having two sets,
and comparing them to each other.
So for example, in here,
difference simply says my set dot difference,
and we give it another set, such as your set.
If I print this,
right here,
and I click run,
I get 1, 2, 3.
It's going to to find the difference
of the first my set, with your set.
So any duplicates between the two,
which is four and five,
gets ignored, and it only shows the difference.
That is 1, 2, 3.
What's different between my set
and your set, when viewed from my set?
Well, your set doesn't have 1, 2, 3.
What about the next one?
Discard.
Again, if we do my set dot discard,
it says it removes an element from a set
if it is a member.
So here, we can say discard five,
and if I do print,
my set,
and I click run,
I get none.
But if I print here,
my set,
and I click run,
I get 1, 2, 3, 4,
because we've just discarded five.
It modifies our set.
Just to keep our working space clean,
I'm going to comment these out.
Next is difference update.
So I'm going to say, my set,
and then once again,
let's see what difference update does.
Remove all elements of another set from this set.
So, if I do, your set here,
and once again, I do print,
and I click run,
again, none, because it just modifies my set.
So remember, we just have to have our print, my set,
and now, this my set,
is updated with just one, two and three,
because we're updating it
so that the differences are removed.
So four and five are removed,
unlike difference,
where it just told you the difference without changing.
Again, if we go back here,
and simply do, my set,
and I click run,
you see that, my set here,
was not modified, right here,
but then, my set bottom, was modified,
because of difference update.
Alright, a few more to go.
Intersection,
and this one, once again, my set.
And in here, once again,
let's read what it does.
It just has intersection, with something else.
Well, let's find out.
If I do your set here,
and I do print, and I run this,
it's going to give me four, five,
because that's the intersection,
the two common things,
that my set and your set has.
What about isdisjoint?
Well, if I do my set and then your set,
I feel like I'm saying those words over and over.
Hopefully, that's not too annoying.
And then here, if, let's comment this out.
We run this, I get false.
Isdisjoint simply says,
hey, are those two circles overlapping,
do they have anything in common?
If for example,
my set didn't have four,
didn't have four and five,
and I click run, I get true,
because isdisjointed,
means these sets have nothing in common.
Awesome, just a few more.
The next ones we're gonna see is,
is issubset,
and issuperset.
But actually before we do that,
let's do union.
Let's see what this does.
Union will say my set,
and in here,
we want to add the other set, your set.
If we print this,
and click run,
I get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
What did that just do?
Well, union just united these sets together,
but removed any duplicates.
That's kind of nice.
And as you can see,
it created a new set for us.
It returned a new set.
By the way,
there's actually a shorthand
that you can do here, in Python,
which is the shift,
and then the key above your enter,
which is right here, this straight line.
So shift, and then the back slash,
but if you press shift,
you'll get this little straight line.
If you print this,
remove the brackets here,
let's run again,
and you see that works, as well.
And by the way,
this also exists for intersection.
Again, you don't see it that often,
but with intersection,
you can do the same thing
where you just do the end,
and if you do that,
it gives you the intersection
of my set and your set.
Alright, I promised you this is gonna be short,
so two other ones, issubset and issuperset.
As you can imagine, my set,
if let's say,
my set only has four and five,
and I say, hey, is my set, is that a subset,
of your set?
And I print this,
if I click run, it's true.
My set is a subset that is,
it's inside of the circle,
of your set,
because four and five, is inside of it.
So my set, the entirety of my set,
is inside of this other set,
which we call a subset.
On the other hand,
we also have this idea of superset.
So if I do,
instead of issubset, issuperset,
I'll get false,
because, well, my set,
is not a superset,
it doesn't encompass your set,
it's the other way around.
Your set is a superset of my set,
because your set encompasses everything
that my set has,
which is four and five.
Oh boy, that was a lot.
And you're asking yourself,
there is no way I'm going to
memorize any of that.
Well, good, I don't memorize
any of this either.
As a matter of fact, I usually go
to the Python documentation,
or just Google around.
And because I know sets exist,
if I ever wanna compare,
let's say two sets, I know to just go here,
and find the tool that I want.
That's the key of a programmer
that I keep mentioning over and over.
It's not memorization.
It's the ability to know
that there's tools that you can use,
and being able to Google for them.
I know it doesn't sound exciting,
because in movies you see everybody typing super fast,
and coding everything, but that's just not real life.
In real life,
you just Google around, find solutions,
because by now you know what sets are,
you know what sets can do,
so that you can Google for methods
that are appropriate to your problem.
I'll see you in the next one.
Bye bye.