码迷,mamicode.com
首页 > 编程语言 > 详细

python基础--元祖

时间:2019-10-01 22:33:57      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:查看类   mos   module   --   pytho   class   att   取值   元素   

元祖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‘

python基础--元祖

标签:查看类   mos   module   --   pytho   class   att   取值   元素   

原文地址:https://www.cnblogs.com/wenm1128/p/11592323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!