Python – Set Exercises
Here are some exercises to practice working with Python sets. These exercises will help you understand how to perform various set operations, such as adding, removing, and modifying elements, and combining sets.
Exercise 1: Create a Set
- Create a set called
my_setwith the following elements: 1, 2, 3, 4, 5. - Print the set.
Example Solution:
my_set = {1, 2, 3, 4, 5}
print(my_set)
Exercise 2: Add Elements to a Set
- Start with the set
my_set = {10, 20, 30}. - Add the elements 40 and 50 to the set using the
add()method. - Print the updated set.
Example Solution:
my_set = {10, 20, 30}
my_set.add(40)
my_set.add(50)
print(my_set)
Exercise 3: Remove an Element from a Set
- Start with the set
my_set = {1, 2, 3, 4, 5}. - Remove the element 3 using the
remove()method. - Print the set after removing the element.
Example Solution:
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)
Exercise 4: Check if an Element Exists in a Set
- Start with the set
my_set = {1, 2, 3, 4, 5}. - Check if the number 3 exists in the set using the
inkeyword. - Print
Trueif the element exists, otherwise printFalse.
Example Solution:
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # Output: True
Exercise 5: Union of Two Sets
- Create two sets:
set1 = {1, 2, 3}andset2 = {3, 4, 5}. - Find the union of these two sets using the
union()method or the|operator. - Print the resulting set.
Example Solution:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result_set = set1.union(set2)
print(result_set) # Output: {1, 2, 3, 4, 5}
Exercise 6: Intersection of Two Sets
- Create two sets:
set1 = {1, 2, 3, 4}andset2 = {3, 4, 5, 6}. - Find the intersection of these two sets using the
intersection()method. - Print the resulting set.
Example Solution:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result_set = set1.intersection(set2)
print(result_set) # Output: {3, 4}
Exercise 7: Difference Between Two Sets
- Create two sets:
set1 = {1, 2, 3, 4, 5}andset2 = {4, 5, 6, 7}. - Find the difference between
set1andset2using thedifference()method. - Print the resulting set.
Example Solution:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7}
result_set = set1.difference(set2)
print(result_set) # Output: {1, 2, 3}
Exercise 8: Symmetric Difference Between Two Sets
- Create two sets:
set1 = {1, 2, 3, 4}andset2 = {3, 4, 5, 6}. - Find the symmetric difference between the two sets using the
symmetric_difference()method. - Print the resulting set.
Example Solution:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result_set = set1.symmetric_difference(set2)
print(result_set) # Output: {1, 2, 5, 6}
Exercise 9: Check if One Set is a Subset of Another
- Create two sets:
set1 = {1, 2, 3}andset2 = {1, 2, 3, 4, 5}. - Check if
set1is a subset ofset2using theissubset()method. - Print
Trueif it is a subset, otherwise printFalse.
Example Solution:
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2)) # Output: True
Exercise 10: Check if Two Sets are Disjoint
- Create two sets:
set1 = {1, 2, 3}andset2 = {4, 5, 6}. - Check if the two sets are disjoint (i.e., have no common elements) using the
isdisjoint()method. - Print
Trueif they are disjoint, otherwise printFalse.
Example Solution:
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2)) # Output: True
Exercise 11: Pop a Random Element from a Set
- Start with the set
my_set = {1, 2, 3, 4, 5}. - Use the
pop()method to remove and print a random element from the set. - Print the updated set after the element is removed.
Example Solution:
my_set = {1, 2, 3, 4, 5}
removed_element = my_set.pop()
print("Removed element:", removed_element)
print("Updated set:", my_set)
Exercise 12: Create a Set from a List
- Create a list with duplicate elements:
my_list = [1, 2, 2, 3, 4, 4, 5]. - Convert the list into a set to remove the duplicates.
- Print the resulting set.
Example Solution:
my_list = [1, 2, 2, 3, 4, 4, 5]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4, 5}