Member-only story
2)Set methods in Python
issubset()
Returns True if all elements of the set are present in the specified set, otherwise returns False.
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4}
result = set1.issubset(set2)
print(result)
# Output: Trueissuperset()
Returns True if all elements of the specified set are present in the original set, otherwise returns False.
set1 = {1, 2, 3, 4}
set2 = {1, 2}
result = set1.issuperset(set2)
print(result)
# Output: Trueisdisjoint()
Returns True if the two sets have no common elements, otherwise returns False.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
result = set1.isdisjoint(set2)
print(result)
# Output: Truecopy()
Returns a shallow copy of the set.
set1 = {1, 2, 3}
set_copy = set1.copy()
print(set_copy)
# Output: {1, 2, 3}update() (|=)
Adds elements from another set or iterable to the set. This modifies the original set.
set1 = {1, 2, 3}
set1.update({3, 4, 5})
print(set1) # Output: {1, 2, 3, 4, 5}
set1 = {1, 2, 3}
set1 |= {3, 4, 5}
print(set1) # Output: {1, 2, 3, 4, 5}intersection_update() (&=)
Updates the set by keeping only elements found in both sets.
set1 = {1, 2, 3}
set1.intersection_update({2, 3, 4})…






