python基本数据类型——tuple
2017-06-02 23:50
357 查看
一、元组的创建与转换:
ages = (11, 22, 33, 44, 55) ages = tuple((11, 22, 33, 44, 55)) ages = tuple([]) # 字符串、列表、字典(默认是key)
元组基本上可以看成不可修改的列表
tuple(iterable),可以存放所有可迭代的数据类型
二、元组的不可变性
如:t = (17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse'])元组t中的元素数字17和字符串‘Jesse’以及元组('LinuxKernel', 'Python')本身属于不可变元素,故其在元组中不可更新;但是其中包含的列表[17, 'Jesse']本身属于可变元素,故:
>>> t = (17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse']) >>> t (17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse']) >>> t[0] = 18 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> t[3] [17, 'Jesse'] >>> t[3][0] = 21 >>> t (17, 'Jesse', ('LinuxKernel', 'Python'), [21, 'Jesse'])
三、元组常用操作
#count(self,value) #功能:统计当前元组中某元素的个数 tup = (55,77,85,55,96,99,22,55,) tup.count(55) #返回结果:3 元素‘55’在元组tup中出现了3次 #index(self, value, start=None, stop=None) 功能:获取元素在元组中的索引值,对于重复的元素,默认获取从左起第一个元素的索引值 tup = (55,77,85,55,96,99,22,55,) tup.index(55) 返回结果:0 tup.index(85) 返回结果:2 tup.index(55,2,7) 返回结果:3
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
tuple源码
相关文章推荐
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- 自学Python2.3-基本数据类型-元组tuple(object) 方法
- python基本数据类型——tuple
- python基本数据类型——tuple
- python基本数据类型list,tuple,set,dict用法以及遍历方法
- python基本数据类型——tuple
- python教程19--《python基本数据类型list、tuple、dict、linecache读取file内容大综合运用》
- python基本数据类型——tuple
- Python基本数据类型之tuple
- python基本数据类型——tuple
- python基础教程之基本内置数据类型介绍
- Python基础02 基本数据类型
- 4种编程语言基本数据类型及其取值范围整理(C++,Java,Python,Go)
- 基础篇6-福利课-python基本数据类型讲解1.2
- Python基本数据类型详细介绍
- Python基本数据类型详细介绍
- 一起学Python 2.1 开始加一些python的基本数据类型进入你的程序
- Python基础02 基本数据类型
- 基本数据类型-python
- 基础篇7-python基本数据类型讲解1.3