Python3 tuple


Python’s tuples are similar to lists, except that the elements of the tuple cannot be modified.

Use parentheses for tuples and square brackets for lists.

Tuple creation is very simple, just add elements in brackets and separate them with commas.

The following example:

tup1 = ('Google', 'php', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

Create an empty tuple

tup1 = ();

When the tuple contains only one element, you need to add a comma after the element

tup1 = (50,);

element Groups are similar to strings. The subscript index starts from 0 and can be intercepted, combined, etc.


Accessing the tuple

The tuple can use the subscript index to access the value in the tuple, as shown in the following example:

#!/usr/bin/python3

tup1 = ('Google', 'php', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )

print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])

The output result of the above example:

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

Modify the tuple

The element values ​​in the tuple are not allowed to be modified, but we can connect and combine the tuples, as shown in the following example:

#!/usr/bin/python3

tup1 = (12, 34.56);
tup2 = ('abc', 'xyz')

# 以下修改元组元素操作是非法的。
# tup1[0] = 100

# 创建一个新的元组
tup3 = tup1 + tup2;
print (tup3)

Above Example output result:

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

Delete tuple

The element value in the tuple is not allowed to be deleted, but we can use the del statement to delete the entire tuple, as shown in the following example:

#!/usr/bin/python3

tup = ('Google', 'php', 1997, 2000)

print (tup)
del tup;
print ("删除后的元组 tup : ")
print (tup)

After the above instance tuple is deleted, the output variable will have exception information, and the output is as follows:

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

Tuple operator

and string Similarly, the + sign and * sign can be used to perform operations between tuples. This means that they can be combined and copied, resulting in a new tuple.

Python expressionResultDescription
len((1, 2 , 3))3Calculate 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)TrueWhether the element exists
for x in (1, 2, 3): print x,1 2 3 Iteration

Tuple index, interception

Because the tuple is also a sequence, we can access the tuple The element at the specified position can also intercept a section of elements in the index, as shown below:

Tuple:

L = ('Google', 'Taobao', 'php')
Python expression ResultDescription
L[2]'php!'Read the third element
L[-2]'Taobao'Reverse reading; read the penultimate element
L[1:]('Taobao', 'php!')Intercept elements, all elements starting from the second one.

The running example is as follows:

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

Tuple built-in functions

Python tuple contains the following built-in functions

##1len(tuple)
>>> tuple1 = ('Google', 'php', 'Taobao')
>>> len(tuple1)
3
>>>
2max(tuple)
>>> tuple2 = ('5', '4', '8')
>>> max(tuple2)
'8'
>>>
3min(tuple)
>>> tuple2 = ('5', '4', '8')
>>> min(tuple2)
'4'
>>>
4tuple(seq)
>>> list1= ['Google', 'Taobao', 'php', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'php', 'Baidu')
Serial numberMethod and descriptionExample
Calculate the number of tuple elements.
Returns the maximum value of the element in the tuple.
Returns the minimum value of the element in the tuple.
Convert a list to a tuple.