python入门到放弃-基本数据类型之tuple元组
#概述
- 元组俗称不可变的列表,又称只读列表,是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字符串篇
python入门到放弃-基本数据类型之tuple元组的更多相关文章
- python入门到放弃-基本数据类型之dcit字典
1.概述 字典是python中唯一的一个映射类型,以{}大括号括起来的键值对组成 字典中的key是唯一的,必须是可hash,不可变的数据类型 语法:{key1:value,key2:value} #扩 ...
- python入门(8)数据类型和变量
python入门(8)数据类型和变量 数据类型 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样 ...
- pyton全栈开发从入门到放弃之数据类型与变量
一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...
- Python入门 常量 注释 基础数据类型 用户输入 流程控制
Python入门 一.常量 在Python中,不像其他语言有绝对的常量,修改会报错,在Python中有个约定俗成的规定--常量就是将变量名大写. 尽量保持不更改的一种量 , 这个常量有是干什么的呢 其 ...
- Python入门到放弃
前传:计算机基础 01-计算机基础1 02-计算机基础2 第一章:Python入门 01-python入门之解释器环境安装 02-python入门之变量和基本数据类型 03-python内存管理之垃圾 ...
- 跟着ALEX 学python day2 基础2 模块 数据类型 运算符 列表 元组 字典 字符串的常用操作
声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/ 模块初始: Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相 ...
- python入门基础知识三(列表和元组)
列表(list)的操作 1. 形式 var = ['char1','char2','char3',...] var = [value1,value2,value3,...] 2. 列表的增删改查 查: ...
- Python入门一:基本数据类型
作为一个刚入门编程的大一狗,第一次写博客,希望能对自己学的知识进行巩固和提升,也希望记录自己成长的过程. 学习Python,一是因为暑假学的c++头疼,听说Python简单,那我就试试吧,二是因为Py ...
- Python 入门(四)List和Tuple类型
创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...
随机推荐
- 新开通blog
从今天开始我有blog了,,以后要经常总结一些自己接触的东西,提升自己
- dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性
dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性 分类: Java2011-06-03 16:14 976人阅读 评论(0) 收藏 举报 xml扩展语言 今天在开发sitemap地 ...
- Java多线程处理任务(摘抄)
很多时候,我们需要对一个庞大的队列或者二维数组进行处理.这些处理可能是循环的,比如给一个excel多个sheet的联系人列表发邮件.很幼稚的方法就是用一个或者两个FOR循环搞定,对于庞大的数据有得让你 ...
- iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码
iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...
- F5 BIG-IP LTM负载均衡策略
standard 模式 可以很好的 防止 DDOS攻击 轮询设置是在POOL 中设置 按比率设置是在Node节点中设置
- [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 ...
- 吴裕雄--天生自然 HADOOP大数据分布式处理:使用XShell远程连接主机与服务器并配置它们之间SSH免密登录
- Block to|wreck|Range|Reach|span|chase around|amuse|exploit |instructed
English note: Block to 纷涌而至 destroy多指彻底地.毁灭性地破坏,含导致无用,不能或很难再修复的意味. wreck侧重指船只.车辆.房屋等受到严重破坏或完全毁坏,也可指计 ...
- MAYA安装未完成,某些产品无法安装的解决方法
MAYA提示安装未完成,某些产品无法安装该怎样解决呢?,一些朋友在win7或者win10系统下安装MAYA失败提示MAYA安装未完成,某些产品无法安装,也有时候想重新安装MAYA的时候会出现本电脑wi ...
- python3下应用pymysql(第一卷)
编程不会操作数据库,就像男人做做了太监,人生不完整,我不想人生不完整,写下pymysql的使用总结 先做下准备工作,准备下数据表,由于是练习操作,所以先做个简单的数据表: 创建单独的一个库:再创建表 ...