元祖tuple 元祖不能改 只能读取 元祖里面存列表/字典(可变类型对象)是可以改变的
定义:a=()
>>> type(a)#查看类型 <class ‘tuple‘> >>> isinstance(a,tuple)#判断是否为元祖 True >>> a=1,2#不加括号也可自动转化为元祖 >>> type(a) <class ‘tuple‘> >>> a=(1)#只有一个元素是int类型 >>> type(a) <class ‘int‘> >>> a=(1,)#只放一个元素的元祖必须加逗号
1. 查
例:元祖取值
>>> a=(1,2,[4,5,6]) >>> a (1, 2, [4, 5, 6]) >>> a[2][0]#嵌套取值 4 >>>
遍历元祖
按值遍历
>>> for i in a: ... print(i) ... 1 2 [4, 5, 6] >>>
按坐标遍历
>>> a=(1, 2, [3, 4, 5, 6])
>>> for i in range(len(a)): ... print(a[i],end=‘ ‘) ... 1 2 [3, 4, 5, 6]
>>>
2.增 只能改变元祖内可变对象的值
>>> a=(1,2,[3,4,5]) >>> a[2].append(6) >>> a (1, 2, [3, 4, 5, 6])
>>> a.append(0)Traceback (most recent call last):File "<stdin>", line 1, in <module>AttributeError: ‘tuple‘ object has no attribute ‘append‘