Tuples are also called read-only lists and cannot be modified by their contents
1. Create a tuple
tuple1= (1,3,2,4,5) tuple2= () #空元组tuple3 = (' A ',) #元组中只有一个元素时要在元素后面加上逗号, otherwise it will be treated as a different data type tuple4= (1, ' a ', [], (3,4)) # An element in a tuple can be any data type
2. You can convert a list to a tuple
List1 = [1,2,3,4]tup = tuple (list1) print (tup) #输出 (1,2,3,4)
3. Enquiry
Tuple queries and slices are basically the same as lists
tuple1= (1,2,3,4,5) print (tuple1[1]) #输出2 # Other query slices are the same as the list, followed by the tuple name in parentheses
Number of 4.count statistics elements
tuple1= (1,3,3,4,5) print (Tuple1.count (3)) #输出为2
5.index returns the index position of the element
Tup = (' A ', ' B ', ' C ', ' d ') print (Tup.index (' C ')) # returns the index position of the element ' C ', Output 2
6.len counts the number of elements in a tuple
Tup = (' A ', ' B ', ' C ', ' d ') print (len (tup)) #计算元组长度, Output is 4
Python Basic Learning 4----tuples