Python Basic Notes Series Five: Tuples

  • tuple

1) The structure, access, usage and list of tuples are basically the same. There are two main differences: 1. Use parentheses to enclose each data item; 2. The values ​​in the tuple cannot be modified.
example:

1  #A tuple is equivalent to a read-only list, its elements cannot be modified 
2 demotuple = (1,3,5,6,8,9) #Define a tuple, use parentheses 3 print demotuple #Only read    but not write 4 print demotuple[2:5] #Output tuples with subscripts 2 to 5
 
 

output:

1 (1, 3, 5, 6, 8, 9)
2 (5, 6, 8)

2) Multidimensional tuples: similar to multidimensional lists, but also have the relevant characteristics of tuples

example:

1  #It can also be similar to a two-dimensional array 
2 tuple1 = ((1,2),(3,4),(5,6)) #Define a multi-dimensional tuple 3 print tuple1 #Print the entire two-dimensional tuple 4 print tuple1 [0] #Print the first line 5 print tuple1[0][1] #Print the second element of the first line
 
 
 

output:

1 ((1, 2), (3, 4), (5, 6))
2 (1, 2)
3 2

 

 


   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324751279&siteId=291194637