https://www.runoob.com/python3/python3-tuple.html
Tuples use parentheses, square brackets list.
Tuple create very simple, only need to add elements in parentheses and separated by commas can be.
Python is similar to a list of tuples, except that the elements of the tuple can not be modified. When the object is an element (such as List), then the referenced object may not be modified, but the values which may be modified.
E.g:
tp=(1,2,[c,d])
tp [2] [0] = a c will be changed to a time, which is allowed.
TUP1 = >>> ( ' the Google ' , ' Runoob ' , 1997 , 2000 ); >>> tup2 = ( . 1 , 2 , . 3 , . 4 , . 5 ); >>> tup3 = " A " , " B " , " C " , " D " ; # brackets need not be >>> type (tup3) < class ' tuple ' >
Empty tuples
tup1 = ();
When tuple contains only one element, the element needs to be added after the comma, or brackets will be used as the operator: Examples (the Python 3.0 + ) >>> = TUP1 ( 50 ) >>> type (TUP1) # without comma, type Integer < class ' int ' > TUP1 = >>> ( 50 ,) >>> type (TUP1) # comma, a tuple of type < class ' tuple ' >
access:
tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5])
delete
tup = ('Google', 'Runoob', 1997, 2000) print (tup) tup; Print ( " after deleting tuples TUP: " ) print (tup) After the deleting tuples tup: Traceback (most recent call last): File "test.py", line 8, in <module> print (tup) NameError: name 'tup' is not defined
Operators tuple
Like the string, a tuple can be used between + and calculates asterisk. This means that they can be combined and after replication operation will generate a new tuple.
Python expression | result | description |
---|---|---|
as ((1, 2, 3)) | 3 | Calculating 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 | Element exists |
for x in (1, 2, 3): print (x,) | 1 2 3 | Iteration |
Tuple index, taken
Because a sequence of tuples is, we can access the element at the location of a tuple, some elements may be taken in the index, as follows:
Tuple:
L = ('Google', 'Taobao', 'Runoob')
Python expression | result | description |
---|---|---|
L[2] | 'Runoob' | Read the third element |
L[-2] | 'Taobao' | Reverse reading; read penultimate element |
L[1:] | ('Taobao', 'Runoob') | Intercepting elements, all the elements from the second start. |
Run the examples are as follows:
>>> L = ('Google', 'Taobao', 'Runoob') >>> L[2] 'Runoob' >>> L[-2] 'Taobao' >>> L[1:] ('Taobao', 'Runoob')
Tuple built-in functions
Python tuple contains the following built-in functions
No. | Method and Description | Examples |
---|---|---|
1 | len (tuple) calculates the number of tuples of elements. |
>>> tuple1 = ('Google', 'Runoob', 'Taobao') >>> len(tuple1) 3 >>> |
2 | max (tuple) Returns the maximum element tuple. |
>>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>> |
3 | min (tuple) returns the minimum value tuple element. |
>>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>> |
4 | tuple (seq) converts a list of tuples. |
>>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'Runoob', 'Baidu') |