SET OPERATIONS: =============== # set is a collection of unique elements and defined with {} # s1 = {} s1 = set() # constructor ==> a special method print(type(s1)) print(s1) s2 = {1,10,100,1000,10001,999,777,-1} print(type(s2)) print(s2) # set data is not ordered. s3 = {1,11,111,1111,11111,1111,111,11,1} print(type(s3)) print(s3) # set is not index based. # print(s2[0]) # set can be homogeneous and/or heterogeneous. s4 = {1,2,3,4,5,6,7,8,9,10} # set with integers s5 = {101,1.101, 1-23j,True} print(s4) print(s5) print(id(s4)) # sets are mutable s4.add(100) s4.add(-11) print(s4) print(id(s4)) ================================================= Creating the sets from other collections: ========================================= s = "Python is Easy to learn" l = [1,11,111,1111,11111,11111,1111,111,11,1] t = (2,20,200,2000,2000,200,20,2) print(type(s),type(l),type(t)) # string into set s1 = set(s) print(type(s1)) print(s1) # list to set s2 = set(l) print(type(s2)) print(s2) # Tuple to set s3 = set(t) print(type(s3)) print(s3) ========================================== Traversing on set: ================== # set dynamic definition s = eval(input("Enter a set:")) result = set() # print(type(s)) # print(s) # I want create a new set with only even elements of given set. for i in s: if i % 2 == 0: result.add(i) print(result) ========================================== Set Manipulations: ================== add(): ===== ==> can used to add element/value to the set at random position. Syntax: set-data-name.add(element) update(): ======== ==> used to add more than one element to the set. Syntax: set-data-name.update(val1, val2, val3,..) # Adding data into set s = {1,101,11,99,21,121,97,63,79} print("The Set before the adding element = ") print(s) s.add(77) s.add(1001) print("The Set after the adding element = ") print(s) s.update([100,200,300,400]) print(s) s.update(range(20,30)) print(s) ===================================== a = {1, 11, 101, 1001, 1010, 1001} b = a.copy() print(a) print(b) print(id(a)) print(id(b)) ================================================== Delete Operations: ================== # a = {1, 11, 101, 1001, 1010, 1001} # # b = a.copy() # # print(a) # print(b) # # print(id(a)) # print(id(b)) a = {1, 11, 101, 1001, 1010, 1001} print("The Set before the delete operation = ") print(a) a.remove(1001) # to remove the specified element print("The Set after the delete operation = ") print(a) # a.remove(1001) key error a.discard(101) # can also remove the specified element print(a) a.discard(1000) # never return an error when we can apply for unknown elements. a.clear() # can clear the whole set print(a) del a print(a) ===================================================== Math Operations: ================ # set Union ============ # set Union s1 = {1,2,3,4,5,6,7} s2 = {1,3,5,7,9,11,13,15} print("s1 = ",s1) print("s2 = ",s2) s3 = s1.union(s2) print("s1 = ",s1) print("s2 = ",s2) print("s3 = ",s3) s4 = s1.intersection(s2) print("s4 = ",s4) s5 = s1.difference(s2) print("s5 = ",s5) s6 = s1.symmetric_difference(s2) print("s6 = ",s6) Assignment: ============ 1) WAP TO CHECK ALIASING AND CLONING ON SETS. 2) WAP TO DEFINE THE TUPLE AND MAKE UPDATE THE TUPLE WITH SOME ELEMENTS AND PRINT THAT TUPLE AFTER THE UPDATE.