元组也是一种线性结构,和list类似,但是它是不可变
的
创建元组(tuple), 和List类似,但是由于不可变的特性,所以相比列表,它少了很多方法,注意如果只有一个元素
时,一定要在结尾加上,
,否则类型就不是元组了.
In [62]: t = ()
In [63]: type(t)
Out[63]: tuple
In [64]: t = (1)
In [65]: type(t)
Out[65]: int
In [66]: t = (1,)
In [67]: type(t)
Out[67]: tuple
In [68]: t
Out[68]: (1,)
In [69]: t[0]
Out[69]: 1
In [70]: t[0] = 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-70-29b3302c4f70> in <module>()
----> 1 t[0] = 2
TypeError: 'tuple' object does not support item assignment
In [71]: t2 = ('a')
In [72]: type(t2)
Out[72]: str
In [73]: t2
Out[73]: 'a'