>>> dir(tuple)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> tuple
<type 'tuple'>
>>> tuple_1=(1,2,3,4,5,6)
>>> tuple_1
(1, 2, 3, 4, 5, 6)
>>> tuple[0]=1 Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
tuple[0]=1
TypeError: 'type' object does not support item assignment
>>>

tuple就是元组的类型,他和列表相近,但是也有很大的区别,如上,tuple不能修改元素,如果修改就会报错,如上

判断一个列表可以看中括号,但是看元组的依据主要是逗号

eg:如果我们只是需要一个元素,如果不加上逗号,那么会被默认为一个整数类型,如果在后面加上一个逗号,就会被认为是tuple

>>> tuple_2=(1)
>>> tuple_2
1
>>> type(tuple_2)
<type 'int'>
>>> tuple_3=(1,)
>>> tuple_3
(1,)
>>> type(tuple_3)
<type 'tuple'>
>>>

但是对于空的加不加逗号就不影响了

>>> list1=[]
>>> type(list1)
<type 'list'>
>>> tuple1=()
>>> type(tuple1)
<type 'tuple'>
>>>

因为元组不能更改,所有列表里面的3中添加和删除都不能使用

但是我们可以通过切片进行增加和删除

>>>
>>> tmp1=(1,2,4,5,6)
>>> tmp1=tmp1(:2)
SyntaxError: invalid syntax
>>> tmp1=tmp1[:2]+tmp[3:] Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
tmp1=tmp1[:2]+tmp[3:]
NameError: name 'tmp' is not defined
>>> tmp1=tmp1[:2]+tmp1[3:]
>>> tmp1
(1, 2, 5, 6)
>>> tmp1=tmp1[:2]+(4,) #这里注意加上的单个元素一定要加上逗号
>>> tmp1
(1, 2, 4)
>>>

python——tuple元组的更多相关文章

  1. Python tuple 元组

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 ...

  2. Python Tuple(元组) tuple()方法

    描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- ...

  3. Python Tuple(元组) min()方法

    描述 Python 元组 min() 函数返回元组中元素最小值.高佣联盟 www.cgewang.com 语法 min()方法语法: min(tuple) 参数 tuple -- 指定的元组. 返回值 ...

  4. Python Tuple(元组) max()方法

    描述 Python 元组 max() 函数返回元组中元素最大值.高佣联盟 www.cgewang.com 语法 max()方法语法: max(tuple) 参数 tuple -- 指定的元组. 返回值 ...

  5. Python Tuple(元组) len()方法

    描述 Python 元组 len() 函数计算元组元素个数.高佣联盟 www.cgewang.com 语法 len()方法语法: len(tuple) 参数 tuple -- 要计算的元组. 返回值 ...

  6. Python tuple元组学习

    1.tuple和list非常类似,但是tuple一旦初始化就不能修改 classmates = ('Michael', 'Bob', 'Tracy') 现在,classmates这个tuple不能变了 ...

  7. Python Tuple元组的操作说明

    Tuple的特性在于,它的元素是不可变的(immutable),一旦设定,就不能使用索引去修改. >>> t1=1,2,3,4,5 #给Tuple赋值 >>> t1 ...

  8. Python Tuple(元组) cmp()方法

    描述 Python 元组 cmp() 函数用于比较两个元组元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cmp(tuple1, tuple2) 参数 tuple1 -- ...

  9. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...

随机推荐

  1. Python学习笔记——基本语法

    1.程序输出——print语句 >>> myString = 'Hello World!' >>> print myString Hello World! > ...

  2. 深入浅出MyBatis

    参考文献:深入浅出MyBatis MyBatis功能架构图: MyBatis内部原理流程图: 详情见:深入浅出MyBatis

  3. hasClass addClass removeClass

    //函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...

  4. 使用commons-fileupload包进行大文件上传注意事项

    项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...

  5. IIS 8.5 伪静态去掉index.php thinkphp 3.2.2

    因为测试都是在win下开发的 win8.1企业版 II8.5 首先安装  Thinkphp 3.2.2 URL Rewrite Module 2.0 http://www.iis.net/downlo ...

  6. Python基础之【第三篇】

    dir(): 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名 vars() 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名和值 reload() 将以前导入 ...

  7. svn branch and merge(svn切换分支和合并)详解

    下文的实践主要是参考了TortoiseSVN的帮助文档和Subversion的在线文档,Subversion的在线文档:http://svnbook.red-bean.com/en/1.5/svn-b ...

  8. iOS9 适配

    iOS适配的相关内容的整理 之前iOS开发者一直很庆幸自己不用像安卓开发者那样适配各种不同类型的机型,但如今随着iPhone各种机型的改变,适配也成了我们开发中必须会的内容了.首先我们来了解一下对于不 ...

  9. 【转】asp.net mvc 页面跳转

    1.使用传统的Response.Redirect例如string url = "/account/create";Response.Redirect(url); 1.Server. ...

  10. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...