TUPLE OPERATIONS ================== 1) count() ======== ==> an inbuilt method, which we can use to count the number of occurrences of the givent of element/item in a tuple. Syntax: tuple-data-name.count(tuple item) td = (1,2,3,1,2,3,4,5,6,1,2,3,4,5,6) print(td.count(1)) print(td.count(2)) print(td.count(4)) print(td.count(100)) Note: ==== count() can return '0' if the specified element is unknown to the tuple data. =========================================== index() ====== ==> an inbuilt method which we can use to find the first occurrence of the specified element in a tuple. Syntax: tuple-data-name.index(tuple-element/tuple-item) td = (1,2,3,1,2,3,4,5,6,1,2,3,4,5,6) print("The First Occurrence of 3 in the given tuple {} is {}".format(td,td.index(3))) # print(td.index(100)) Value error Note: ===== if the specified element is unknown to the tuple, index() can return: "Value Error" ================================= sorted() ====== Note: ==== sort() ==> is not applicable to tuple sorted() is an inbuilt method which we can use to arrange the elements in ascending order or descending order. sorted() can arrange the tuple elements in ascending order as by default. Syntax: sorted(tuple-data) Note: ==== sorted() can return a list as an output. td = eval(input("Enter the tuple data:")) print("The Given tuple is:") print(td) tds = sorted(td) print("After the sorting, the tuple is:") print(tuple(tds)) sorting in descending order: ===================== Syntax: identifier = sorted(tuple-data,reverse = True) td = eval(input("Enter the tuple data:")) print("The Given tuple is:") print(td) tds = sorted(td) print("After the sorting, the tuple is:") print(tuple(tds)) # ascending order # sorting in descending order tds1 = sorted(td,reverse=True) print("The Tuple After the sorting is = ") print(tuple(tds1)) ================================ Finding of minimum and maximum of tuple data: ====================================== min() ===== an inbuilt method, which we can use to find the minimum of the given collection data. Syntax: min(collection) max() ==== an inbuilt method, which we can use to find the maximum of the given collection. Syntax: max(collection) d1 = eval(input("Enter a string:")) d2 = eval(input("Enter a list:")) d3 = eval(input("Enter a tuple:")) d4 = eval(input("Enter a set:")) print("The Minimum value of the given collections are:") print(min(d1));print(min(d2));print(min(d3));print(min(d4)) print("The Maximum value of the given collections are:") print(max(d1));print(max(d2));print(max(d3));print(max(d4)) ================================ Note: ==== Python3: Tuple comparison with relational operators only possible. Python2: cmp() can also use to do the comparison on tuples. But, python3 cannot support cmp() for the tuple comparison. cmp(): ==== Syntax: cmp(t1,t2) return: 0 ==> if both tuples are equal return: 1 ==> if t1 > t2 return: -1 ==> if t1 < t2 ======================== Tuple Packing and Unpacking: ======================= Packing: ====== making a tuple from individual data Unpacking: ======== from tuple data we can prepare individual data # tuple packing a = 10;b = 20;c = 30 d = 1.2;e = 1.12;f = 1.112 g = True;h = False tp = a,b,c,d,e,f,g,h tp1 = (a,b,c,d,e,f) print("The Tuples are:") print(tp);print(tp1) # Tuple Unpacking p,q,r,s,t,u = tp1 print("The Individual Data from tuple is = ") print(p);print(q);print(r);print(s);print(t);print(u)