Sets are defined by placing a comma-separated sequence of values inside curly braces {} or by using the set() function.
my_set = {1, 2, 3}
another_set = set(['a', 'b', 'c'])
You can create sets using curly braces {} or the set() constructor.
Using Curly Braces:
my_set = {1, 2, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}
Using the set() Constructor:
another_set = set([1, 2, 3, 4])
print(another_set) # Output: {1, 2, 3, 4}
You can add elements to a set using the add() and update() methods.
add(): Adds a single element to the set.
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
update(): Adds multiple elements to the set. Accepts any iterable.
my_set = {1, 2, 3}
my_set.update([4, 5])
print(my_set) # Output: {1, 2, 3, 4, 5}
You can remove elements using the remove(), discard(), and pop() methods.
remove(): Removes a specified element. Raises a KeyError if the element is not found.
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4}
discard(): Removes a specified element. Does not raise an error if the element is not found.
my_set = {1, 2, 3, 4}
my_set.discard(3)
print(my_set) # Output: {1, 2, 4}pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.
my_set = {1, 2, 3, 4}
element = my_set.pop()
print(element) # Output: (One of the elements from the set)
print(my_set) # Output: (Remaining elements in the set)
clear(): Removes all elements from the set.
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set) # Output: set()
Sets support various mathematical operations like union, intersection, difference, and symmetric difference.
union(): Returns a set containing all elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # or a | b
print(union_set) # Output: {1, 2, 3, 4, 5}
intersection(): Returns a set containing only the common elements between sets.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2) # (or) a & b
print(intersection_set) # Output: {2, 3}
difference(): Returns a set containing elements that are in the first set but not in the second set.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference_set = set1.difference(set2) # or a-b
print(difference_set) # Output: {1}
symmetric_difference(): Returns a set containing elements that are in either set, but not in both.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
sym_diff_set = set1.symmetric_difference(set2) # (or) a ^ b
print(sym_diff_set) # Output: {1, 4}
# Creating setss1 = {1, 2, 3, 4}s2 = {3, 4, 5, 6}print(s1) # Output: {1, 2, 3, 4}print(s2) # Output: {3, 4, 5, 6}# Adding elementss1.add(5)print(s1) # Output: {1, 2, 3, 4, 5}# Removing elementss1.remove(5)print(s1) # Output: {1, 2, 3, 4}# Unionprint(s1.union(s2)) # Output: {1, 2, 3, 4, 5, 6}# Intersectionprint(s1.intersection(s2)) # Output: {3, 4}# Differenceprint(s1.difference(s2)) # Output: {1, 2}# Symmetric Differenceprint(s1.symmetric_difference(s2)) # Output: {1, 2, 5, 6}# Membership testprint(2 in s1) # Output: Trueprint(7 in s1) # Output: False# Subset, Superset, and Disjoints3 = {1, 2}print(s3.issubset(s1)) # Output: Trueprint(s1.issuperset(s3)) # Output: Trueprint(s1.isdisjoint({6, 7})) # Output: True