Welcome back.
The methods we're going to 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 are 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 one, two, three.
It's going 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 one, two, three.
What's different between my set and your set when viewed from my set?
Well, your set doesn't have 1 to 3.
What about the next one?
Discard.
Again, if we do my set.
DOD 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 one, two, three, four because we've just discarded five.
It modifies our set just to keep our working space clean.
I'm going to comment this 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.
I get 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.
All right.
A few more to go.
Intersection.
And this one once again, my set.
And in here once again, let's see 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 or five because that's the intersection, the two common things that my set
and your set has.
What about this this joint?
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 is disjoint simply says, Hey, are those two circles overlapping?
Do they have anything in common?
If, for example, my set didn't had four, didn't have four and five and I click run.
I get true because it's disjointed.
It means these sets have nothing in common.
Awesome.
Just a few more.
The next ones we're going to see is is subset and is superset.
But actually, before we do that, let's do union.
Let's see what this does.
Union.
Well her say my set.
And in here we want to add the other set.
Your set.
If we print this.
And Click Run.
I get one, two, three, four, five, six, seven, eight, nine, ten.
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 backslash.
But if you press shift, you'll get this little straight line if you print this.
Well, 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.
And.
And if you do that.
It gives you the intersection of my set and your set.
All right.
I promise you, this is going to be short.
So two other ones is subset and is superset.
As you can imagine, my set if let's say my set only has four and five.
And I say, Hey, it's 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 is.
Subset is.
Superset.
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 want to 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.