Python - Sets

9.
How can you check if an element is present in a set?

To check if an element is present in a set in Python, you can use the in keyword. Here's an example:

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

# Checking if an element is present in the set
element_to_check = 3

if element_to_check in my_set:
    print(f"{element_to_check} is present in the set.")
else:
    print(f"{element_to_check} is not present in the set.")
3 is present in the set.

In this example, we create a set my_set and check if element_to_check (which is 3) is present in the set using the in keyword.


10.
Discuss the use of the discard() and remove() methods in Python sets.

The discard() and remove() methods are used to remove elements from a set in Python. The key difference between them is that if the element is not present in the set, discard() does nothing, while remove() raises a KeyError.

Here's an example demonstrating their use:

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

# Using discard() to remove an element
my_set.discard(3)
print("After using discard(3):", my_set)

# Using remove() to remove an element
try:
    my_set.remove(4)
    print("After using remove(4):", my_set)
except KeyError as e:
    print(f"KeyError: {e}")
    
# Attempting to remove a non-existent element with discard()
my_set.discard(6)
print("After using discard(6):", my_set)

# Attempting to remove a non-existent element with remove() (raises KeyError)
try:
    my_set.remove(6)
    print("After using remove(6):", my_set)
except KeyError as e:
    print(f"KeyError: {e}")
After using discard(3): {1, 2, 4, 5}
After using remove(4): {1, 2, 5}
After using discard(6): {1, 2, 5}
KeyError: 6

In this example, we create a set my_set and use discard() and remove() to remove elements. Note that discard() does not raise an error when attempting to remove a non-existent element, while remove() raises a KeyError.


11.
What is the purpose of the clear() method in Python sets?

The clear() method in Python sets is used to remove all elements from the set, resulting in an empty set.

Here's an example demonstrating the use of clear():

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

# Displaying the set before using clear()
print("Before using clear():", my_set)

# Using clear() to remove all elements
my_set.clear()

# Displaying the set after using clear()
print("After using clear():", my_set)
Before using clear(): {1, 2, 3, 4, 5}
After using clear(): set()

In this example, we create a set my_set and use the clear() method to remove all elements from the set, resulting in an empty set.


12.
How do you use the copy() method to create a shallow copy of a set?

The copy() method in Python sets is used to create a shallow copy of the set. A shallow copy creates a new set, but the elements themselves are still references to the same objects as the original set.

Here's an example demonstrating the use of copy() to create a shallow copy of a set:

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

# Creating a shallow copy using copy()
shallow_copy_set = original_set.copy()

# Displaying both sets
print("Original set:", original_set)
print("Shallow copy set:", shallow_copy_set)
Original set: {1, 2, 3, 4, 5}
Shallow copy set: {1, 2, 3, 4, 5}

In this example, copy() is used to create a shallow copy of original_set, resulting in a new set shallow_copy_set. Both sets have the same elements, and modifying the elements in one set does not affect the other.