《零基础入门学习Python》(小甲鱼)P14 学习笔记
元组:不可改变的类型,一旦建立不能修改。
创建和访问元组:
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1
(1, 2, 3, 4, 5, 6, 7, 8)
tuple1
2
>>> tuple1
(6, 7, 8)
tuple2 = tuple1
>>> tuple2
(1, 2, 3, 4, 5, 6, 7, 8)
>>> tuple1 = 3
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
tuple1 = 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
>>> 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
元组支持拼接、重复、关系操作符、成员操作符、逻辑操作符。
有切片 我看懂了
页:
[1]