Set Operations: =============== Set is a collection of homogeneous and/or heterogeneous elements Set is unordered datatype. {} Inbuilt datatype pre-defined class set # What is the set sd1 = {1,2,3,4,5,10,20,30,40,50,11,31,21,44,55} # Homogeneous sd2 = {111,True,111-222j,'python',123.34e-7} # Heterogeneous print(type(sd1)) print(type(sd2)) print(sd1) print(sd2) ============================================== How to define the sets: ======================= 1) Compile Time Definition: =========================== 2) Run Time Definition: ====================== to define the set with dynamically change, we can go for run time definition. eval(): ====== Syntax: eval(input()) # Run time definition of set data sd = eval(input("Enter a set data:")) print(type(sd)) print(sd) 3) Using set(): =============== set() is an inbuilt method using for type conversion list/tuple/string ==> set, set() Syntax: set(collection-data) # Using set() strData = "Python" listData = [1,3,5,7,9,11,13,17,19,15,21] tupleData = ('a','u','e','o','i') sd1 = set(strData) sd2 = set(listData) sd3 = set(tupleData) print(type(sd1));print(type(sd2));print(type(sd3)) print(sd1);print(sd2);print(sd3) ================================== # WAP IN PYTHON TO DEFINE THE EMPTY SET. sd1 = {} # empty dictionary sd2 = set() # empty set print(type(sd1));print(type(sd2)) print(sd1);print(sd2) ======================================= Features of Set Data: ==================== 1) Is a pre-defined collection 2) Set with homogeneous elements/heterogeneous elements. 3) Set is Unordered data structure. 4) No duplication can preserve in sets. 5) Sets are not support indexing. 6) Mutable after the definition of the set, we can modify the set increase or decrease the set length etc. # Set Features # 1. No Duplication for set data sd = {1,3,5,7,9,1,2,3,4,5,9,7,5,3,1} print(sd) print(id(sd)) # 2. Indexing is not supported # print(sd[0]) # Type Error # Set is Mutable sd.add(97) print(sd) print(id(sd)) ============================================ Set Operations: =============== 1) Finding the length of the set: ================================= len(): ===== ==> is an inbuilt method, which we can use to find the length of the set or any collection. Syntax: len(set-data-name) # Finding the length of sets sd1 = {1,3,5,7,9} # set without duplication sd2 = {1,3,5,7,9,1,2,3,4,5,9,7,5,3,1} # set with duplication print("The Length of the set1 = ",len(sd1)) print("The Length of the set2 = ",len(sd2)) ============================================== Set Traversing: ============== Is possible with only for loop. Syntax: for loop-variable in set-data: block of operation # WAP IN PYTHON TO DEFINE THE SET AND PRINT THE ELEMENTS WHICH ARE ONLY EVEN. sd = eval(input("Enter a set data:")) for i in sd: if i % 2 == 0: print(i) =========================================== Note: ===== Sets are not supporting concatenation and repetition. sd1 = {1,2,3,4,5} sd2 = {2,4,6,8,10,12,14,16,18,20} sd3 = sd1 + sd2 print(sd3) # Type Error sd3 = sd1 * 3 print(sd3) # Type Error ============================================== Adding of Elements into set: ============================ 1) add(): ========= is an inbuilt method use to add the elements to the set at random position. Syntax: set-data-name.add(value) # Adding of Elements to set sd = set() print("The Set Data before the add operation is = ",sd) for i in range(1,100): if i % 7 == 0: sd.add(i) print("The Set Data After the add operation is = ",sd) 2) update(): ============ is an inbuilt method, which we can use to add multiple values (more than one) to the set at random positions. Syntax: set-data-name.update(any collection) # Adding of Elements to set sd = set() sd1 = set() sd2 = {1,3,5,7,9} print("The Set Data before the add operation is = ",sd) for i in range(1,100): if i % 7 == 0: sd.add(i) print("The Set Data After the add operation is = ",sd) sd1.update(sd) sd2.update((100,200,300,400)) print(sd2);print(sd1) ==================================== 3) copy(): ========== in an inbuilt method, which we can use to copy the set data to another set data. Syntax: new-set-object = old-set-object.copy() # Set Copy sd1 = {1,10,11,110,21,210} sd2 = sd1.copy() print(sd1) print(sd2) print(id(sd1)) print(id(sd2))