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, -) tup2= (1,2,3,4,5) Tup3="a","b","C","D"
Create an empty tuple
Tup = ()
When you include only one element in a tuple, you need to add a comma after the element
Tup = (10,)
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:
#!/usr/bin/python tup1 = (' Physics ', ' Chemistry ', 1997, $) tup2 = (1, 2, 3, 4, 5, 6, 7) print "tup1[0]:", Tup1[0]print "Tup2[1:5]:", Tup2[1:5]
Execution Result:
tup1[0]: physicstup2[1:5]: (2345 )
modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:
#!/usr/bin/-*-coding:utf-8 -*-= (34.56= (' ABC'xyz') # The following modifications to the tuple element operation are illegal. # tup1[0= tup1 + tup2print tup3
Execution Result:
(34.56, ' 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:
#!/usr/bin/= ('physics'chemistry'1997 - ""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 (most recent call last): "test.py"9 in <module> ' tup ' are not Defined
Tuple operators
As with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.
| Python Expressions |
Results |
Description |
| Len ((1, 2, 3)) |
3 |
Count the number of elements |
| (1, 2, 3) + (4, 5, 6) |
(1, 2, 3, 4, 5, 6) |
Connection |
| (' hi! ',) * 4 |
(' hi! ', ' hi! ', ' hi! ', ' hi! ') |
Copy |
| 3 in (1, 2, 3) |
True |
Whether the element exists |
| For x in (1, 2, 3): Print x, |
1 2 3 |
Iteration |
Tuple index, intercept
Because tuples are also a sequence, we can access the elements at the specified location in the tuple, or we can intercept an element in the index as follows:
Meta-group:
L = (' spam ', ' spam ', ' spam! ')
| Python Expressions |
Results |
Description |
| L[2] |
' spam! ' |
Reading a third element |
| L[-2] |
' Spam ' |
Read backwards, read the second-to-last element |
| L[1:] |
(' Spam ', ' spam! ') |
Intercepting elements |
No close delimiter
Arbitrarily unsigned objects, separated by commas, are tuples by default, as in the following example:
#!/usr/bin/python print ' abc ', -4.24E93, 18+6.6j, ' xyz ' x, y = 1, 2print "Value of X, y:", X, y
Execution results
abc-4.24e+93 (18+6.6j) Xyzvalue of X, Y:1 2
Tuple built-in functions
The Python tuple contains the following built-in functions
| Serial Number |
Method and Description |
| 1 |
CMP (Tuple1, Tuple2) Compares two tuples of elements. |
| 2 |
Len (tuple) Counts the number of tuple elements. |
| 3 |
Max (tuple) Returns the element's maximum value in a tuple. |
| 4 |
MIN (tuple) Returns the element minimum value in a tuple. |
| 5 |
Tuple (SEQ) Converts a list to a tuple. |
12. Python tuples