The tuple of the python3 tuple built-in function

Python3 tuple

Python is similar to the list, except that the tuple's elements cannot be modified. The

meta group uses parentheses and the list uses square brackets.

元组Create is very simple, just add elements in parentheses and separate them with commas. Examples

(Python 3.0 +)

>>> tup1 = ( '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 When the @@元 group contains only one element, you need to add a comma after the element, otherwise the brackets will be used as operators:

tup1 = ();

Instance (Python 3.0+)

>>>tup1 = (50) >>> type (tup1) # without comma, type integer <class' int '> >>> tup1 = (50,) >>> type (tup1) # comma, a tuple of type <class' Tuple'>

元组 is similar to a string. The subscript index starts from 0 and can be intercepted, combined, and so on.

访元组

元组You can use the subscript index to access the values ​​in the tuple, as in the following example:

Instance (Python 3.0+)

The above example output: @@ The @@改元

#!/usr/bin/python3 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])

元组 element value is not allowed to be modified, but we can connect and combine the tuple, as in the following example:

tup1[0]:  Google
tup2[1:5]:  (2, 3, 4, 5)

Instance (Python 3.0+)

#! / usr / bin / python3 tup1 = (12, 34.56); tup2 = ( 'abc', 'xyz') # following modifications tuple element operation is illegal. # Tup1 [0] = 100 # Create a new tuple tup3 = tup1 + tup2; Example output print (tup3)

above:

delete element value tuples in tuple It is not allowed to delete, but we can use the del statement to delete the entire tuple, as in the following example:

(12, 34.56, 'abc', 'xyz')

Instance (Python 3.0+)

#!/usr/bin/python3 tup = ('Google', 'Runoob', 1997, 2000) print (tup) del tup; print ( "after deletion tuples tup:") after print (tup)

above example tuple is deleted, there is abnormality information output variable, The output is as follows:

The tuple operator

is the same as the string, and the tuples can be operated with the + and * signs. This means that they can be combined and copied, and a new tuple is generated after the operation.

删除后的元组 tup : 
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print (tup)
NameError: name 'tup' is not defined

Python Expression

结果

描述 计算元素数
len((1, 2, 3)) 3
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6)
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!')
3 in (1, 2, 3) True
for x in (1, 2, 3): print (x,) 1 2 3

@@ @元组索引, intercept

because the tuple is also a sequence, so we can access the elements of the specified position in the tuple, or intercept an element in the index, as follows:

元组:

L = ('Google', 'Taobao', 'Runoob')
Python Expression 结果 描述
L[2] 'Runoob' Read the third element
L[-2] 'Taobao' 反读; read the second last element
L[1:] ('Taobao', 'Runoob') 截元素, All elements from the second start.

Run instance is as follows:

>>> L = ('Google', 'Taobao', 'Runoob')
>>> L[2]
'Runoob'
>>> L[-2]
'Taobao'
>>> L[1:]
('Taobao', 'Runoob')

元组 Built-in function

Python tuple contains the following built-in functions

方法和描述 Instance
1 len (tuple) Calculate the number of tuple elements.
>>> tuple1 = ('Google', 'Runoob', 'Taobao')
>>> len(tuple1)
3
>>> 
2 max(tuple) Returns the maximum value of the element in the tuple.
>>> tuple2 = ('5', '4', '8')
>>> max(tuple2)
'8'
>>> 
3 min(tuple) Returns the minimum value of the element in the tuple.
>>> tuple2 = ('5', '4', '8')
>>> min(tuple2)
'4'
>>> 
4 tuple(seq) Convert the list to a tuple.
>>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Runoob', 'Baidu')