Ad Code

Responsive Advertisement

Basic Python Program - 33

Question 33: Write a Python program to perform following operations on set: intersection of sets, union of sets, set difference, symmetric difference, clear a set.
Download hole Program / Project code, by clicking following link:
👉 Click Here: How to create, read, update and remove set values in Python
Describe the various methods of set ?
Sets in Python are unordered collections of unique elements, and they come with a variety of built-in methods for performing various operations. Here's a description of some of the most commonly used methods associated with sets:
  1. add(item): Adds an element to the set. If the element is already present in set, it doesn't add a duplicate.
    my_set = {1, 2, 3}
    my_set.add(4) # Adds 4 to the set
  2. update(iterable) : Adds multiple elements from an iterable (e.g., another set, list, or tuple) to the set.
    my_set = {1, 2, 3}
    my_set.update([3, 4, 5]) # Adds 4 and 5 to the set
  3. remove(item): Removes a specified element from the set. If the element is not found, it raises a KeyError.
    my_set = {1, 2, 3}
    my_set.remove(2) # Removes 2 from the set
  4. discard(item): Removes a specified element from the set if it exists, but does nothing if the element is not found (no error is raised).
    my_set = {1, 2, 3}
    my_set.discard(4) # No error; set remains unchanged
  5. pop(): Removes and returns an arbitrary element from the set. Since sets are unordered, the choice of which element is arbitrary.
    my_set = {1, 2, 3}
    popped_item = my_set.pop() # Removes and returns an element
  6. clear(): Removes all elements from the set, making it an empty set.
    my_set = {1, 2, 3}
    my_set.clear() # Empties the set; my_set is now set()
  7. copy(): Creates a shallow copy of the set.
    my_set = {1, 2, 3}
    copy_set = my_set.copy() # Creates a copy of my_set
  8. union(iterable): Returns a new set containing all unique elements from the set and an iterable.
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    union_set = set1.union(set2) # Creates a set with elements {1, 2, 3, 4, 5}
  9. intersection(iterable): Returns a new set containing elements that are common to both the set and an iterable.
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    intersection_set = set1.intersection(set2) # Creates a set with the element {3}
  10. difference(iterable): Returns a new set containing elements that are in the set but not in the iterable.
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    difference_set = set1.difference(set2) # Creates a set with elements {1, 2}
  11. symmetric_difference(iterable): Returns a new set containing elements that are in either the set or the iterable, but not in both.
    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    symmetric_difference_set = set1.symmetric_difference(set2) # Creates a set with elements {1, 2, 4, 5}
These methods allow you to manipulate sets efficiently, perform set operations, and manage their content. Sets are useful when you need to work with collections of unique items or perform operations like unions, intersections, and differences.
Programming Code:
Following code write in: BP_P33.py
# Operations on Set

# create set
a = set([1, 2, 3, 4, 5])
b = set([2, 4, 6, 8, 10])

print("set a is: ", a)
print("set b is: ", b)

# Intersection of set

result1 = a.intersection(b)
print("Intersection of set a & b is: ", result1)

# Union of set

result2 = a.union(b)

print("Union of set a & b is: ", result2)

# set difference

result3 = a.difference(b)

print("set difference is: ", result3)

result3 = b.difference(a)

print("set difference is: ", result3)

# symmetric difference

result4 = a.symmetric_difference(b)

print("symmetric difference is: ", result4)

# clear set

result5 = a.clear

print("After clear set a: ", result5)

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement