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 10.
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 the difference between my set in your set when viewed from my set?
Well, your set doesn't have one, two, three.
What about the next one?
Discard again if we do my set.
Don't discard.
It says it removes an element from a set if it is a member.
So here we can say discard.
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 Defence 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.
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 an intersection with something else.
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 might
set in your said has.
What about this disjoined?
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 disjoined, simply says, hey, are those two circles overlapping?
Do they have anything in common?
If, for example, my said didn't had four, didn't have four and five and I click run.
I get true because it's disjointed means these sets have nothing in common.
Awesome, just a few more.
The next one we're going to see is is subsect and is superset.
But actually, before we do that, let's do union.
Let's see what this does, Union.
Well, 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, 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, a return to 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, the 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 end.
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, is my set, is that a subset of your set and I print this if I click run.
It's true, 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 a subset is.
I'll get false because what my said 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 seat encompasses everything that my set has, which is four and five.
However, 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 cue 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.