A tuple is another data type, similar to a list.
The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list. Cannot be modified once initialized
Role:
1, for some data we do not want to be modified, you can use the tuple
2. Tuples can be used as keys in mappings (and members of collections)-but not in lists, tuples as return values for many built-in functions and methods
tuple = ('runoob'John', 70.2) # format: tuple name = ( Element 1, Element 2, Element 3)# tuple = ( a) # an element that needs to add a comma after the element, representing the element, or a variable 'John ')
1. Check
Print(tuple)#Output Full tuplePrint(Tuple[0])#the first element of an output tuplePrint(Tuple[1:3])#outputs the second to third elementPrint(tuple[2:])#outputs all elements from the third start to the end of the listPrint(Tinytuple * 2)#output tuple two timesPrint(Tuple + tinytuple)#Print a group of tuples#Output Result:" "(' Runoob ', 786, 2.23, ' John ', 70.2) Runoob (786, 2.23) (2.23, ' John ', 70.2) (123, ' John ', 123, ' John ') (' Runoob ', 786, 2.23, ' John ', 70.2, 123, ' John ')" "
2. By deleting
del tuple # delete entire tuple
3. Other
#The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:#!/usr/bin/python#-*-coding:utf-8-*-Tu = ('Runoob', 786, 2.23,'John', 70.2) Li= ['Runoob', 786, 2.23,'John', 70.2]#tu[2] = 1000 # tuples are illegal to apply#li[2] = 1000 # is a legitimate application in the list#Method:Print(Tu.index ('Runoob'))#Print the index value of an elementPrint(Tu.count ('Runoob'))#number of printed elements
Python data type-tuples