SET OPERATIONS: =============== Remove of Elements from set: ============================ 1) pop(): ========= an inbuilt method use to remove the element randomly from the set. Syntax: set-data-name.pop() # pop() operation on sets sd = eval(input("Enter any set:")) print("The Set before the pop operation is = ",sd) sd.pop() print("The Set after the pop operation is = ",sd) # sd.pop(19) # # print("The Set after the pop operation is = ",sd) ================================================= remove(): ========= is an inbuilt method, which we can use to remove the specific element. Syntax: set-data-name.remove(set-element) ==> if the specified element is not in the given set: remove() can return : "key error". # remove() operation sd = {1,3,5,11,15,7,9,13,17,25,21,19} print("The Set before the remove operation is = ",sd) sd.remove(13) print("The Set after the remove operation is = ",sd) # sd.remove(122) # print("The Set is = ",sd) ================================================ discard(): ========== is an inbuilt method, which we can use to remove the specified element. Syntax: set-data-name.discard(set-element) ==> if the specified element is not in the given set: discard() ==> return : None # discard operation sd = {1,111,11,91,21,81,31,71,41,61,51,101} print("The Set before the discard operation is = ",sd) print(sd.discard(81)) print("The Set after the discard operation is = ",sd) print(sd.discard(1221)) print("The Set is = ",sd) ====================================================== 4) clear() ========== ==> is an inbuilt method which we can use to clear all the elements from the set. Syntax: set-data-name.clear() # Clear Operation sd = {1,3,5,7,9,2,4,6,8,10} print("The Set before the clear operation is = ",sd) sd.clear() print("The set after the clear operation is = ",sd) ============================================= 5) del: ======== is a property which we can use to delete the specific element or the total collection also. Syntax: 1) to delete the specific element: is not possible usually, need an index but, the set cannot support the index to access. 2) del property can delete the entire set Syntax: del set-name sd = {1,3,5,7,9,2,4,6,8,10} print("The Set before the delete is = ",sd) # del sd[0] del sd print(sd) ============================================ Math Operations: ================ Set Union ========== ==> Joining of two sets by avoiding the duplications is called as "set Union" ex: a = {1,2,3,4,5} b = {1,3,5,7,9} a union b ==> {1,2,3,4,5,7,9} union(): ======== Syntax: set1.union(set2) # Set Union a = eval(input("Enter the first set:")) b = eval(input("Enter the second set:")) c = set() print("The Sets before the union operations are = ") print(a);print(b);print(c) c = a.union(b) print("The Sets after the Union Operation are = ") print(a);print(b);print(c) ===================================================== Set Intersection ================ is an operation which can generate a new set with common elements of both the sets only. ex: a = {1,2,3,4,5} b = {1,3,5,7,9} a intersection b ==> {1,3,5} intersection(): =============== Syntax: set1.intersection(set2) a = eval(input("Enter the first set:")) b = eval(input("Enter the second set:")) c = set() print("The Sets before the Intersection operations are = ") print(a);print(b);print(c) c = a.intersection(b) print("The Sets after the intersection operation are = ") print(a);print(b);print(c) ======================================= Set Difference =============== is an operation can return a new set with the elements which are present in only the first set. ex: a = {1,2,3,4,5} b = {1,3,5,7,9} a difference b ==> {2,4} b difference a ==> {7,9} difference(): ============ Syntax: set1.difference(set2) a = eval(input("Enter the first set:")) b = eval(input("Enter the second set:")) c = set() d = set() print("The Sets before the Set difference operation are = ") print(a);print(b);print(c);print(d) c = a.difference(b) d = b.difference(a) print("The Sets after the Set difference Operation are = ") print(a);print(b);print(c);print(d) =========================================== Set Symmetric Difference: ========================= is an operation which can return a new set with the elements which are present in only set1 and in only set2. ex: a = {1,2,3,4,5} b = {1,3,5,7,9} a symmetric-difference b ==> {2,4,7,9} symmetric_difference(): ======================= Syntax: set1.symmetric_difference(set2) a = eval(input("Enter the first set:")) b = eval(input("Enter the second set:")) c = set() print("The Sets before the Set Symmetric difference operation are = ") print(a);print(b);print(c) c = a.symmetric_difference(b) print("The Sets after the Set Symmetric difference operation are = ") print(a);print(b);print(c) ============================================ # WAP IN PYTHON TO ELIMINATE DUPLICATE ELEMENTS PRESENT IN THE GIVEN LIST. ld = eval(input("Enter a list with duplicates:")) print("The Original List is = ",ld) # list to set, to avoid/remove the duplicates sd = set(ld) print("The Set from the list = ",sd) # set ==>list ld = list(sd) print("The list without duplicates is = ",ld) ======================================= # WAP IN PYTHON TO PRINT DIFFERENT VOWELS PRESENT IN THE GIVEN WORD. """ str = "Object Oriented Programming" O, e, i, a """ word = input("Enter a string data:") sd = set(word) vowels = {'a','A','e','E','i','I','o','O','u','U'} result = sd.intersection(vowels) print("The set with different vowels are = ",result)