These methods can accept any iterable as an argument i.e list,tuple,set etc.
Operator based counterpart requires both arguments to be set.
Every operation on python set is same as we do in set theory using formulas or Venn Diagrams. Let us look over each operation on sets with an example to clarify the syntax details.
For all the given examples consider:-
A = {2,4,6,8}
B = {3,6,9}
1.union(self,*iterable) – It returns the copy of union of sets as a new set.
>>> C = A.union(B)
>>> print(C)
{2, 3, 4, 6, 8, 9}
#operator based
>>> print(A|B)
{2, 3, 4, 6, 8, 9}
2.intersection(self,*iterable) – It returns the copy of intersection of sets as a new set.
>>> C = A.intersection(B)
>>> print(C)
{6}
#operator based
>>> print(A&B)
{6}
3.intersection_update(*iterable) – Update the set with elements found in all sets.
>> B.intersection_update(A)
>>> print(B)
{6}
#operator based
>>> B&=A
>>> print(B)
{6}
4.difference(self,*iterable) – Return a new set with elements which aren’t in others.
>>> C = A.difference(B)
>>> print(C)
{8, 2, 4}
>>> print(A-B) #operator based
{8, 2, 4}
5.difference_update(self,*iterable) – Update the set with elements that aren’t present in others.
>>> B.difference(A)
>>> print(B)
{9, 3}
>>> B-=A #operator based
print(B)
{9, 3}
6. symmetric_difference(self,iterable) – Return the set of element in either the set but not both.
>>> C = A.symmetric_difference(B)
>>> print(C)
{2, 3, 4, 8, 9}
>>> print(A^B) #operator based
{2, 3, 4, 8, 9}
7. symmetric_difference_update(self,iterable) – Update the set with element in any set but not both.
>>> B.symmetric_difference_update(A)
>>> print(B)
{2, 3, 4, 8, 9}
#operator based
>>> B^=A
>>> print(B)
{2,3,4,8,9}
8.issubset(self,iterable) – Check if every element of set is in iterable.
>>> B.issubset(A)
False
>>> B<=A
False
9.issuperset(self,iterable) – Check if every element of iterable is in set.
>>> B.issuperset(A)
False
>>> B>=A
False