Similar to Sets in Mathematics, you can also do operations like union, difference in Python as well. This makes Sets unique as well as increases their usefulness. Moreover for doing an operation on sets there does exist some equivalent method as well. Like for doing union of two sets(Putting two sets together) you can either use | operator or use .union() method. Both works the same way.
Below is a table containing Set Operators/Equivalent methods and description for each one.
Operator | Equivalent Method | Description | Examples |
---|---|---|---|
Intersection (Symbol &) | .intersection() | Makes a new set having only common elements amongst two sets | A & B A.intersection(B) |
Union (Symbol |) | .union() | Putting together elements of two sets | A | B A.union(B) |
Difference (Symbol –) | .difference() | Makes a newer set of elements which are in A set but not in B set | A - B A.difference(B) |
Symmetric Difference (Symbol ^) | .symmetric_difference() | Set of those elements of A, B which are not in both of sets | A ^ B A.symmetric_difference(B) |
Disjoint (No Operator for this in Python) | .isdisjoint() | Checks if two sets have common elements or not | A.isdisjoint(B) |
Issubset (Symbol <=) | .issubset() | Checks if one set is subset of another or not | A <= B A.issubset(B) |
Issuperset (Symbol >=) | .issuperset() | Checks if one set is superset of another or not | A >= B A.issuperset(B) |
Table of Contents
Code Examples of Union(|) or .union() method
# Creating a set
set1 = set({12,3452,1242,3921,2211})
# Crating a set
set2 = set({"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"})
set3 = set1 | set2
print(set3)
# {12,3452,1242,3921,2211,"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"}
set4 = set1.union(set2)
print(set4)
# {12,3452,1242,3921,2211,"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"}
# Here set3 and set4 are same as union operator(|) and union method have same functionality
Code Examples of Intersection(&) or .intersection() method
# Creating a set
set1 = set({64, 29, 19, 272, 12})
# Crating a set
set2 = set({64, 2912, 941})
set3 = set1 & set2
print(set3)
# {64} as set1,set2 have only 64 in common
set4 = set1.intersection(set2)
print(set4)
# {64} as set1,set2 have only 64 in common
# set3 and set4 are same as both Intersection Operator and .intersection() method are same
Code Examples of Difference(-) or .difference() method
# Creating a set
set1 = set({64, 29, 19, 272, 12})
# Crating a set
set2 = set({64, 2912, 941})
set3 = set1 - set2
print(set3)
# {29, 19, 272, 12} as these elements are in set1 but not in set2
set4 = set1.difference(set2)
print(set4)
# {29, 19, 272, 12} as these elements are in set1 but not in set2
# set3 and set4 are same as both Difference Operator and .difference() method are same
Code Examples of Symmetric Difference(^) or .symmetric_difference() method
# Creating a set
set1 = set({73, 281, 291, 647, 3})
# Crating a set
set2 = set({64, 2912, 941, 3, 73})
set3 = set1 ^ set2
print(set3)
# {281, 291, 647, 64, 2912, 941} as these elements are not common amongst set1, set2
set4 = set1.symmetric_difference(set2)
print(set4)
# {281, 291, 647, 64, 2912, 941} as these elements are not common amongst set1, set2
# set3 and set4 are same as both Symmetric Difference Operator and .symmetric_difference() method are same
Code Examples of Disjoint or .isdisjoint() method
# Example 1
# Creating a set
set1 = set({737, 92, 291, 913})
# Crating a set
set2 = set({737, 190, 28, 182, 2})
set3 = set1.isdisjoint(set2)
print(set3)
# Returns False as set1,set2 have 737 element in common
# Example 2
# Creating a set
set1 = set({7137, 192, 2912, 9133})
# Crating a set
set2 = set({99, 9213, 952})
set3 = set1.isdisjoint(set2)
print(set3)
# Returns True as set1, set2 doesn't have any common element
Code Examples of Issubset(<=) or .issubset() method
# Example 1
# Creating a set
set1 = set({737, 2})
# Crating a set
set2 = set({737, 190, 28, 182, 2})
set3 = set1.issubset(set2)
print(set3)
# Returns True as all elements in set1 are there in set2
# Example 2
# Creating a set
set1 = set({737, 2})
# Crating a set
set2 = set({737, 190, 28, 182, 2})
set1 <= set2 # Returns True
Code Examples of Issuperset(>=) or .issuperset() method
# Example 1
# Creating a set
set1 = set({281, 192, 19382, 18492, 241, 13452})
# Crating a set
set2 = set({192, 241})
set3 = set1.issuperset(set2)
print(set3)
# Returns True as set2 have only those elements which are in set1
# Example 2
# Creating a set
set1 = set({281, 192, 19382, 18492, 241, 13452})
# Crating a set
set2 = set({192, 241})
set1 >= set2 # Returns True
No Comments
Leave a comment Cancel