SET DATA OPERATIONS =================== COLLECTION DATATYPES IN PYTHON ARE: 1) STRING 2) LIST 3) TUPLE 4) SETS AND FROZENSETS 5) DICTIONARY 6) BYTES AND BYTE ARRAY SET - FEATURES: =============== 1) Set is a collection datatype 2) Symbol of notation for set data is: {} All the elements in {} must be separated with comma ex: {1,3,5,7,9} 3) Set is an inbuilt datatype, because it has an inbuilt class: "set". type() 4) Set is not an ordered datatype. Ex: {1,2,3,4,5} print() ==> {1,5,2,4,3} 5) Set is not a sequential datatype. it is not suppose to use an index Note: if the index is used with set ==> Type Error 6) No slicing is possible with set data. 7) set can be homogeneous 8) set can be heterogeneous. 9) No duplication is allowed on set. (no duplication can preserve by the set) ex: {1,2,3,4,5,6,1,2,3,4,7,8,9,5,6,7} print() ==> {1,2,3,4,56,7,8,9} 10) set can nested with only tuples Note: limitation with list, set, dictionary set never be nested with list If a set with list data ==> "Type Error". # s1 = {} # empty set s1 = {1,3,5,7,9,11,13,15,17,19,21,23,25,100,150,200,250,300,400} # set definition homogeneous s2 = {True, 100, 123-234j, 'string',0.001} # Heterogeneous s3 = {1,2,3,4,5,10,20,30,40,50,1,2,3,4,5,1,3,5,7,9} # s4 = {(1,3,5,7,9),[1,2,3,4,5],'abcde'} # s4 = {'abcdef',(1,3,5,7,9),{1,4,6,7,9},{'a':100,'b':200,'c':300}} s4 = {{'a':100,'b':200,'c':300},{'d':111,'e':121,'f':133}} # s4 = {(1,2,3,4),(5,6,7,8),(9,10,11,12)} print(type(s1));print(type(s2)) print(s1);print(s2);print(s3);print(s4) # print(s1[1]) ========================================================= CREATION OF SETS: ================ 1) COMPILE TIME DEFINITION: =========================== Syntax: set-object-name = {e1,e2,e3,e4,e5} 2) RUN TIME DEFINITION: ======================= eval() we can define the set in run time which can dynamically change Syntax: eval(input("Enter a set data:")) 3) USING set() METHOD: ====================== set(): is an inbuilt method we can use to convert/change any collection data to set data Syntax: set-collection-name = set() ==> to create an empty set data set-collection-name = set(collection data) # run time definition of set s1 = eval(input("Enter a set data:")) print(type(s1)) print(s1) # using set() method s2 = set() s3 = set((1,3,5,7,9)) s4 = set([1,10,100,1000,11,111,1111]) print(type(s2));print(type(s3));print(type(s4)) print(s2);print(s3);print(s4) =================================================== Traversing of set data: ======================= Looping on set data syntax: for iteration-variable in set-data: block of operation # Traversing on sets s1 = eval(input("Enter a set data:")) sum = 0 for i in s1: # print(i,end = "\t") sum = sum + i print("The sum of set elements = ",sum) # index = 0 # while index < len(s1): # print(s1[index]) # index += 1 ================================================= Math Operations: ================ 1) Set Concatenation is not allowed in python. 2) Set Repetition is also not allowed. ======================================================= Set Comparison: =============== i) comparison with '==' and '!=' operators: =========================================== ==> these are can check lengths of the both sets ==> if both lengths are same (equal): individual element comparison should take. ii) comparison with '<', '>', '<=' and '>=': ============================================= ==> comparison should be based on individual elements of sets only. s1 = {1,2,3,4,5} s2 = {6,7,8,9,10} s3 = {1,2,3} s4 = {1,2,3,4,5,6} # print(s1 + s2) # print(s1 * 3) print(s1 == s2);print(s1 == s3);print(s1 == s4) print(s1 != s2) print(s1 > s2);print(s1 > s3);print(s1 > s4) print(s1 < s2);print(s1 < s3);print(s1 < s4) ===================================================== IS SET MUTABLE OR IMMUTABLE? ============================ Set is mutable ADDING ELEMENTS INTO SET: ======================== Note: Adding elements into set using index ==> not possible 1) add() ======= ==> an inbuilt method which we can use to add an element to set. Syntax: existed-set = set-object-name.set(element) Note: only one element can add to set # using add() s1 = {1,2,3,4,5} print("The set before change is = ",s1) print("The Address of set before change is = ",id(s1)) s1.add(10) s1.add(100) s1.add(400) print("The set after the change is = ",s1) print("The Address of set after the change is = ",id(s1)) 2) update() =========== ==> is an inbuilt method which we can use to add multiple values to the set. Syntax: set-object-name.update(collection) # using update() s1 = {1,2,3,4,5} print("The set before change is = ",s1) print("The Address of set before change is = ",id(s1)) # s1.update(10) Type Error s1.update((10,20,30)) print("The set after the change is = ",s1) print("The Address of set after the change is = ",id(s1)) Set Aliasing: ============= # set aliasing s1 = {1,3,5,7,9} s2 = s1 print(s1);print(s2) s1.add(10) print(id(s1));print(id(s2)) print(s1);print(s2) 3) copy() ========= is an inbuilt method which we can use to copy the set data from other set with different object name. Syntax: new-set-object = old-set-object.copy() ==> set data is getting cloned. # set aliasing s1 = {1,3,5,7,9} s2 = s1.copy() print(s1);print(s2) s1.add(100) print(id(s1));print(id(s2)) print(s1);print(s2) Note: === 1) if one element is used to add in set ==> Type Error here: update() ==> iterative method 2) if multiple individual elements using to add in set using update() ==> Type Error Run the python file using command prompt: ======================================== cd navigation of folder python file.py