Python中的tuple

来源:互联网 发布:数据库decode 编辑:程序博客网 时间:2024/05/21 17:02

1.空的tuple:    tup1 = ();

2.只含有一个元素的tuple:

tup1 = (50,); #注意,必须有,尽管只有一个元素

3Delete Tuple Elements:

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.

To explicitly remove an entire tuple, just use the del statement:

Example:

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

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

This will produce following result. Note an exception raised, this is because after del tup tuple does not exist any more:

('physics', 'chemistry', 1997, 2000)After deleting tup :Traceback (most recent call last): File "test.py", line 9, in <module> print tup;NameError: name 'tup' is not defined

 

 

相关函数:

Python includes following tuple functions

SNFunction with Description1cmp(tuple1, tuple2)
Compares elements of both tuples.2len(tuple)
Gives the total length of the tuple.3max(tuple)
Returns item from the tuple with max value.4min(tuple)
Returns item from the tuple with min value.5tuple(seq)
Converts a list into tuple.