1, what is the tuple
Python will not be able to modify values that are immutable, and immutable lists become tuples, using () to indicate that tuples also use indexes to access their elements. It is important to note that the elements of the tuple cannot be changed , but the elements of the element can be changed .
1.1. Define Tuples
1Dimensions = (200,50)2 3 Print(Dimensions[0])4 Print(dimensions[1])5 6Dimensions = Tuple ((200,50)) 7 8 Print(Dimensions[0])9 Print(dimensions[1])Ten One ATuple1 = (1,)#if the tuple has only one element, you must add a comma at the end of the element. Otherwise python will handle the parentheses as a mathematical operator. - Print(Tuple1)
1.2. Traverse all elements in a tuple
1 dimensions = (200,50)2 for in Dimensions:3 print(dimension)
1.3, "modify" the elements in the tuple
1Tuple1 = (200,[100,300])2 3Tuple1[0] = 300#The operation failed. 4Tuple1[1][0] = 505TUPLE1[1][1] = 1006 7 Print(Tuple1)#The result is (200,[50,100]). 8 9Tuple2 = (200,{'K1':'v1'})Ten Onetuple2[1]['K1'] ='v2' A - Print(Tuple2)#The result is (200,{' K1 ': ' v2 '})
04-python-Meta-Group