|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
由于和列表是近亲关系,所以元组和列表在实际使用上是非常相似的。
区别元组和列表
1)创建和访问一个元组>>> tuple1 = (1,2,3,4,5,6,7,8)
>>> tuple1
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1[1]
2
>>> tuple1[5:]
(6, 7, 8)
>>> tuple1[:5]
(1, 2, 3, 4, 5)
>>> tuple2 = tuple1[:]
>>> tuple2
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple2 = tuple1[:]
>>> tuple2
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1[1] = 3
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tuple1[1] = 3
TypeError: 'tuple' object does not support item assignment
>>> temp = (1)
>>> temp
1
2)更新和删除一个元组>>> type(temp)
<class 'int'>
>>> temp2 = 2,3,4
>>> type(temp2)
<class 'tuple'>
>>> temp = []
>>> type(temp)
<class 'list'>
>>> temp = ()
>>> type(temp)
<class 'tuple'>
>>> temp = (1,)
>>> type(temp)
<class 'tuple'>
>>> temp = 1,
>>> type(temp)
<class 'tuple'>
>>> 8 * (8)
64
>>> 8 * (8,)
(8, 8, 8, 8, 8, 8, 8, 8)
>>> temp = ('小甲鱼','黑夜','迷途','小布丁')
>>> temp = temp[:2] + ('怡静',) + temp[2:]
>>> temp
('小甲鱼', '黑夜', '怡静', '迷途', '小布丁')
>>> del temp
>>> temp
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
temp
NameError: name 'temp' is not defined
3)元组相关的操作符 |
评分
-
查看全部评分
|