Python Set Methods

Sets in Python are unordered collections of unique elements. Python provides several methods to work with sets, many of which are useful for mathematical operations like union, intersection, and difference.

Common Set Methods

Example Usage

Python
# Example of using set methods
set1 = {1, 2, 3}
set2 = {2, 3, 4}

print(set1.union(set2))  # {1, 2, 3, 4}
print(set1.intersection(set2))  # {2, 3}
print(set1.difference(set2))  # {1}