PythonMeta-group
A python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
The following example:
Tup1 = (' Physics ', ' Chemistry ', 1997, n= (1, 2, 3, 4, 5= "A", "B", "C", "D";
To create an empty tuple:
Tup1 = ();
when you include only one element in a tuple, you need to add a comma after the element :
Tup1 = (50,);
Tuples are similar to strings, and subscript indexes start at 0 and can be intercepted, combined, and so on.
accessing tuples
Tuples can use the subscript index to access the values in the tuple, as in the following example:
Tup1 = (' Physics ', ' Chemistry ', 1997, #= (1, 2, 3, 4, 5, 6, 7"tup1[0]:", tup1[0"Tup2[1:5]: ", Tup2[1:5]
Results:
Tup1[0]: physicstup2[1:5]: (2, 3, 4, 5)
modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:
Tup1 = (34.56 = ('abc''xyz'); # The following modifications to tuple element operations are illegal. # tup1[0] = +; # Create a new tuple Tup3 = tup1 + tup2; print tup3;
Results:
' ABC ' ' XYZ ')
Delete a tuple
element values in tuples are not allowed to be deleted, but we can use the DEL statement to delete an entire tuple, as in the following example:
Tup = ('physics'chemistry', 1997,); Print Tup; del Tup; Print " "print tup;
When the above instance tuple is deleted, the output variable will have exception information, the output is as follows:
('physics'chemistry', 1997, *) after deleting tup: Traceback (recent): "test.py" in <module> print ' tup ' is not defined
Basic Python Tutorial Note 13: Tuples