Set is homogenous and heterogeneous collection of elements.
Sets can be created by placing a comma-separated sequence of elements within curly braces {} or by using the built-in set() function.
Examples:
# Creating a set with curly braces
fruits = {"apple", "banana", "cherry"}
# Creating a set with the set() function
numbers = set([1, 2, 3, 4, 5])
# Creating an empty set
empty_set = set() # Note: {} creates an empty dictionary, not a set
You can add elements to a set using the add() method or the update() method to add multiple elements.
Examples:
fruits = {"apple", "banana", "cherry"}
# Adding a single element
fruits.add("orange")
print(fruits) # Output: {'banana', 'apple', 'cherry', 'orange'}
# Adding multiple elements
fruits.update(["kiwi", "mango"])
print(fruits) # Output: {'banana', 'apple', 'cherry', 'kiwi', 'orange', 'mango'}
You can remove elements from a set using methods like remove(), discard(), and pop().
Examples:
fruits = {"apple", "banana", "cherry"}
# Removing an element with remove() (raises an error if the element is not present)
fruits.remove("banana")
print(fruits) # Output: {'apple', 'cherry'}
# Removing an element with discard() (does not raise an error if the element is not present)
fruits.discard("kiwi")
print(fruits) # Output: {'apple', 'cherry'}
# Removing and returning an arbitrary element with pop()
popped_element = fruits.pop()
print(popped_element) # Output: apple (or cherry, as the set is unordered)
print(fruits) # Output: {'cherry'} (or {'apple'})
Python provides several methods and operators for performing set operations like union, intersection, difference, and symmetric difference.
Union: Combines all elements from both sets (duplicates are removed).
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
# Using the | operator
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5}
Intersection: Returns elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
# Using the & operator
intersection_set = set1 & set2
print(intersection_set) # Output: {3}
Difference: Returns elements that are in the first set but not in the second.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}
# Using the - operator
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}
Symmetric Difference: Returns elements that are in either of the sets, but not in both.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
sym_diff_set = set1.symmetric_difference(set2)
print(sym_diff_set) # Output: {1, 2, 4, 5}
# Using the ^ operator
sym_diff_set = set1 ^ set2
print(sym_diff_set) # Output: {1, 2, 4, 5}
Examples:
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# Checking subset
print(set1.issubset(set2)) # Output: True
# Checking superset
print(set2.issuperset(set1)) # Output: True
# Checking disjoint sets
set3 = {6, 7, 8}
print(set1.isdisjoint(set3)) # Output: True
# Clearing a set
set1.clear()
print(set1) # Output: set()
# Copying a set
set_copy = set2.copy()
print(set_copy) # Output: {1, 2, 3, 4, 5}