We define two sets a
and b
>>> a = {1, 2, 2, 3, 4}
>>> b = {3, 3, 4, 4, 5}
NOTE: {1} creates a set of one element, but {} creates an empty dict. The correct way to create an empty set is set().
a.intersection(b)
returns a new set with elements present in both a
and b
>>> a.intersection(b)
{3, 4}
a.union(b)
returns a new set with elements present in either a
and b
>>> a.union(b)
{1, 2, 3, 4, 5}
a.difference(b)
returns a new set with elements present in a
but not in b
>>> a.difference(b)
{1, 2}
>>> b.difference(a)
{5}
a.symmetric_difference(b)
returns a new set with elements present in either a
or b
but not in both
>>> a.symmetric_difference(b)
{1, 2, 5}
>>> b.symmetric_difference(a)
{1, 2, 5}
NOTE: a.symmetric_difference(b) == b.symmetric_difference(a)
c.issubset(a)
tests whether each element of c
is in a
.
a.issuperset(c)
tests whether each element of c
is in a
.
>>> c = {1, 2}
>>> c.issubset(a)
True
>>> a.issuperset(c)
True