1、元组与列表的区别
1.1.tuple与list类似,简单说tuple不可以修改, 而list可以修改
1.2.list创建时,用“[ ]”。tuple创建时大部分是用“()”和“,”
>>> list1 = [1]
>>> tuple1 = (1, )
>>> tuple1
(1,)
>>> list1
[1]
2、相同方式
2.1.访问tuple
>>> tuple2= (1, 2, 3, 4, 5)
>>> tuple1[2]
3
>>> 2 * tuple1
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
2.2.更新tuple
>>> tuple3
('键盘', '鼠标', '显示器', '主机')
>>> tuple3 = tuple3[:2] + ('电源',) + tuple3[2:]
>>> tuple3
('键盘', '鼠标', '电源', '显示器', '主机')
2.3.删除tuple
>>> del tuple2
>>> tuple2
Traceback (most recent call last):
File "", line 1, in
tuple2
NameError: name 'tuple2' is not defined
2.4.其他,‘<’'>','or','in'等