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 = (' Google ', ' W3cschool ', 1997, n); tup2 = (1, 2, 3, 4, 5); tup3 = "A", "B", "C", "D";
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:
#!/usr/bin/python3tup1 = (' Google ', ' W3cschool ', 1997, $) tup2 = (1, 2, 3, 4, 5, 6, 7) print ("tup1[0]:", tup1[0]) print ("Tup2[1:5]:", Tup2[1:5])
The result of the above example output:
Tup1[0]: Googletup2[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:
#!/usr/bin/python3tup1 = (34.56); tup2 = (' abc ', ' XYZ ') # The following modification of tuple element operations is illegal. # Tup1[0] = 100# Create a new tuple Tup3 = Tup1 + tup2;print (TUP3)
The result of the above example output:
(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/python3tup = (' Google ', ' W3cschool ', 1997, +) print (tup) del tup;print ("Deleted tuple tup:") print (TUP)
When the above instance tuple is deleted, the output variable will have exception information, the output is as follows:
Deleted tuple Tup:traceback (most recent call last): File "test.py", line 8, in <module> print (tup) Nameerror:na Me ' tup ' is 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 = (' Google ', ' Taobao ', ' W3cschool ')
| Python Expressions |
Results |
Description |
| L[2] |
' w3cschool! ' |
Reading a third element |
| L[-2] |
' Taobao ' |
Read the second last element of the inverse |
| L[1:] |
(' Taobao ', ' w3cschool! ') |
Intercepts elements, starting from the second after all elements. |
Run the following example:
>>> L = (' Google ', ' Taobao ', ' W3cschool ') >>> l[2] ' w3cschool ' >>> l[-2] ' Taobao ' >>> L [1:] (' Taobao ', ' W3cschool ')
Tuple built-in functions
The Python tuple contains the following built-in functions
| Serial Number |
Method and Description |
Example |
| 1 |
Len (tuple) Counts the number of tuple elements. |
|
| 2 |
Max (tuple) Returns the element's maximum value in a tuple. |
|
| 3 |
MIN (tuple) Returns the element minimum value in a tuple. |
|
| 4 |
Tuple (SEQ) Converts a list to a tuple. |
>>> list1= [' Google ', ' Taobao ', ' w3cschool ', ' Baidu ']>>> tuple1=tuple (list1) >>> tuple1 (' Google ', ' Taobao ', ' w3cschool ', ' Baidu ') |
Python3 Meta-Group