#概述

元组俗称不可变的列表,又称只读列表,是python的基本数据类型之一, 用()小括号表示,里面使用,逗号隔开

元组里面可以放任何的数据类型的数据,查询可以,循环可以,但是就是不能修改

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

lass tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return def __add__(self, y): # real signature unknown; restored from __doc__
""" x.__add__(y) <==> x+y """
pass def __contains__(self, y): # real signature unknown; restored from __doc__
""" x.__contains__(y) <==> y in x """
pass def __eq__(self, y): # real signature unknown; restored from __doc__
""" x.__eq__(y) <==> x==y """
pass def __getattribute__(self, name): # real signature unknown; restored from __doc__
""" x.__getattribute__('name') <==> x.name """
pass def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__
"""
x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported.
"""
pass def __ge__(self, y): # real signature unknown; restored from __doc__
""" x.__ge__(y) <==> x>=y """
pass def __gt__(self, y): # real signature unknown; restored from __doc__
""" x.__gt__(y) <==> x>y """
pass def __hash__(self): # real signature unknown; restored from __doc__
""" x.__hash__() <==> hash(x) """
pass def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass def __iter__(self): # real signature unknown; restored from __doc__
""" x.__iter__() <==> iter(x) """
pass def __len__(self): # real signature unknown; restored from __doc__
""" x.__len__() <==> len(x) """
pass def __le__(self, y): # real signature unknown; restored from __doc__
""" x.__le__(y) <==> x<=y """
pass def __lt__(self, y): # real signature unknown; restored from __doc__
""" x.__lt__(y) <==> x<y """
pass def __mul__(self, n): # real signature unknown; restored from __doc__
""" x.__mul__(n) <==> x*n """
pass @staticmethod # known case of __new__
def __new__(S, *more): # real signature unknown; restored from __doc__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass def __ne__(self, y): # real signature unknown; restored from __doc__
""" x.__ne__(y) <==> x!=y """
pass def __repr__(self): # real signature unknown; restored from __doc__
""" x.__repr__() <==> repr(x) """
pass def __rmul__(self, n): # real signature unknown; restored from __doc__
""" x.__rmul__(n) <==> n*x """
pass def __sizeof__(self): # real signature unknown; restored from __doc__
""" T.__sizeof__() -- size of T in memory, in bytes """
pass tuple

tuple

#判断是否是元组

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

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

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

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

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

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

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

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

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

#例子:

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

#元组的索引和切片

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

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

最新文章

  1. requirejs:性能优化-及早并行加载
  2. python模块app登陆认证(M2Crypto数字证书加密)
  3. WebClient 访问https
  4. leetcode算法思想快速一览
  5. 图解Android - Android GUI 系统 (1) - 概论
  6. I2C转UART
  7. ulimit 参数介绍
  8. Linux 配置tomcat遇见的若干问题
  9. UVALive - 3644 X-Plosives (并查集)
  10. sql中having、group by用法及常用聚合函数
  11. java 判断是否为中文字符,部分,。中文符号不能识别
  12. python第七天
  13. Vue开发爬坑记录
  14. Java NIO工作机制简介
  15. Vasya and Multisets CodeForces - 1051C(英语限制了我的想象力)
  16. Provide your license server administrator with the following information.error code =-42,147
  17. PHP各环境下的伪静态配置
  18. PhotoSwipe中文API(三)
  19. 项目Beta冲刺(团队)第一天
  20. HttpWebRequest和HttpWebResponse的应用

热门文章

  1. 关于Apache Commons-IO的使用
  2. 让mybatis不再难懂(二)
  3. QQ消息群发助手(超级简单)
  4. android形状属性、锁屏密码、动态模糊、kotlin项目、抖音动画、记账app、视频播放器等源码
  5. EncryptUtils
  6. HTTP协议八种请求类型介绍
  7. URI和URL对比
  8. 在HADOOP中使用MRUNIT进行单元测试
  9. windows 不能在本地计算机启动apache2。有关更多信息,查阅系统事件日志。如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码1
  10. telnet不是内部或外部命令的问题解决