# Sorting: # Arranging of the data in either ascending order or descending order # [1,7,3,97,-1,-10,99,79] # # Ascending Order ==> Low to High # Descending Order ==> High to low # sort() ld = eval(input("Enter a list:")) ld1 = ld print("The List Before the sorting:") print(ld) # sorting in ascending order ==> Forward Sorting # sort() ==> ascending order by default ld.sort() print("The List After the sorting in Ascending order = ") print(ld) # sorting in descending order ==> Reverse Sorting ld1.sort(reverse = True) print("The List after the sorting in Descending order = ") print(ld1) ============================================== Aliasing and Cloning: ==================== ld = eval(input("Enter a list:")) # dynamic list definition d1 = ld # created a new list with another name by taking existed data print(ld) print(d1) print(id(ld),id(d1)) # Aliasing: the change on one object can reflect on another object ld[4] = 'Python' print(ld) print(d1) d1[5] = 'Java' print(ld) print(d1) # Cloning: change on one object cannot be reflect on another object. # create the object from other object by doing copy. # cloning is also possible with slicing also. # cloning with copy() d2 = ld.copy() print(ld) print(d2) print(id(ld),id(d2)) ld[4] = 400 print(ld) print(d2) d2[5] = 600 print(ld) print(d2) # cloning with slicing d3 = ld[:] print(id(d3)) print(id(ld)) ld[5] = 500 print(ld) print(d3) ======================================================= String Sorting: ============== s = input("Enter a string:") print("The String before the sorting :",s) # sorting in ascending order ==> Forward Sorting s1 = sorted(s) print("The String after the sorting :",s1) # print("The String after the sorting :",str(s1)) s2 = ''.join(s1) print(s2) # sorting in descending order s3 = sorted(s,reverse = True) print(s3) s4 = ''.join(s3) print(s4) # s5 = s.sort() ========================================================== Tuple Data Structure: ===================== # Tuple And its Properties # tuple data can define with () a = () # Empty Tuple print(type(a)) b = (10,20,30,40,50) print(type(b)) # c = (111) c = (111,) print(type(c)) d = 111,222,333,444 print(type(d)) e = 121, print(type(e)) # Dynamic Tuple Definition f = eval(input("Enter a tuple:")) print(type(f)) # creating the tuple from other collections s = "Python" l = [11,22,33,44,55] t1 = tuple(s) t2 = tuple(l) print(type(t1),type(t2)) # ============================================================================= # tuple can be an ordered. print(t1) # tuple can be index based. print(t1[0],t1[1],t1[2],t1[3],t1[4],t1[5]) # forward indexing print(t1[-1],t1[-2],t1[-3],t1[-4],t1[-5],t1[-6]) # reverse indexing print(t2[::-1]) # tuple can be homogeneous # tuple can be heterogeneous t3 = (-1,0,-2,1,-3,-2) # tuple with integers t4 = ('a',101,0.0001,1-2j,True) print(type(t3),type(t4)) # Immutable Datatype # t3[0] = 'a' ================================================ Tuple Packing and Unpacking: ============================ # Tuple Packing a = 100 b = 200 c = 300 d = 400 # tp = a,b,c,d tp = (a,b,c,d) print(tp) print(type(tp)) # Tuple Unpacking p,q,r,s = tp print(p) print(q) print(r) print(s) ====================================== Tuple Sorting: ============== # Tuple Sorting td = eval(input("Enter a tuple:")) print("The Tuple before the sorting:") print(td) # td.sort() result = sorted(td) print(td) print(result) res = sorted(td,reverse = True) print(res) ==================================================== Assignment: =========== 1) WAP TO CREATE A TUPLE WITH INTEGERS AND PRINT THE ODD ELEMENTS OF THE TUPLE. 2) WAP TO CREATE A TUPLE AND PRINT ALL THE TUPLE ELEMENTS ALONG WITH BOTH POSITIVE AND NEGATIVE INDEX. HINT:(1,2,3,4,5) The Element at positive index 0 and at negative index -5 = 1 3) WAP TO CREATE THE TUPLE AND PRINT ITS LENGTH WITH AND WITHOUT BUILT-IN METHOD. 4) WAP TO CREATE THE TUPLE AND PRINT THE TUPLE WITH ITS REVERSE. 5) WAP TO CHECK WHETHER THE TUPLE CAN BE ALIASED AND CLONED.