>>> 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. [原创]用“人话”解释不精确线搜索中的Armijo-Goldstein准则及Wolfe-Powell准则

    [原创]用“人话”解释不精确线搜索中的Armijo-Goldstein准则及Wolfe-Powell准则 转载请注明出处:http://www.codelast.com/ line search(一维 ...

  2. CodeForces 459A Pashmak and Garden(水~几何-给两点求两点组成正方形)

    题目链接:http://codeforces.com/problemset/problem/459/A 题目大意: 给出两个点(在坐标轴中),求另外两个点从而构成一个正方形,该正方形与坐标轴平行. 如 ...

  3. Python + OpenCV2 系列:3 - python 字符串,类,编码规范

    首先,强烈推荐<<简明 Python 教程>> Swaroop, C. H. 著 沈洁元 译 其实,这本书里已经把python的最基本的用法,编码等等介绍的很好,这里把我用到的 ...

  4. Robot Framework--11 RF结合Jenkins

    转自:http://blog.csdn.net/tulituqi/article/details/17846463 为什么我们要引入RF?其实最初我们引入RF是为了能够快速的开展自动化验收测试,为敏捷 ...

  5. RabbitMQ 命令行

    用户命令 .添加用户 rabbitmqctl add_user username password .删除用户 rabbitmqctl delete_user username .修改密码 rabbi ...

  6. 固定导航(Sticky nav)

    方法1: <div class="footer"></div> .footer{ position:fixed; bottom:0; left:0; wid ...

  7. 使用cachemanager做缓存(Session的缓存)

    1.我在这里直接用 cachemanager.redis 往redis里面存储缓存数据2.步骤 1)下载CacheManager.Redis(包含了CacheManager.Core) 下载Stack ...

  8. 2015年11月30日 spring初级知识讲解(一)装配Bean

    序,Spring的依赖注入是学习spring的基础.IOC为控制反转,意思是需要的时候就由spring生成一个,而不是先生成再使用. 写在前面 Spring提供面向接口编程,面向接口编程与依赖注入协作 ...

  9. WPF中的数据绑定!!!

    引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx  数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...

  10. Jquery Md5加密解密

    首先需要调用md5解析的js文件.(右击-目标另存为方式下载) http://files.cnblogs.com/files/colinliu/md5.js 加密方法参考: <script ty ...