TUPLE OPERATIONS ================ WHAT IS THE TUPLE? =================== Tuple is a collection datatype. collection of homogeneous and heterogeneous elements can represent with () the elements within () must be separated with comma (,) Syntax: identifier = (ele1,ele2,ele3,ele3,..) Tuple is also an inbuilt datatype pre-defined class ==> "tuple" # Definition of the tuple tupleData = () # Empty Tuple tupleData1 = (1,3,5,7,9) # Homogeneous Tuple tupleData2 = (1-2j,2+3j,3-4j,4+5j,6-7j,7+9j) # Homogeneous Tuple tupleData3 = (121,12.345,123-234j,True,'abcdef') # Heterogeneous Tuple print(type(tupleData)) print(type(tupleData1)) print(type(tupleData2)) print(type(tupleData3)) ============================================== How to define the tuple? ======================== The ways to define the tuple is: 1) Compile time Definition 2) Run time Definition 3) Using tuple() 4) using split() 1) Compile time Definition =========================== two ways: with parenthesis without parenthesis Syntax: 1) tuple-object-name = (ele1,ele2,ele3,ele4,...) ==> with parenthesis 2) tuple-object-name = ele1,ele2,ele3,ele4,... ==> without parenthesis # Compile time definition of the tuple # Syntax-1: with parenthesis a = (1,10,100,1000,10000,11,111,1111) print(type(a)) # Syntax-2: without parenthesis b = 111,121,131,141,151,97,79,9,7 print(type(b)) c = (100) d = (200,) e = 300, print(type(c));print(type(d));print(type(e)) ============================================ 2) Run time definition of the tuple: ==================================== program: 1) Compilation stage: syntax 2) Execution stage eval(): Syntax: eval(input("Enter some data:")) # Run time definition a = eval(input("Enter a tuple data:")) print(type(a)) ================================================ 3) Using tuple(): ================= tuple(): ======== ==> is an inbuilt method which can use to for the type conversion. string/list/set/dictionary ===> tuple Syntax: tuple(string/list/set/dictionary) # Creating the tuple with tuple() a = [1,3,5,7,9] # list definition b = 'Python' # string definition tl = tuple(a) ts = tuple(b) print(type(tl)) print(type(ts)) ======================================= 4) Using split(): ================= to generate/define the tuple from the split, we need tuple() method string ====> list using split() ===> tuple with tuple() # Creation of the tuple using split() strData = "Python is High Level and Object Oriented Programming Language" data = strData.split() tupleData = tuple(data) print(type(data)) print(type(tupleData)) ======================================================= Features of the Tuple data: =========================== 1) Tuple is an inbuilt datatype. 2) Tuple can define with () and without (). 3) Tuple can be the collection of homogeneous and heterogeneous elements. 4) Tuple can be Ordered. 5) Tuple can be generated using range of values or sequence of values. 6) Tuple can be indexed. using positive and negative index values, we can access the tuple data from left to right or right to left with individual elements. Syntax: tuple-data-name[index] 7) Tuple can be duplicated. 8) Tuple can be nested with any collection. 9) Immutable datatype. # Features of tuple data # Ordered. td = (1,2,3,4,5,6,7,8,9,10) # tuple from range() tr = tuple(range(1,15,3)) # Tuple with duplicated elements tde = 1,3,5,7,9,9,7,5,3,1,1,1,3,3,5,5,7,7,9,9 # Tuple with nesting tnt = ((1,2,3),(4,5,6),(7,8,9)) tnl = ([10,20,30],[40,50,60],[70,80,90]) tns = ('abcdef',"ghijkl",'mnopqrst') print(td) print(tr) # Indexing # Positive Index print(td[0]);print(td[1]);print(td[2]);print(td[3]);print(td[4]) # Negative Index print(td[-1]);print(td[-2]);print(td[-3]);print(td[-4]);print(td[-5]) # slicing print(tr[::1]) # with forward slicing print(tr[::-1]) # with reverse slicing print(type(tde)) print(tde) print(tnt) print(tnl) print(tns) print(tnt[0]) print(tnt[0][0]);print(tnt[0][1]);print(tnt[0][2]) # tnt[0] = [1,3,5,7,9] =========================================== Math Operations: ================ 1) Tuple Concatenation ==> + 2) Tuple Repetition ==> * # Math Operations t1 = (1,3,5,7,9) t2 = (2,4,6,8,10) print(id(t1));print(id(t2)) print(t1);print(t2) t1 = t1 + t2 print(t1);print(t2) print(id(t1));print(id(t2)) t3 = t2 * 5 print(t3)