现在的位置: 首页 > 综合 > 正文

Python中的tuple

2013年11月28日 ⁄ 综合 ⁄ 共 918字 ⁄ 字号 评论关闭

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

SN Function with Description
1 cmp(tuple1, tuple2)

Compares elements of both tuples.
2 len(tuple)

Gives the total length of the tuple.
3 max(tuple)

Returns item from the tuple with max value.
4 min(tuple)

Returns item from the tuple with min value.
5 tuple(seq)

Converts a list into tuple.

抱歉!评论已关闭.