Tuple(元组)
tuple和list(列表)相似,但是初始化后不能更改,速度比list快。
通过dir(tuple)可以发现只有count和index两个内建函数。
>>> t = 1234,'hello',x # t=(1234,'hello',x)的缩写
>>> a,b,c=t
>>> a
1234
>>> b
'hello'
>>> c
5
>>> t_add=t,(1,2,3)
>>> t_add
((1234, 'hello', 5), (1, 2, 3))
>>> t_add[0]
(1234, 'hello', 5)
>>> t_add[1]
(1, 2, 3)
>>> t[1:]
('hello', 5)
通过元组来进行数据交换:
>>> a,b=(1,2)
>>> a,b=b,a
>>> a,b
(2, 1)