Sets

Sets are unordered collections of unique elements. Sets are created using curly braces {} or the set() function.

# Creating a set of integers
my_set = {1, 2, 3, 4, 5}

# Creating a set of strings
my_strings = set(["apple", "banana", "cherry"])

Sets support mathematical set operations such as union, intersection, and difference.

# Set operations
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.union(set2))  # Output: {1, 2, 3, 4}
print(set1.intersection(set2))  # Output: {2, 3}
print(set1.difference(set2))  # Output: {1}

Sets are often used to eliminate duplicates or to perform set-based operations.

Learn Python : https://learndataengineeringskills.com/category/python/

 

Leave a Comment