#概述

  1. 元组俗称不可变的列表,又称只读列表,是python的基本数据类型之一, 用()小括号表示,里面使用,逗号隔开
  2.  
  3. 元组里面可以放任何的数据类型的数据,查询可以,循环可以,但是就是不能修改

#先来看看tuple元组的源码写了什么,方法:按ctrl+鼠标左键点tuple

  1. lass tuple(object):
  2. """
  3. tuple() -> empty tuple
  4. tuple(iterable) -> tuple initialized from iterable's items
  5.  
  6. If the argument is a tuple, the return value is the same object.
  7. """
  8. def count(self, value): # real signature unknown; restored from __doc__
  9. """ T.count(value) -> integer -- return number of occurrences of value """
  10. return
  11.  
  12. def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  13. """
  14. T.index(value, [start, [stop]]) -> integer -- return first index of value.
  15. Raises ValueError if the value is not present.
  16. """
  17. return
  18.  
  19. def __add__(self, y): # real signature unknown; restored from __doc__
  20. """ x.__add__(y) <==> x+y """
  21. pass
  22.  
  23. def __contains__(self, y): # real signature unknown; restored from __doc__
  24. """ x.__contains__(y) <==> y in x """
  25. pass
  26.  
  27. def __eq__(self, y): # real signature unknown; restored from __doc__
  28. """ x.__eq__(y) <==> x==y """
  29. pass
  30.  
  31. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  32. """ x.__getattribute__('name') <==> x.name """
  33. pass
  34.  
  35. def __getitem__(self, y): # real signature unknown; restored from __doc__
  36. """ x.__getitem__(y) <==> x[y] """
  37. pass
  38.  
  39. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  40. pass
  41.  
  42. def __getslice__(self, i, j): # real signature unknown; restored from __doc__
  43. """
  44. x.__getslice__(i, j) <==> x[i:j]
  45.  
  46. Use of negative indices is not supported.
  47. """
  48. pass
  49.  
  50. def __ge__(self, y): # real signature unknown; restored from __doc__
  51. """ x.__ge__(y) <==> x>=y """
  52. pass
  53.  
  54. def __gt__(self, y): # real signature unknown; restored from __doc__
  55. """ x.__gt__(y) <==> x>y """
  56. pass
  57.  
  58. def __hash__(self): # real signature unknown; restored from __doc__
  59. """ x.__hash__() <==> hash(x) """
  60. pass
  61.  
  62. def __init__(self, seq=()): # known special case of tuple.__init__
  63. """
  64. tuple() -> empty tuple
  65. tuple(iterable) -> tuple initialized from iterable's items
  66.  
  67. If the argument is a tuple, the return value is the same object.
  68. # (copied from class doc)
  69. """
  70. pass
  71.  
  72. def __iter__(self): # real signature unknown; restored from __doc__
  73. """ x.__iter__() <==> iter(x) """
  74. pass
  75.  
  76. def __len__(self): # real signature unknown; restored from __doc__
  77. """ x.__len__() <==> len(x) """
  78. pass
  79.  
  80. def __le__(self, y): # real signature unknown; restored from __doc__
  81. """ x.__le__(y) <==> x<=y """
  82. pass
  83.  
  84. def __lt__(self, y): # real signature unknown; restored from __doc__
  85. """ x.__lt__(y) <==> x<y """
  86. pass
  87.  
  88. def __mul__(self, n): # real signature unknown; restored from __doc__
  89. """ x.__mul__(n) <==> x*n """
  90. pass
  91.  
  92. @staticmethod # known case of __new__
  93. def __new__(S, *more): # real signature unknown; restored from __doc__
  94. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  95. pass
  96.  
  97. def __ne__(self, y): # real signature unknown; restored from __doc__
  98. """ x.__ne__(y) <==> x!=y """
  99. pass
  100.  
  101. def __repr__(self): # real signature unknown; restored from __doc__
  102. """ x.__repr__() <==> repr(x) """
  103. pass
  104.  
  105. def __rmul__(self, n): # real signature unknown; restored from __doc__
  106. """ x.__rmul__(n) <==> n*x """
  107. pass
  108.  
  109. def __sizeof__(self): # real signature unknown; restored from __doc__
  110. """ T.__sizeof__() -- size of T in memory, in bytes """
  111. pass
  112.  
  113. tuple

tuple

#判断是否是元组

  1. print((3)) #不是元组
  2. tu = (3, ) #元组中如果只有一个元素,需要在括号里写一个,逗号,否则就不是元组
  3. tu = tuple() #空元组写法
  4. print(type(tu)) #<class 'tuple'>

#验证元组不能增删改,查看索引就可以

  1. tu = ("人民币","美元","美金","欧元")

  2. tu.append("张三") #不允许添加:tuple' object has no attribute 'append'

  3. tu[0] = "日元" #不允许修改 :object does not support item assignment

  4. del tu[2] #报错,不允许删除:'tuple' object doesn't support item deletion

  5. print(tu[2]) #索引就可以
    #欧元

