TUPLE DATA STRUCTURE ====================== Tuple-Features: ============ 1) It is a sequential collection datatype 2) Tuple data can define with paranthesis () Syntax: identifier = (elements/items with comma separation) 3) It is a pre-defined/an inbuilt datatype class = "tuple" 4) Tuple is an ordered datatype. 5) tuple can index supported. to access the individual elements of the tuple, we can use an "index". Syntax: tuple-data-name[index-value] Note: ==== Like strings and list: Tuple can support both positive index and negative index. positive indexing ==> forward access ==> accessing of tuple data from left to right negative indexing ==> reverse access ==> accessing of tuple data from right to left. 6) Tuple can allow to access the part of the data using slicing. Syntax: tuple-data-name[start:stop:step] 7) Tuple can define with homogeneous elements 8) Tuple can define with heterogeneous elements 9) Tuple can be nested with other collections. Note: list can also be nested with with other collections. 10) Tuple can be immutable datatype. Note: ==== to modify the tuple, we can convert that tuple into list, on this list we can define any modification. After that the list can convert into tuple again tuple ==> list ==> define change ==> tuple list() ==== using this, we can convert any collection into list data. Syntax: list(collection) tuple() ===== using this, we can convert any collection into tuple data. Syntax: tuple(collection) a = () # empty tuple b = (1,2,3,4) # tuple with integers c = (1.2,0.23,1.4,0.0097) # tuple with floats d = (True, False, True, False) # tuple with booleans e = ('a','b','c','d') # tuple with strings f = (111,12.234,12-24j,True,'False') g = [(1,11,12),{13,14,15}] # list with tuple and set h = ([1,2,3,4],{8,7,6,5}) # tuple with list and set print(type(a));print(type(b));print(type(c));print(type(d));print(type(e));print(type(f)) print(type(g));print(type(h)) print(a);print(b) # Positive indexing print(b[0]);print(b[1]);print(b[2]);print(b[3]);print() # negative indexing print(b[-1]);print(b[-2]);print(b[-3]);print(b[-4]);print() # slicing: print(b[0:4:1]);print(b[::1]);print(b[-1:-5:-1]);print(b[::-1]);print() # a[0] = 100 Type Error lt = list(b) lt[0] = 100 b = tuple(lt) print(b) ================================== TRAVERSING ON TUPLE DATA: ========================== ==> to perform the trabersing on list: two loop statements are used: 1) while loop 2) for loop 1) while loop: ========== Syntax: initialization while condition: block of statements update # Traversing on tuple using while loop td = (1,2,3,4,5) index = 0 while index < len(td): print("The Element at positive index {} and at negative index {} is = {}".format(index,index-len(td),td[index])) index += 1 2) for loop: ======== Syntax: for iteration-variable in tuple-data: block of code td = (1,3,5,7,9) index = 0 for p in td: print("The Element at positive index {} and at negative index {} is = {}".format(index,index-len(td),p)) index += 1 Traversing on Tuple in reverse: ======================== # Traversing on Tuple in reverse td = (1,2,3,4,5) i = -1 while -len(td) <= i: print("The Element at an index {} is = {}".format(i,td[i])) i -= 1 ================================ td = (1,3,5,7,9) index = -1 for i in td[::-1]: print("The Element at {} is = {}".format(index,i)) index -= 1 ============================================== Creation of the tuple: ================ 1) Compile time definition: ==================== ==> direct assignment of the tuple data Syntax: identifier = (val1,val2,val3,val4) ==> for the tuple definition, () is not mandatory. Syntax: identifier = val1,val2,val3,val4 Note: for the list definition [] is mandatory. 2) Dynamically changed tuple/Run time definition of tuple: ============================================= eval() ==== we can craete any collection data in run time Syntax: identifier = eval(input()) =================================================== 3) using tuple() ============ tuple() can use to convert any collection data into tuple data Syntax: tuple(any collection) # Rune Time definition of the tuple td = eval(input("Enter a tuple data:")) td1 = 1,2,3,4,5,6 td2 = (100,) td3 = tuple({1,100,1000,10000}) print(type(td));print(type(td1));print(type(td2));print(type(td3)) print("The Given tuple data = ",td);print(td1);print(td2);print(td3) ================================================== Math operations: ============= 1) Concatenation: ============== ==> joining of two or more tuples into one called as "tuple concatenation" ==> symbol: + Syntax: tuple1 + tuple2 + tuple3 + .... 2) Repetition: ========== ==> to make repeat the data of tuple for number of times, we can use tuple repetition. ==> Symbol: * Syntax: tuple-data * n t1 = (1,2,3,4,5);t2 = (6,7,8,9,10);t3 = (11,13,15,17,19) # Tuple Concatenation t4 = t1 + t2 + t3 print("Concatenated Tuple = ",t4) # Tuple Repetition t5 = t1 * 7 print("Repeated Tuple data = ",t5) ===================================== Tuple Comparison: =============== ==> it is possible with relational operators 1) with euality operators: =================== ==, != check: i) length of both the tuples ==================== if both tuple lengths are different: equality operators return "False" if both tuple lengths are same then: all the elements from both the tuples can be compared element by element if all the elements are same: return "True" otherwise: return "False" 2) checking with other relational operators: ================================= remaining relational operators can check and do comparison on both tuples of any length element by element # Equality check t1 = (1,2,3,4,5);t2 = (1,3,5,7,9);t3 = (1,2,3,4,5);t4 = (1,2,3,4,5,6);t5 = (1,3,5,7) print(t1 == t2);print(t1 == t3);print(t1 == t4);print(t1 == t5);print() print(t1 != t2);print(t1 != t3);print(t1 != t4);print(t1 != t5);print() # Checking with remaining relational operators print(t1 > t2);print(t1 < t2) print(t1 > t4);print(t1 < t4)