Tuple Data Structure ==================== -> Tuple data is a collection in python which can be defined with () -> Tuple is an immutable datatype. How to define the tuple: ======================= # creation of empty tuple # there are two options: # 1. using () # 2. using tuple() t1 = () t2 = tuple() print("t1 = ",id(t1)) print(type(t1)) print(type(t2)) # t1.append(123) # t1.insert(0,123) t1 = (1,2,3,4,5) # re-definition print("t1 = ",id(t1)) print(t1) # we can define the tuple using eval() # t3 = eval(input("Enter a tuple:")) # # print(type(t3)) # print(t3) # do we able to define tuple without paranthesis? t4 = (56,) t5 = 101,102,103,104,105 print(type(t4)) print(type(t5)) # creation of tuple from other collections t6 = tuple("Python") t7 = tuple([1,11,21,31,41]) print(t6) print(t7) =================================================== Properties of Tuple: ==================== 1) Immutable data structure 2) index based 3) slicing is applicable. 4) tuple can define with homogeneous elements and also allowed with heterogeneous elements. 5) tuple can preserve the insertion order. 6) tuple can accept the duplication. t = (1,11,111,1111,11111,111111) # tuple with homogeneous elements print(t[0],t[1],t[2],t[3],t[4],t[5]) # positive indexing range print(t[-1],t[-2],t[-3],t[-4],t[-5],t[-6]) # Negative indexing print(t[::1]) print(t[::-1]) t1 = ('a',121,1.23,True,1-2j) # tuple with heterogeneous elements print(type(t1)) print(t) print(t1) t2 = (1,11,11,1,2,22,22,2) print(t2) ======================================================= How to access the tuple elements: ================================= 1) Indexing 2) Slicing 3) Looping ========== # WAP TO ACCEPT THE TUPLE AND MAKE NEW TUPLE FROM THE CREATED TUPLE WITH ONLY EVEN ELEMENTS. tupleData = eval(input("Enter a tuple:")) resultingList = list() # empty list for i in tupleData: if i % 2 == 0: resultingList.append(i) resultingTuple = tuple(resultingList) print("The Tuple after the filter operation = ",resultingTuple) ==================================================================== Note: ===== Like strings and list, the tuple can support the math operations like: Tuple concatenation ==> + t1 + t2 + t3 + ... Tuple Repetition ==> * t1 * n ======================================================= Q: Do we able to add (appending/inserting) of elements into tuple: ================================================================== Yes, we do add elements into the tuple using list. t1 = (1,10,101,1010,10101) print("The Tuple before the modification:") print(t1) # t1.append(121) l = list(t1) print(l) l.append(1212) l.insert(3,1331) print(l) t1 = tuple(l) print("The Tuple after the modification:") print(t1) ==================================================== Tuple methods: ============== 1) len() ==> to find the number of elements/length of any collection, we can use "len()". Syntax: len(tupleData) 2) count() ========== -> To count the number of occurrences of the specified element in a tuple/collection, we can use "count()". Syntax: tuple-data-name.count(element) 3) index() ========== -> to get the index (first occurrence) of the specified element from the given tuple. Syntax: tuple-data-name.index(element) 4) min() ======== -> to get the minimum of the tuple Syntax: min(tuple-data) 5) max() ======== -> to get the maximum from the tuple Syntax: max(tuple-data) 6) sorted() =========== -> to sort the tuple, we can use "sorted()" Note: ==== sort() is not possible on tuple. t = ('a','e','i','o','u','a','e','i','o','u') print("Length of the tuple = ",len(t)) cnt = t.count('a') print("The number of occurrences of the specified element = ",cnt) print("the index of 'i' = ",t.index('i')) print("Minimum = ",min(t)) print("Maximum = ",max(t)) # Natural Sorting (Forward sorting) tns = sorted(t) # sorted() can return a result in list format by default # to convert that into tuple, we can use tuple() print("The Tuple in natural order = ",tuple(tns)) # Unnatural Sorting (Reverse Sorting) trs = sorted(t,reverse = True) print("The Tuple in reverse order = ",trs) # t.sort() ========================================================= Tuple Packing and Unpacking: ============================ tuple can be created with individual elements ==> tuple packing tuple can divide into individual elements ==> tuple unpacking # tuple packing a = 1001 b = 2002 c = 3003 d = 4004 e = 5005 t = a,b,c,d,e print(t) # Tuple unpacking p,q,r,s,t = t print(p,q,r,s,t)