Great Job! You have learned about many different ways to work with set
and frozenset
containers! We looked at:
Creating a set
or frozenset
:
- For
set
containers, we can use curly braces{}
, theset()
constructor, or set comprehension. - For
frozenset
containers, we can only use thefrozenset()
constructor.
Adding items to a set
:
- We can add items to a
set
individually using the.add()
method. - We can add multiple items at once using the
.update()
method.
Removing items from a set
:
- The
.remove()
method is used to remove elements from aset
. - The
.discard()
method can also be used to remove elements from aset
. It does not throw aKeyError
if the element is not found.
Finding Elements:
- The
in
keyword can be used withset
andfrozenset
containers to test if an element exists inside of them.
Union:
- A union can be found using
set
orfrozenset
containers with the.union()
method or|
operator.
Intersection:
- An intersection can be found using
set
orfrozenset
containers with the.intersection()
method or&
operator.
Difference:
- The difference can be found using
set
orfrozenset
containers with the.difference()
method or-
operator.
Symmetric Difference:
- The symmetric difference can be found using
set
orfrozenset
containers with the.symmetric_difference()
method or^
operator.
Want to learn more about sets? Check out everything about sets including additional methods, testing for superclass and subclasses, and more from the Python documentation.
Instructions
For these checkpoints, we will review the different operations which you can perform on set
and frozenset
containers.
First, create a frozenset
called my_tags
which contains the elements: 'pop'
, 'electronic'
, 'relaxing'
, 'slow'
, and 'synth'
.
Try finding the union of music_tags
and my_tags
, but make sure to return the result as a frozenset
. Store the result in a variable called frozen_tag_union
.
Now store the intersection of music_tags
and my_tags
into a variable called regular_tag_intersect
. Make sure that it is stored as a normal set
this time.
Now try finding the difference of my_tags
with music_tags
. Store this result in a variable called frozen_tag_difference
. The type
of the result should be a frozenset
.
Finally, get the symmetric difference of music_tags
with my_tags
and store it in a variable called regular_tag_sd
. The result should should be a set
and not a frozenset
.