Set#
Overview#
A set is an unordered collection (i.e., not maintain any specific order) of unique items. Sets are defined by enclosing elements in curly braces {} or by using the set() constructor.
Note
General syntax
my_set = {`item1`, `item2`, `item3`, ..., `itemN`}
my_set: Set variable nameelement1,element2,element3, …,elementN: Each element is unique and can be of any data type.Elements are separated by commas and enclosed in curly braces
{}.
Examples#
# Create two sets with numbers
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Merge the sets
union_set = set1 | set2
print(union_set) # {1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
# Verify the similarities between two sets
intersection_set = set1 & set2
print(intersection_set) # {3}
{3}
# Find the difference in terms of elements between two sets
difference_set = set1 - set2
print(difference_set) # {1, 2}
{1, 2}