元组
1.元组的特点
- 不可更改
- 元素是有序的
2.元组基本操作
- 元组相加 >>> d+(2,3)
(1, '34', 23, 2, 3)
>>> d+(23,)
(1, '34', 23, 23)
>>> d+(3) - 错误代码>>> d+(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "int") to tuple
>>> - 检测元素是否存在>>> 3 in d
False
>>> if not 3 in d:
... print('missing fail')
...
missing fail
>>>
- 元组索引>>> d[0]
1
>>> d[:]
(1, '34', 23)
3.使用元组的原因
他的不可更改性