union()
Use the union() method to convert more than one set into a single set.
a={"orange","yellow","black","pink"}
b={"pink","white","grey","green","black"}
print(a.union(b))
''''
Output:
{'white', 'orange', 'pink', 'grey', 'black', 'yellow', 'green'}
''''
Description:
There are 4 items in the set a. There are 5 items in the set b. The statement a.union(b) combines the items in b with a to form a complete set.
But what should be noted is that there is an item called pink in the set called a. Similarly, there is an item called pink in the set called b. But we don't get two pink items in the output. Only one pink item comes. Because set doesn't return the same item.
intersections()
The intersection() method takes only common items that are in more than one set. Returns the retrieved items as a new set.
a={"orange","yellow","pink"}
b={"pink","grey","green"}
print(a.intersection(b))
''''
Output:
{'pink'}
''''
Description:-
The statement a.intersection(b) takes only the item “pink” in the sets a and b because it is common to this item.
intersection_update()
This method works like Intersection(), but usually updates the retrieved items to the original set.
a={"orange","pink"}
b={"pink","green"}
a.intersection_update(b)
print(a)
''''
Output:
{'pink'}
''''
Description:-
a.intersection_update(b) -> Here the value pink is common in set a and b, so it is taken and updated in set a.
print(a) -> Now when you print the set named a, there is only item named pink in it.
difference()
a={"green","pink"}
b={"pink","yellow"}
print(a.difference(b))
''''
Output:
{'green'}
''''
Description:-
Set a has two items green and pink.
The item pink is in set b, so pink is a common item.
The difference() method returns us the non-common green items in set a.
difference_update()
a={"green","pink"}
b={"pink","yellow"}
a.difference_update(b)
print(a)
''''
output:
{'green'}
''''
symmetric_difference()
This method takes only the value that is not common to the two given sets.
a={"green","pink"}
b={"pink","yellow"}
print(a.symmetric_difference(b))
''''
Output:
{'yellow', 'green'}
''''
Description:-
Pink is common item in both sets a and b.
The symmetric_difference() method returns the remaining items excluding the pink item.
symmetric_difference_update()
a={"green","pink"}
b={"pink","yellow"}
a.symmetric_difference_update(b)
print(a)
''''
output:
{'yellow', 'green'}
''''
issubset()
If all the elements of a set are in another set, it is called a subset.
The issubset() method returns true if the set is a subset and false otherwise.
a={"pink"}
b={"pink","yellow","green"}
print(a.issubset(b))
''''
Output:
True
''''
Description:-
Since all elements in set a are in set b, we get true output.
issuperset()
a={"pink"}
b={"pink","green"}
print(a.issuperset(b))
''''
Output:
False
''''
Description:-
Since all the elements in the set b are not in the set a, it returns false in the output.
isdisjoint()
This method returns true if there is a different item in both sets and false otherwise.
a={"pink","green"}
b={"yellow","grey"}
print(a.isdisjoint(b))
''''
Output:
True
''''
Description:-
Since set-a and set-b do not have a common value, the value true is available as output.