- [Instructor] In this video, I'm going to
focus on the mutable set operators and methods.
And those are operators and methods you can use on
a variable that refers to a set object to
modify the set that the variable refers to.
In this particular slide, we're
looking at some of the mutable operations.
First, we have the augmented assignments for the
union operation, intersection operation,
difference operation, and symmetric difference operation.
In the bullets below each of those,
we have the corresponding method names that
can perform the same operations.
Now, the key difference once again between, in this case,
the assignments and the methods, is that the assignments
have to have sets on both the left and the right sides of
the operator and in the case of the left side of the
operator, it has to be something that I can modify.
So, it's typically going to be a
variable that refers to an existing set object.
And on the method case or in the method cases,
each of these methods not only can receive as an argument,
a set, but can receive any sequence of arguments and
turn that into a set and then apply the operation.
So, that's some of the operations
we're going to look at in this video.
We'll also look at various methods for
adding and removing elements in sets.
So, let's go switch over to a terminal window.
And, to get us started, let me go ahead and
create a numbers variable that refers to a set object,
containing the unique values one, three, and five.
And let's demonstrate one of those augmented assignments.
Now, I'm not going to demonstrate all of them because they
work the same basic way as the mathematical set operations
we just looked at a few moments ago, two videos ago.
And therefore, there's no need to show them all again.
But, let's just show that you can,
in fact, modify an existing set.
So here we're going to do a union assignment operation.
We're going to add all of the unique values from this set
into the existing set, as long as they are not duplicates.
And to confirm that, we can evaluate numbers and see that
the duplicated three, of course, does not appear twice but
all of the other unique values do indeed appear.
Now, just as we can do that with the
augmented assignment, we can also do that with the
update method called in the case of a union operation.
So let's do this, let's use a range call,
which we know produces a sequence of integers,
zero through nine, in this case because the argument is 10.
And now what's going to happen is the
update method will create a set out of the values from
zero through nine, all of which happen to be unique.
And it will then take all of the unique items from that set,
combine them with the numbers set and
remove any duplicates to produce the resulting set.
So, if we go ahead and say numbers here, you can see we now
have a set containing the values from zero through nine.
So, again, all of the different augmented assignments and
corresponding methods will work similarly to this,
but of course performing their individual operations.
Now, we also have the ability to add items that
are not already in a set, using the add method.
So, for example, if I say numbers dot add and
I give it as an argument, 17, which is clearly not in
this set yet, if I evaluate numbers after doing that,
we can see indeed 17 was added.
Of course, if I say numbers dot add and I give
it a value like three that's already in there,
whoops I spelled numbers wrong, so let's do that again,
numbers dot add and give it a three,
and then I evaluate numbers again, you can see that the
set has not changed from back up here in snippet seven.
Now, sometimes you may wanna remove an item from a set and
it turns out there's several different ways to do that.
So, let's say we would like to remove the value three.
So, if I go ahead and say numbers dot remove with three as
an argument, now three is in the set at the moment so
this will work, if in fact the value was not in the set,
I'd actually get a key error as a result of that.
So you can try that on your own.
But, let's go and evaluate numbers and now we can see that
the value three is no longer in the set.
So you have the remove method to remove an item and
if the item is not there, you'll get
an exception called a key error.
If you simply want to get rid of an item and
you don't care whether it was there or not, you can
actually use a method named discard instead of remove.
Discard does exactly the same thing but does not
cause an exception if the value is not in the set.
Now, if you don't care which value you remove,
you just want to remove a value,
perhaps you're working your way through the set and
you just wanna grab one value at a time and
do something with it, you can use
the pop method for that purpose.
And because sets are unordered, you should not rely on
what value will come out next each time you call pop.
In this case the value zero was returned.
And if I now go and evaluate numbers,
I can see that zero is no longer in the set.
And finally, if you ever needed to completely clear out a
set, you, of course, could use the clear method as we
have on other of the built in collections in Python.
And if I evaluate numbers after that,
I can see that I get the empty set representation.
And again, because curly braces with
nothing in them represent an empty dictionary for
an empty set, they display that as the
word set followed by an empty set of parentheses.