#关于元组不可变的注意点

  1. 这里元组的不可变意思是子元素不可变,但是子元素内容的子元素是可以变的,这要取决子元素本身是否是可变

#例子:

  1. # 因为列表是可变长类型,所以可以在里面增加 tu = (1,"蒋小鱼","鲁炎",[]) tu[3].append("张冲") print(tu)
  2. # (1, '蒋小鱼', '鲁炎', ['张冲'])
  3.  
  4. #需要注意的是,元组的第一层是不能进行赋值的,内部元素是没有要求的
  5. #例子
  6. #tu = (1,"巴朗","项羽","张飞") #像这样子是不能进行赋值的

#元组的索引和切片

  1. tu = (3,4,5,6,7,8)
  2. #索引:下标从0开始找
  3. print(tu[0]) #
  4. print(tu[1]) #
  5. #切片
  6. print(tu[1:3]) #(4, 5)

#详细使用可参考str字符串篇

python入门到放弃-基本数据类型之tuple元组的更多相关文章

  1. python入门到放弃-基本数据类型之dcit字典

    1.概述 字典是python中唯一的一个映射类型,以{}大括号括起来的键值对组成 字典中的key是唯一的,必须是可hash,不可变的数据类型 语法:{key1:value,key2:value} #扩 ...

  2. python入门(8)数据类型和变量

    python入门(8)数据类型和变量 数据类型 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样 ...

  3. pyton全栈开发从入门到放弃之数据类型与变量

    一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...

  4. Python入门 常量 注释 基础数据类型 用户输入 流程控制

    Python入门 一.常量 在Python中,不像其他语言有绝对的常量,修改会报错,在Python中有个约定俗成的规定--常量就是将变量名大写. 尽量保持不更改的一种量 , 这个常量有是干什么的呢 其 ...

  5. Python入门到放弃

    前传:计算机基础 01-计算机基础1 02-计算机基础2 第一章:Python入门 01-python入门之解释器环境安装 02-python入门之变量和基本数据类型 03-python内存管理之垃圾 ...

  6. 跟着ALEX 学python day2 基础2 模块 数据类型 运算符 列表 元组 字典 字符串的常用操作

    声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  模块初始: Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相 ...

  7. python入门基础知识三(列表和元组)

    列表(list)的操作 1. 形式 var = ['char1','char2','char3',...] var = [value1,value2,value3,...] 2. 列表的增删改查 查: ...

  8. Python入门一:基本数据类型

    作为一个刚入门编程的大一狗,第一次写博客,希望能对自己学的知识进行巩固和提升,也希望记录自己成长的过程. 学习Python,一是因为暑假学的c++头疼,听说Python简单,那我就试试吧,二是因为Py ...

  9. Python 入门(四)List和Tuple类型

    创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...

随机推荐

  1. 新开通blog

    从今天开始我有blog了,,以后要经常总结一些自己接触的东西,提升自己

  2. dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性

    dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性 分类: Java2011-06-03 16:14 976人阅读 评论(0) 收藏 举报 xml扩展语言 今天在开发sitemap地 ...

  3. Java多线程处理任务(摘抄)

    很多时候,我们需要对一个庞大的队列或者二维数组进行处理.这些处理可能是循环的,比如给一个excel多个sheet的联系人列表发邮件.很幼稚的方法就是用一个或者两个FOR循环搞定,对于庞大的数据有得让你 ...

  4. iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码

    iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...

  5. F5 BIG-IP LTM负载均衡策略

    standard 模式 可以很好的 防止 DDOS攻击 轮询设置是在POOL 中设置 按比率设置是在Node节点中设置

  6. [LC] 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. 吴裕雄--天生自然 HADOOP大数据分布式处理:使用XShell远程连接主机与服务器并配置它们之间SSH免密登录

  8. Block to|wreck|Range|Reach|span|chase around|amuse|exploit |instructed

    English note: Block to 纷涌而至 destroy多指彻底地.毁灭性地破坏,含导致无用,不能或很难再修复的意味. wreck侧重指船只.车辆.房屋等受到严重破坏或完全毁坏,也可指计 ...

  9. MAYA安装未完成,某些产品无法安装的解决方法

    MAYA提示安装未完成,某些产品无法安装该怎样解决呢?,一些朋友在win7或者win10系统下安装MAYA失败提示MAYA安装未完成,某些产品无法安装,也有时候想重新安装MAYA的时候会出现本电脑wi ...

  10. python3下应用pymysql(第一卷)

    编程不会操作数据库,就像男人做做了太监,人生不完整,我不想人生不完整,写下pymysql的使用总结 先做下准备工作,准备下数据表,由于是练习操作,所以先做个简单的数据表: 创建单独的一个库:再创建表 ...