Python Sets: Excercises

Python Sets Exercises

Below are 20 exercises to help you master Python sets. Each exercise will test your understanding of set operations and methods. Make sure to try them all!

  1. Exercise 1: Create a Set
    Write a Python code to create a set containing the numbers 2 through 6 and print it.

    myset = {2, 3, 4, 5, 6}
    print(myset)
    # Output: {2, 3, 4, 5, 6}
  2. Exercise 2: Access Set Items
    Use a for loop to iterate through a set containing the elements “The Hobbit”, “1984”, and “Moby Dick” and print each item.

    books = {"The Hobbit", "1984", "Moby Dick"}
    for book in books:
        print(book)
    # Output (order may vary): 1984, Moby Dick, The Hobbit
  3. Exercise 3: Add an Item to a Set
    Create a set with the elements “dog” and “cat”. Add “hamster” to the set and print the updated set.

    animals = {"dog", "cat"}
    animals.add("hamster")
    print(animals)
    # Output: {'dog', 'hamster', 'cat'}
  4. Exercise 4: Remove an Item from a Set
    Given a set {“blue”, “yellow”, “green”}, remove “yellow” using the remove() method and print the set.

    colors = {"blue", "yellow", "green"}
    colors.remove("yellow")
    print(colors)
    # Output: {'blue', 'green'}
  5. Exercise 5: Use discard() to Remove an Item
    Create a set with elements “pen”, “pencil”, and “marker”. Use discard() to remove “pencil” and print the set.

    items = {"pen", "pencil", "marker"}
    items.discard("pencil")
    print(items)
    # Output: {'pen', 'marker'}
  6. Exercise 6: Union of Two Sets
    Create two sets, {2, 4, 6} and {6, 8, 10}. Use the union() method to combine them into a new set and print it.

    set1 = {2, 4, 6}
    set2 = {6, 8, 10}
    result = set1.union(set2)
    print(result)
    # Output: {2, 4, 6, 8, 10}
  7. Exercise 7: Intersection of Two Sets
    Given sets {5, 10, 15} and {10, 15, 20}, find the intersection and print the result.

    set1 = {5, 10, 15}
    set2 = {10, 15, 20}
    result = set1.intersection(set2)
    print(result)
    # Output: {10, 15}
  8. Exercise 8: Difference Between Two Sets
    Create sets {7, 14, 21} and {14, 21, 28}. Use the difference() method to find elements in the first set that are not in the second set.

    set1 = {7, 14, 21}
    set2 = {14, 21, 28}
    result = set1.difference(set2)
    print(result)
    # Output: {7}
  9. Exercise 9: Symmetric Difference of Two Sets
    Use sets {3, 9, 27} and {9, 27, 81}. Find the symmetric difference and print the result.

    set1 = {3, 9, 27}
    set2 = {9, 27, 81}
    result = set1.symmetric_difference(set2)
    print(result)
    # Output: {81, 3}
  10. Exercise 10: Check if Set is Subset
    Verify if set {4, 8} is a subset of {4, 8, 12} and print the result.

    set1 = {4, 8}
    set2 = {4, 8, 12}
    result = set1.issubset(set2)
    print(result)
    # Output: True
  11. Exercise 11: Check if Set is Superset
    Determine if {16, 32, 64} is a superset of {32, 64} and print the result.

    set1 = {16, 32, 64}
    set2 = {32, 64}
    result = set1.issuperset(set2)
    print(result)
    # Output: True
  12. Exercise 12: Use Update to Merge Sets
    Start with sets {11, 22} and {33, 44}. Use update() to merge the second set into the first and print the updated first set.

    set1 = {11, 22}
    set2 = {33, 44}
    set1.update(set2)
    print(set1)
    # Output: {11, 22, 33, 44}
  13. Exercise 13: Pop an Item from a Set
    Create a set {“War and Peace”, “The Catcher in the Rye”, “To Kill a Mockingbird”}. Use pop() to remove an item and print the removed item and the updated set.

    myset = {"War and Peace", "The Catcher in the Rye", "To Kill a Mockingbird"}
    removed_item = myset.pop()
    print("Removed item:", removed_item)
    print("Updated set:", myset)
    # Output: Removed item: War and Peace; Updated set: {'The Catcher in the Rye', 'To Kill a Mockingbird'}
  14. Exercise 14: Clear a Set
    Initialize a set {9, 18, 27}. Use clear() to remove all items from the set and print the empty set.

    myset = {9, 18, 27}
    myset.clear()
    print(myset)
    # Output: set()
  15. Exercise 15: Delete a Set
    Create a set {6, 12, 18}. Use the del keyword to delete the set and print it to confirm it has been deleted.

    myset = {6, 12, 18}
    del myset
    # Uncomment the following line to see the error:
    # print(myset)
    # Output: Error: name 'myset' is not defined
  16. Exercise 16: Union with a Tuple
    Combine the set {1, 4} with the tuple (7, 10) using the union() method and print the resulting set.

    myset = {1, 4}
    mytuple = (7, 10)
    result = myset.union(mytuple)
    print(result)
    # Output: {1, 4, 7, 10}
  17. Exercise 17: Set Comprehension
    Use a set comprehension to create a set of squares of numbers from 1 to 5 and print it.

    myset = {x**2 for x in range(1, 6)}
    print(myset)
    # Output: {1, 4, 9, 16, 25}
  18. Exercise 18: Copy a Set
    Create a set {“apple”, “banana”, “cherry”}. Make a copy of it using the copy() method and print both the original and the copy.

    fruits = {"apple", "banana", "cherry"}
    copied_set = fruits.copy()
    print("Original set:", fruits)
    print("Copied set:", copied_set)
    # Output: Original set: {'apple', 'banana', 'cherry'}; Copied set: {'apple', 'banana', 'cherry'}
  19. Exercise 19: Find Maximum and Minimum in a Set
    Given a set {8, 12, 5, 20}, find and print the maximum and minimum values.

    numbers = {8, 12, 5, 20}
    max_num = max(numbers)
    min_num = min(numbers)
    print("Max:", max_num)
    print("Min:", min_num)
    # Output: Max: 20; Min: 5
  20. Exercise 20: Check if Set is Empty
    Write a Python code to check if a set is empty. Initialize an empty set and print a message if it is empty.

    myset = set()
    if not myset:
        print("The set is empty")
    # Output: The set is empty

Leave a Comment

The impact of iot devices on smart home living.