Student Community
Resources ### union() Function

The
union()

method
combines
all
the
elements
of
two
or
more
sets
into
a
single
set
.
You
can
multiple
sets
as
arguments
inside
the
union()
function
to
get
the
union.
set_union = set_1.union(set_2, set_3)
#same as set_union = set_2.union(set_1, set_3)
set_1 = {1, 2, 3}
set_2 = {2, 3, 4}
set_union = set_1.union(set_2)
print(set_union)

### intersection() Function

The
intersection()

method
finds
the
common
elements
between
two
or
more
sets
and
stores
them
in
a
single
set.
You
can
multiple
sets
as
arguments
inside
the
intersection()

function
to
get
the
intersection.
set_intersect = set_1.intersection(set_2, set_3)
#same as set_intersect = set_2.intesection(set_1, set_3)
set_1 = {1, 2, 3}
set_2 = {2, 3, 4}
set_intersect = set_1.intersection(set_2)
print(set_intersect)

### difference() Method

The
difference()

method
removes
the
common
elements
between
two
or
more
sets.
It
returns
a
set
with
elements
present
in
the
first
set,
but
not
in
others.
set_diff = set_1.difference(set_2)
#returns set_1 - set_2
set_diff = set_2.difference(set_1)
#returns set_2 - set_1
set_1 = {1, 2, 3}
set_2 = {2, 3, 4}
set_diff = set_1.difference(set_2, set_3)
print(set_diff)

function
can
be
used
to
an
element
to
a
set.
set_name.add(element)
set_1 = {1, 2, 3}
print(set_1)

function
can
be
used
to
remove
an
element
from
a
set.
set_name.discard(element)
set_1 = {1, 2, 3}
print(set_1)

### copy() Function

copy()

function
allows
you
to
make
a
copy
of
a
set.
Note

-
new_set
=
old_set
does
not
work,
it
will
just
give
a
new
name
to
the
old
set,
it
will
not
create
a
new
copy.
new_set=old_set.copy()
set_1 = {1, 2, 3}
set_new=set_1.copy()
print(set_new)