Python - Sets

5.
Discuss the difference between a set and other data types, such as lists or tuples.

Sets in Python are a unique data type with distinct characteristics that differentiate them from other data types like lists or tuples. Here's a detailed comparison with examples:

1. Uniqueness: Sets only allow unique elements. Duplicate values are automatically removed.

# Creating a set with duplicate values
my_set = {1, 2, 2, 3, 4, 4, 5}

# Displaying the set
print("Set with Duplicates:", my_set)
Set with Duplicates: {1, 2, 3, 4, 5}

2. Mutability: Sets are mutable, meaning you can add or remove elements after the set is created.


# Creating a set
my_set = {1, 2, 3}

# Adding an element
my_set.add(4)

# Removing an element
my_set.remove(2)

# Displaying the modified set
print("Modified Set:", my_set)
Modified Set: {1, 3, 4}

3. Ordering: Sets are unordered, meaning they do not maintain the order of elements.

# Creating a set
my_set = {3, 1, 4, 2}

# Displaying the set
print("Unordered Set:", my_set)
Unordered Set: {1, 2, 3, 4}

4. Indexing: Sets do not support indexing or slicing since they are unordered.

# Attempting to access an element by index (results in an error)
try:
    print(my_set[0])
except TypeError as e:
    print("TypeError:", e)
TypeError: 'set' object is not subscriptable

These characteristics make sets useful for scenarios where uniqueness and mutability are essential, and the order of elements is not significant.


6.
What is the purpose of the add() method in Python sets?

The add() method in Python sets is used to add a single element to a set. It ensures that the element is unique, and if the element is already present, the set remains unchanged. Here's an explanation with an example:

# Creating an empty set
my_set = set()

# Adding elements using the add() method
my_set.add(1)
my_set.add(2)
my_set.add(3)

# Displaying the set after addition
print("Set after Addition:", my_set)

# Adding a duplicate element
my_set.add(2)

# Displaying the set after attempting to add a duplicate
print("Set after Adding Duplicate:", my_set)
Set after Addition: {1, 2, 3}
Set after Adding Duplicate: {1, 2, 3}

In this example, the add() method is used to add elements to the set. When attempting to add the duplicate element 2, the set remains unchanged as sets do not allow duplicate elements.


7.
How do you remove an element from a set in Python?

To remove an element from a set in Python, you can use the remove() or discard() method. Both methods take an element as an argument and remove it from the set. If the element is not present, remove() will raise a KeyError, while discard() will not. Here's an example:

# Creating a set
my_set = {1, 2, 3, 4, 5}

# Using remove() to remove an element
my_set.remove(3)

# Displaying the set after removal
print("Set after removing 3:", my_set)

# Using discard() to remove an element
my_set.discard(5)

# Displaying the set after removal
print("Set after discarding 5:", my_set)

# Attempting to remove a non-existent element with remove()
try:
    my_set.remove(10)
except KeyError as e:
    print(f"KeyError: {e}")
Set after removing 3: {1, 2, 4, 5}
Set after discarding 5: {1, 2, 4}
KeyError: 10

In this example, remove() and discard() are used to remove elements from the set. The remove() method raises a KeyError when attempting to remove a non-existent element, while discard() does not raise an error.


8.
Explain the concept of set operations (union, intersection, difference).

Set operations in Python, such as union, intersection, and difference, allow you to perform mathematical operations on sets. Here's an explanation with example code:

# Creating two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}

# Union of sets (elements present in either set)
union_set = set1.union(set2)
print("Union of sets:", union_set)

# Intersection of sets (common elements in both sets)
intersection_set = set1.intersection(set2)
print("Intersection of sets:", intersection_set)

# Difference of sets (elements present in set1 but not in set2)
difference_set = set1.difference(set2)
print("Difference of sets (set1 - set2):", difference_set)

# Symmetric difference of sets (elements present in either set, but not both)
symmetric_difference_set = set1.symmetric_difference(set2)
print("Symmetric Difference of sets:", symmetric_difference_set)
Union of sets: {1, 2, 3, 4, 5, 6, 7}
Intersection of sets: {3, 4, 5}
Difference of sets (set1 - set2): {1, 2}
Symmetric Difference of sets: {1, 2, 6, 7}

In this example, union() combines elements from both sets, intersection() finds common elements, difference() finds elements in the first set but not the second, and symmetric_difference() finds elements present in either set, but not both.