本文首发自伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!
1.元组
- tuple(元组)是一种特殊的列表,不同点是元组一旦创建就不能修改
>>> name_list = ('Yiluo', 'Tom', 'Lucy', 'Joe')
>>> name_list.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'pop'
>>> name_list.remove('Tom')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'remove'
- 如果是只读的数据,尽可能使用元组
- 元组中如果包含可变的数据元素,这些数据元素是可以修改的
>>> name_list = ('Yiluo', 'Tom', 'Lucy', ['Joe'])
>>> name_list[3].append('Morgan')
>>> name_list
('Yiluo'