列表可以随便改变元素,元组不能随便改变元素,
>>> tuple1 = (1,2,3,4,5) #创建元组
>>> tuple1
(1, 2, 3, 4, 5)
>>> tuple1[1] #索引元组
2
>>> tuple1[2:]
(3, 4, 5)
>>> tuple1[:]
(1, 2, 3, 4, 5)
>>> list1 = [1,23,4,5,6]
>>> list1[2] = 6 #列表可以随便改变元素
>>> list1
[1, 23, 6, 5, 6]
>>> tuple1[1] = 2 #元组不能随便改变元素
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
tuple1[1] = 2
TypeError: 'tuple' object does not support item assignment
>>> temp = (1)
>>> temp
1
>>> type(temp)
<class 'int'>
>>> temp = (1,) #逗号是关键,不加逗号就是int类型
>>> temp
(1,)
>>> type(temp)
<class 'tuple'>
>>> temp = ()
>>> temp
()
>>> temp = 1,
>>> type(temp)
<class 'tuple'>
>>> 8 * (8)
64
>>> 8 * (8,)
(8, 8, 8, 8, 8, 8, 8, 8)
>>> temp = ("小甲鱼","小鲤鱼","中华鲟") #利用分片在
>>> temp = temp[:1]+("小金鱼",)+temp[1:] #注意小金鱼后面的逗号
>>> temp
('小甲鱼', '小金鱼', '小鲤鱼', '中华鲟')
>>> del temp #删除元组
>>> temp
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
temp
NameError: name 'temp' is not defined