Set Data structure =================== Strings ==> "" List ==> [] Tuple ==> () ==> accept an index ==> duplications -> When we want to create the data structure which not accept an index and duplications then we can use "sets". -> the set data can define with {} (curly braces) Creation of Empty set: ====================== # creation of Empty set s1 = '' s2 = str() l1 = [] l2 = list() t1 = () t2 = tuple() set1 = {} # never be the definition for empty set set2 = set() print(s1) print(s2) print(l1) print(l2) print(t1) print(t2) print(type(set1)) print(set2) ===================================================== Properties of Sets: =================== 1) set is a collection can define with {} and elements of set in {} must be separated with comma. Syntax: {e1,e2,e3,...} 2) No duplication is allowed for sets. 3) Set is not allowed an index to access elements. Because the set elements can store in random locations of heap memory. 4) Not preserve an insertion order. 5) Set is mutable. 6) Set data can define with homogenous elements and also with heterogeneous elements. set1 = {1,11,111,1111,1111,111,11,1} set2 = {1,11,'ab',101,'pq',1021,'lm',1010,'xy'} print(set1) # print(set1[0]) print(set2) print("The Set abefore the modification = ") print(set1) print(id(set1)) # add() set1.add('python') set1.add("Programming ") set1.add("Language") print("The Set after the modificsation = ") print(set1) print(id(set1)) ========================================================== Set Methods: ============ 1) len() ======== -> to find the length of the given collection (set also), we can use "len()". Syntax: len(set-data-name) 2) add(): ======== -> To add any element (only one at a time) to the set at random locations we can use "add()" Syntax: set-data-name.add(element) 3) update(): ============ -> when we want to add more than one element to the set, we can use "update()". Syntax: set-data-name.update([e1, e2, e3,...]) # dynamic definition for set s1 = eval(input("Enter a set:")) print("The Set before to change = ") print(s1) print(id(s1)) s1.update([101,100,102,99]) print("The Set after the change = ") print(s1) print(id(s1)) s1.update(range(1,10,3)) print(s1) ==================================================== 4) for deletion: ================ pop() ===== -> can use to delete the element of the set randomly. Syntax: set-data-name.pop() remove(): ========= -> when we want to delete the specified element from the set, we can use "remove()". Syntax: set-data-name.remove(element) Note: ===== if the specified element is not in the given set, remove() can return "key-error". discard(): ========= Syntax: set-data-name.discad(specified element) -> based on the specified element, the discard() method can remove the element from the set. -> if the element is not present, discard() not return any error. clear() ======= -> to remove all elements of the set, clear() can be used. Syntax: set-data-name.clear() del === Syntax: del set-data-name s = {"Python","is ","one of the ","Easy ","Language"} print("The Given set before the pop operation is = ") print(s) # s.pop() # print("The Set after the pop operation is = ") # print(s) # s.pop("Easy") # print(s) # s.remove("Easy ") # # print(s) s.discard("Easy ") print(s) # s.discard(102) # s.remove(102) # del s s.clear() print(s) ================================================ Set math operations: ==================== Note: ===== No concatenation and repetition on set is possible. -> sets allowed the math operations like: 1) union ======== union() ======= Syntax: new-object = s1.union(s2) -> union() can create new set object by joining two set elements by ignoring duplications. 2) intersection ================ intersection() =============== Syntax: new-object = s1.intersection(s2) -> intersection() can create new set object with common elements from both the sets. 3) difference ============= difference() ============ Syntax: s1.difference(s2) -> this can create new set object with the elements which are only present in first set. 4) symmetric difference ======================== symmetric_difference() ====================== Syntax: s1.symmetric_difference(s2) -> this can create the set object with the elements which are present only in first and also in second set. s1 = {1,2,3,4,5} s2 = {55,44,33,22,11,1,2,3} # print(s1+s2) # print(s1 * 2) # set union sunion = s1.union(s2) print(s1) print(s2) print(sunion) # set Inetersection sinter = s1.intersection(s2) print(sinter) # set difference sdiffer = s1.difference(s2) print(sdiffer) # symmetric difference sdif = s1.symmetric_difference(s2) print(sdif) Assignment: =========== Is aliasing and cloning of set is possible?