|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
元组:
不可改变的类型,一旦建立不能修改。
创建和访问元组:
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)
tuple2 = tuple1
>>> tuple2
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1[1] = 3
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
tuple1[1] = 3
TypeError: 'tuple' object does not support item assignment
元组的关键是逗号,空元祖直接用().
>>> temp = (1)
>>> temp
1
>>> type(temp)
<class 'int'>
>>> temp2 = 2, 3, 4
>>> type(temp2)
<class 'tuple'>
>>> 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 = (1, 2, 4, 5)
>>> temp = temp[:2] + (3,) + temp[2:]
>>> temp
(1, 2, 3, 4, 5)
>>> del temp #删除该元组,没有标签指向它时,python会自动删除
>>> temp
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
temp
NameError: name 'temp' is not defined
元组支持拼接、重复、关系操作符、成员操作符、逻辑操作符。
|
|