列表是可变的,这意味着可以更改其内容:>>> x = [1,2]
>>> x.append(3)
>>> x
[1, 2, 3]
而元组则不是:>>> x = (1,2)
>>> x
(1, 2)
>>> x.append(3)
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'tuple' object has no attribute 'append'
另一个主要区别是元组是可使用的,这意味着可以使用它作为字典的键:>>> x = (1,2)
>>> y = [1,2]
>>> z = {}
>>> z[x] = 3
>>> z
{(1, 2): 3}
>>> z[y] = 4
Traceback (most recent call last):
File "", line 1, in
TypeError: unhashable type: 'list'
您可以将元组添加到一起,例如:>>> x = (1,2)
>>> x += (3,)
>>> x
(1, 2, 3)