8.1 Implement various set operations.

Sets in Python are unordered collections of unique elements, meaning they do not allow duplicate values. They are particularly useful for operations involving membership testing, removing duplicates from sequences, and mathematical set operations like union, intersection, difference, and symmetric difference.

Key Set Operations in Python

  1. Union (|): Combines elements from two sets, removing duplicates.
  2. Intersection (&): Returns elements that are present in both sets.
  3. Difference (-): Returns elements that are in the first set but not in the second.
  4. Symmetric Difference (^): Returns elements that are in either of the sets but not in both.

Write Python program to perform following operations on set :
a) Create
b) Access
c) Update
d) Delete access set elements

# Creating Sets
set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}

print("Set A:", set_a)
print("Set B:", set_b)

# Union of Sets
union_set = set_a | set_b
print("\nUnion of A and B:", union_set)

# Intersection of Sets
intersection_set = set_a & set_b
print("Intersection of A and B:", intersection_set)

# Difference of Sets
difference_set = set_a - set_b
print("Difference of A and B (A - B):", difference_set)

# Symmetric Difference of Sets
symmetric_difference_set = set_a ^ set_b
print("Symmetric Difference of A and B:", symmetric_difference_set)

# Other set operations:
# Adding an element to a set
set_a.add(9)
print("\nSet A after adding 9:", set_a)

# Removing an element from a set (raises KeyError if element not found)
set_a.remove(3)
print("Set A after removing 3:", set_a)

# Discarding an element from a set (no error if element not found)
set_a.discard(10)  # No error even if 10 is not in the set
print("Set A after discarding 10 (no effect if not found):", set_a)

# Checking if two sets are disjoint (no common elements)
are_disjoint = set_a.isdisjoint(set_b)
print("\nAre Set A and Set B disjoint?", are_disjoint)

# Checking if one set is a subset of another
is_subset = set_a.issubset(set_b)
print("Is Set A a subset of Set B?", is_subset)

# Checking if one set is a superset of another
is_superset = set_a.issuperset(set_b)
print("Is Set A a superset of Set B?", is_superset)

OUTPUT:
Set A: {1, 2, 3, 4, 5}
Set B: {4, 5, 6, 7, 8}

Union of A and B: {1, 2, 3, 4, 5, 6, 7, 8}

Intersection of A and B: {4, 5}

Difference of A and B (A - B): {1, 2, 3}

Symmetric Difference of A and B: {1, 2, 3, 6, 7, 8}

Set A after adding 9: {1, 2, 4, 5, 9}

Set A after removing 3: {1, 2, 4, 5, 9}

Set A after discarding 10 (no effect if not found): {1, 2, 4, 5, 9}

Are Set A and Set B disjoint? False

Is Set A a subset of Set B? False

Is Set A a superset of Set B? False

Explanation:

  • Union combines all unique elements from both sets.
  • Intersection finds common elements between the two sets.
  • Difference finds elements in the first set that are not in the second.
  • Symmetric Difference finds elements that are in one set or the other, but not in both.
  • add() adds an element to the set.
  • remove() removes an element from the set (throws an error if the element is not found).
  • discard() removes an element from the set (does not throw an error if the element is not found).
  • isdisjoint() checks if two sets have no elements in common.
  • issubset() checks if one set is entirely contained within another.
  • issuperset() checks if one set contains all elements of another.
Spread the love

Leave a Comment

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

Scroll to Top