# Set Operations(union, intersection, difference and symmetric difference) using Python

## Following program will help university students and beginners to understand the python set operations (union, intersection, difference and symmetric difference)

```"""
Author : ITVoyagers (itvoyagers.in)

Date :1st February 2020

Description : Program to implement Sets and various operations on Sets.
"""
#Implement use of Sets and various operations on Sets.
fruits={"apples","orange","mango","peach","grapes"}
fruits.pop()
print('fruits.pop():',fruits)
fruits.remove('peach')
print("fruits.remove('peach'):",fruits)
#operation on set
A=set(["1","2"])
B=set(["3","2"])

#union
set_union=A.union(B)
print('union',set_union)

#intersection
set_intersection=A & B
print("A intersection B",set_intersection)

#set difference
set_difference=A-set_intersection
print('difference',set_difference)

#symmetric difference
set_symmetric_difference=A^B
print("symetric difference",set_symmetric_difference)

#subset
issubset=A<=B
print('issubset:',issubset)

#superset
issuperset=A>=B
print('issuperset',issuperset)

#copy
set_copy=A.copy()
print('original',A)
print('copy',set_copy)

#Clear
set_to_clear=A.copy()
print('set before use of clear function ',set_to_clear)
set_to_clear.clear()
print('set after use of clear function ',set_to_clear)

#symetric_difference_update
set1=A.symmetric_difference_update(B)
print(set1)

#check the disjoint
print('is A is disjoint B?',A.isdisjoint(B))
print('is A is disjoint A?',A.isdisjoint(A))

```

OUTPUT

```>>>
union {'orange', 'mango', 'grapes', 'apples', 'blue', 'white', 'voilet', 'black', 'peach', 'pink'}
intersection {'orange'}
shades-fruits: {'voilet', 'black', 'blue', 'pink', 'white'}
fruits^shades: {'mango', 'grapes', 'apples', 'blue', 'white', 'voilet', 'black', 'pink', 'peach'}
after fruits.add("pear"): {'orange', 'mango', 'grapes', 'apples', 'peach', 'pear'}
shades.update("orange","red")): {'orange', 'd', 'o', 'g', 'blue', 'a', 'n', 'e', 'white', 'voilet', 'black', 'r', 'pink'}
fruits.pop(): {'mango', 'grapes', 'apples', 'peach', 'pear'}
fruits.remove('peach'): {'mango', 'grapes', 'apples', 'pear'}
union {'2', '1', '3'}
A intersection B {'2'}
difference {'1'}
symetric difference {'1', '3'}
issubset: False
issuperset False
original {'2', '1'}
copy {'2', '1'}
set before use of clear function  {'2', '1'}
set after use of clear function  set()
None
is A is disjoint B? False
is A is disjoint A? False
```