Latest web development tutorials

Python3 tuple

Python tuple with a list of similar, except that the tuple can not be modified.

Tuples with parentheses, use square brackets.

Tuple create very simple, only need to add the elements in parentheses and separated by commas can be.

The following examples:

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

Empty tuples

tup1 = ();

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

tup1 = (50,);

Tuple is similar to a string subscript index is zero, may be intercepted, combinations and the like.


Access tuple

Tuples can use subscript index to access the tuple values ​​following examples:

#!/usr/bin/python3

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

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

Examples of the above output:

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

Modifying Meta Group

Tuple element values ​​can not be modified, but we can connect a combination of tuples, the following examples:

#!/usr/bin/python3

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

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

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

Examples of the above output:

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

Remove tuple

Tuple element values ​​can not be deleted, but we can use the del statement to delete the entire tuple, the following examples:

#!/usr/bin/python3

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

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

After the above example tuple is deleted, the output variable will be exceptions, 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 operators

And strings, tuples can be used between + and * are operated. That means they can generate a new tuple after combination and replication operations.

Python expression result description
len ((1, 2, 3)) 3 Calculate 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, 123 Iteration

Tuple index, interception

Because tuple is a sequence, so we can access the tuple element specifies the location to be intercepted in the index section of the elements, as follows:

Tuple:

L = ('Google', 'Taobao', 'w3big')
Python expression result description
L [2] 'W3big!' Read the third element
L [-2] 'Taobao' Read backwards; read the second last element
L [1:] ( 'Taobao', 'w3big!') Interception All elements from the second after the start.

Run the examples are as follows:

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

Tuple built-in functions

Python tuple contains the following built-in functions

No. Method and Description Examples
1 len (tuple)
Calculate the number of tuple elements.
>>> tuple1 = ('Google', 'w3big', '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 tuple elements.
>>> tuple2 = ('5', '4', '8')
>>> min(tuple2)
'4'
>>> 
4 tuple (seq)
The lists into tuples.
>>> list1= ['Google', 'Taobao', 'w3big', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'w3big', 'Baidu')