SET DATA OPERATIONS =================== REMOVING OF ELEMENTS FROM SET DATA: =================================== 1) pop(): ======== it is an inbuilt method which we can use to remove/delete an item/element of any from the set. Syntax: set-data-object.pop() Note: pop() can remove the element randomly. sd1 = eval(input("Enter a set data:")) sd2 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} print("The Set data before the pop operation are = ") print(sd1) print(sd2) ===================================================== 2) remove(): =========== is an inbuilt method which we can use to remove/delete any specified element from the given set. Syntax: set-data-object.remove(element) Note: ==== if the specified element is not in the set, then: remove() can return "Key Error". sd1 = {1,11,21,31,41,51,61,71,81,91,101} print("The Set before deleting is = ",sd1) sd1.remove(21) # sd1.remove(101,51) Type Error # sd1.remove(97) print("The Set after deleting is = ",sd1) ===================================================== 3) discard() ============ is an inbuilt method which we can use to remove the specified element from the given set data. Syntax: set-data-object.discard(element) Note: ==== if the specified element is not known to the set, then: discard() not return any error. sd1 = {1,10,20,30,40,50,60,70,80,90,100} print("The Set Before the Discard operation is = ",sd1) sd1.discard(30) # sd1.discard(40,50) Type Error sd1.discard(3000) print("The Set After the Discard operation is = ",sd1) ======================================================== 4) del ======= del is a property which we can use to delete the entire set permanently. Syntax: del set-data-object Note: ==== individual element of the set cannot delete using del property. sd1 = {1,2,10,20,3,4,30,40,5,50} print("The Set Before the delete operations is = ",sd1) # del sd1[0] del sd1 # print(sd1) ==================================================== 5) clear() ========== to clear whole set by making an empty, we can use clear() Syntax: set-data-object.clear() sd1 = eval(input("Enter the set data:")) print("The Set before the clear operation is = ",sd1) sd1.clear() print("The Set after the clear operation is = ",sd1) ========================================================== Set Math Operations: ==================== 1) Union: ========= Joining of two sets into one by eliminating common elements from set2. union(): ======= Syntax: set-object1.union(set-object2) s1 = {1,2,3,4,5} s2 = {4,5,6,7,8,9,10} s3 = {6,7,8,9,10} su1 = s1.union(s2) su2 = s1.union(s3) print("The Set Unions are = ") print(su1) # {1,2,3,4,5,6,7,8,9,10} print(su2) # {1,2,3,4,5,6,7,8,9,10} =============================================== 2) Intersection: ================ can create a new set with only common elements from both the sets. intersection() ============== Syntax: set-data-object1.intersection(set-data-object2) s1 = {1,2,3,4,5} s2 = {2,4,5,6,7,8} si = s1.intersection(s2) print("Set with intersection is = ",si) ======================================================= 3) Set Difference: ================== return a set with elements from only the first set. difference(): ============= Syntax: set1.difference(set2) s1 = {1,2,3,4,5} s2 = {1,3,5,7,9,11} print("Sets are:") print(s1) print(s2) sd = s1.difference(s2) print("All the sets are:") print(s1) print(s2) print(sd) ===================================== 4) Symmetric Difference: ========================= can return a new set with elements which are in only first set and only in second set. symmetric_difference() ====================== Syntax: set1.symmetric_difference(set2) s1 = {1,2,3,4,5} s2 = {1,3,5,7,9} print(s1) print(s2) sd = s1.symmetric_difference(s2) print(s1) print(s2) print(sd) sd1.pop() sd2.pop() print("The Set data after the pop operation are = ") print(sd1) print(sd2)