Example 1:How to create a set

      
          # Create a basic set containing unique elements,Pycent.com
basic_set = {1, 2, 3, 4, 5}
print("Basic Set:", basic_set)
# #output:Basic Set: {1, 2, 3, 4, 5}          

Example 2:How to Add an Element to a set

      
          
# Add an element to a set,Pycent.com
basic_set = {1, 2, 3, 4, 5}
basic_set.add(6)
print("Set after adding an element:", basic_set)
#output:Set after adding an element: {1, 2, 3, 4, 5, 6}          

Example 3:How to Add multiple Elements to a set

      
          
# how to add multiple elements to a set,Pycent.com
my_set = {1, 2, 3}

# Elements to add
elements_to_add = [4, 5, 6, 7, 8]

# Adding elements using a loop
for element in elements_to_add:
    my_set.add(element)

print("Set after adding elements with a loop:", my_set)
# Set after adding elements with a loop: {1, 2, 3, 4, 5, 6, 7, 8}          

Example 4:How to use Update() method to add elements to a set

      
          # How to use the update method to add elements in a set, Pycent.com
my_set = {1, 2, 3}

# Elements to add
elements_to_add = [24, 25, 26, 27, 28]

# Adding elements using update()
my_set.update(elements_to_add)

print("Set after adding elements with update():", my_set)
# Output:Set after adding elements with update(): {1, 2, 3, 24, 25, 26, 27, 28}          

Example 5:How to Remove element from a set

      
          
# Remove an element from a set,Pycent.com
basic_set = {1, 2, 3, 24, 25, 26, 27, 28}
basic_set.remove(26)
print("Set after removing an element:", basic_set)
# Output:Set after removing an element: {1, 2, 3, 24, 25, 27, 28}          

Example 6:How to Check if element is in a set

      
          # Check if an element is in a set,Pycent.com
basic_set = {1, 2, 3, 24, 25, 26, 27, 28}
print("Is 3 in the set?", 3 in basic_set)
# output:Is 3 in the set? True          

Example 7:How to Perform Union of two sets

      
          # Union of two sets,Pycent.com
set1 = {1, 2, 3,9,19}
set2 = {4, 5, 6}
union_set = set1.union(set2)
print("Union of two sets:", union_set)
# Output:Union of two sets: {1, 2, 3, 19, 4, 5, 6, 9}           

Example 8:How to Perform Intersection of two sets

      
          # Intersection of two sets,Pycent.com
set1 = {1, 2, 3,9,19}
set2 = {3, 4, 5, 6}
intersection_set = set1.intersection(set2)
print("Intersection of two sets:", intersection_set)
# Output:Intersection of two sets: {3}          

Example 9:How to Perform the Difference of two sets

      
          
# Difference between two sets,Pycent.com
set1 = {1, 2, 3, 4, 9, 19}
set2 = {3, 4, 5, 6}
difference_set = set1.difference(set2)
print("Difference of two sets:", difference_set)
# Output:Difference of two sets: {1, 2, 19, 9}          

Example 10:How to clear all elements from a set

      
          # Clear all elements from a set,Pycent.com
set1 = {1, 2, 3, 4, 9, 19}
set2 = {3, 4, 5, 6}
difference_set = set1.difference(set2)
print("Difference of two sets:", difference_set)
# Clearing all elements from a set
difference_set.clear()
print("Set after clearing:", difference_set)
# Output:Difference of two sets: {1, 2, 19, 9}
Set after clearing: set()