Latest web development tutorials

Python 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 = ('physics', 'chemistry', 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:


tup1 = ('physics', 'chemistry', 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]:  physics
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:

# -*- coding: UTF-8 -*-

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:


tup = ('physics', 'chemistry', 1997, 2000);

print tup;
del tup;
print "After deleting tup : "
print tup;

After the above example tuple is deleted, the output variable will be exceptions, output is as follows:

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
  File "", line 9, 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:


L = ('spam', 'Spam', 'SPAM!')
Python expression result description
L [2] 'SPAM!' Read the third element
L [-2] 'Spam' Read backwards; read the second last element
L [1:] ( 'Spam', 'SPAM!') Interception element

No close delimiter

Any unsigned object, separated by commas, default is tuple following examples:


print 'abc', -4.24e93, 18+6.6j, 'xyz';
x, y = 1, 2;
print "Value of x , y : ", x,y;

Examples of the above operating results:

abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2

Tuple built-in functions

Python tuple contains the following built-in functions

No. Method and Description
1 cmp (tuple1, tuple2)
Compare two tuples of elements.
2 len (tuple)
Calculate the number of tuple elements.
3 max (tuple)
Returns the maximum element tuple.
4 min (tuple)
Returns the minimum tuple elements.
5 tuple (seq)
The lists into tuples.