Python sets are powerful data structures used to store unique, unordered items. These exercises cover basic to advanced set operations, with interview-focused questions to strengthen your understanding.

Basic Python Set Exercises

  1. Create a Set
    Write a Python program to create a set and print its elements.
    Example:
my_set = {1, 2, 3, 4, 5} 
print(my_set)
  1. Add and Remove Items
    Add an item to a set and remove it.
    Example:
my_set = {"apple", "banana", "cherry"} 
my_set.add("orange") 
my_set.remove("banana") 
print(my_set)
  1. Loop Through a Set
    Iterate through a set to display its elements.
    Example:
fruits = {"apple", "banana", "cherry"} 
for fruit in fruits: 
  print(fruit)

Intermediate Python Set Exercises

  1. Set Union and Intersection
    Write a program to demonstrate the union and intersection of sets.
    Example:
set1 = {1, 2, 3} 
set2 = {3, 4, 5} 
print("Union:", set1.union(set2)) 
print("Intersection:", set1.intersection(set2))
  1. Difference and Symmetric Difference
    Write a program to find the difference and symmetric difference between two sets.
    Example:
set1 = {1, 2, 3} 
set2 = {3, 4, 5} 
print("Difference:", set1.difference(set2)) 
print("Symmetric Difference:", set1.symmetric_difference(set2))
  1. Subset and Superset Check
    Verify if a set is a subset or superset of another set.
    Example:
set1 = {1, 2} 
set2 = {1, 2, 3, 4} 
print("Is Subset:", set1.issubset(set2)) 
print("Is Superset:", set2.issuperset(set1))

Advanced Python Set Exercises

  1. Set Operations on Strings
    Perform operations on sets created from strings.
    Example:
set1 = set("hello") 
set2 = set("world") 
print("Union:", set1.union(set2)) 
print("Intersection:", set1.intersection(set2))
  1. Find Unique Words from Text
    Extract unique words from a given text using sets.
    Example:
text = "Python is fun and Python is powerful" 
unique_words = set(text.split()) 
print("Unique Words:", unique_words)

Common Interview Questions (CIQ)

  1. How do you remove duplicates from a list using sets?
    Answer: Use the set() function to remove duplicates.
    Example:
my_list = [1, 2, 2, 3, 4, 4] 
unique_list = list(set(my_list)) 
print(unique_list)

  1. What is the difference between discard() and remove()?
    Answer:
    • remove() raises an error if the item doesn’t exist.discard() does nothing if the item doesn’t exist.
      Example:
my_set = {1, 2, 3} 
my_set.discard(4) # No error 
my_set.remove(4) # Raises KeyError

3.Can a set contain mutable data types like lists?
Answer: No, a set can only contain immutable elements (e.g., numbers, strings, tuples). Mutable types like lists cannot be added to sets.


4.How do you find common elements in two lists using sets?
Answer: Convert the lists to sets and use the intersection() method.
Example:

list1 = [1, 2, 3] 
list2 = [2, 3, 4] 
common = set(list1).intersection(set(list2)) 
print(common)

5.How do you check if two sets have no common elements?
Answer: Use the isdisjoint() method.
Example:

set1 = {1, 2, 3} 
set2 = {4, 5, 6} 
print("No Common Elements:", set1.isdisjoint(set2))

Additional Resources

Leave a Reply

Your email address will not be published. Required fields are marked *