列表:一个大仓库,你可以随时往里面添加和删除任何东西。

元组:封闭的列表,一旦定义不可改变。

与列表的不同:不可更改,其中逗号是关键,用小括号()的不一定是元组,创建和更新用拼接,删除del很少用

>>> temp=2,3,4
>>> type(temp)
<class 'tuple'>
>>> temp1=(1,)
>>> type(temp1)
<class 'tuple'>
>>> temp3=(1)
>>> type(temp3)
<class 'int'>
>>> mix=('北国风光', 1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空')
>>> mix=mix[:1]+('黑夜',)+mix[1:]
>>> mix
('北国风光', '黑夜', 1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空')
>>> mix=mix[:3]+('小布丁',)+mix[3:]
>>> mix
('北国风光', '黑夜', 1, '小布丁', 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空')

 

posted on 2018-01-04 13:48  Samyll  阅读(76)  评论(0编辑  收藏  举报