TUPLE OPERATIONS ================ Traversing on The Tuple Data: ============================= Using while loop: ================= Syntax: initialization while condition with loops: block of operation Update statement # Traversing on Tuple using while loop in forward direction td = eval(input("Enter a tuple data:")) length = len(td) index = 0 while index < length: # print("The Element at positive index %s is = %s"%(index,td[index])) # print("The Element at negative index %s is = %s"%(index-length,td[index])) print("The Element at %s and at %s is = %s"%(index,index-length,td[index])) index += 1 ===================================== # Traversing on Tuple using while loop in reverse direction td = eval(input("Enter a tuple data:")) length = len(td) index = -1 while index >= -length: print("The Element at {} and at {} is = {}".format(index + length,index,td[index])) index -= 1 ======================================== Using for loop: =============== Syntax: for loop-var in tuple-name: block of operation # Traversing on tuple data using for loop in forward direction td = eval(input("Enter a tuple data:")) index = 0 for i in td: print("The Element at %s and at %s is = %s"%(index,index-len(td),i)) index = index + 1 ===================================================== Counting the Occurrences of the element in a tuple: =================================================== count(): ======== Syntax: tuple-data-name.count(Tuple-element) If the specified element is in the given tuple, count() can return: "Number of Occurrences". If the specified element is not in the given tuple, count() can return: '0' # Counting the number of Occurrences of element in a tuple td = (1,2,3,4,5,1,3,5,7,9) print(td.count(0)) print(td.count(1)) print(td.count(2)) ==================================================== Finding the Element in a tuple: =============================== index(): ======== is an inbuilt method, which we can use to find the first occurrence of the specified element in the given tuple. Syntax: tuple-data-name.index(tuple-element) # Finding the element in a tuple td = (1,3,5,7,9,1,2,3,4,5,1,2,3,4,5) print(td.index(5)) # 2 # print(td.index(100)) ============================================= Sorting of Tuple data: ====================== sorted(): ========= Forward Sorting: Arranging from low to high ==> Ascending Order Arrangement Syntax: sorted(tuple-data-name) Reverse Sorting: Arranging from high to low ==> Descending Order Arrangement Syntax: sorted(tuple-data-name, reverse = True) # Sorting Operation td = eval(input("Enter a tuple data:")) # Forward Sorting print("The Tuple Before the sorting operation is = ") print(td) ts = sorted(td) print("The Tuple After the sorting is = ") print(tuple(ts)) # Reverse Sorting tr = sorted(td,reverse = True) print("The Tuple After the sorting is = ") print(tuple(tr)) ============================================= Finding of Minimum and Maximum from the collections: ==================================================== min() ===== is an inbuilt method which we can use to find the minimum from the given collection. Syntax: min(collection-name) max() ===== is an inbuilt method which we can use to find the maximum from the given collection. Syntax: max(collection-name) # Finding minimum and maximum of collection: sd = input("Enter a string data:") ld = eval(input("Enter a list data:")) td = eval(input("Enter a tuple data:")) print("The Minimum values are:") print(min(sd));print(min(ld));print(min(td)) print("The Maximum Values are:") print(max(sd));print(max(ld));print(max(td)) ==================================================== Tuple Packing and Unpacking: ============================ Creating of a tuple from individual data elements is called as "Tuple Packing". Syntax: tuple-data-name = name1,name2,name3,name4,... The tuple data can divide into individual elements is called as "Tuple Unpacking". Syntax: name1,name2,name3,... = tuple-data-name # Packing and Unpacking # Tuple Packing: a = 100 b = 121.234 c = 12-23j d = True e = "Python" td = a,c,d,b,e print(type(td)) print(td) # Tuple Unpacking: p,q,r,s,t = td print(p);print(q);print(r);print(s);print(t) ================================================== Assignment: =========== 1) WAP TO TAKE THE TUPLE AS AN INPUT AND TRAVERSE ON THE GIVEN TUPLE IN REVERSE DIRECTION AND PRINT THE ELEMENTS OF TUPLE FROM RIGHT TO LEFT ALONG WITH BOTH +EVE AND -EVE INDEXES. 2) WAP TO TAKE A TUPLE DATA AS AN INPUT AND FIND THE LENGTH OF THE TUPLE WITHOUT len(). 3) WAP TO TAKE A TUPLE AND PRINT THE TUPLE IN REVERSE.