6、Python-元组
定义
# Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。
aTuple = ('et',77,99.9)
print(aTuple)
元组的操作
aTuple = ('et',77,99.9)
# 访问元组
print(aTuple[0])
# et
# 修改元组
aTuple[0] = 11
# 'tuple' object does not support item assignment
a = ('a', 'b', 'c', 'a', 'b')
# 左闭右开区间 (b,c),找不到会报错
a.index('a', 1, 3)
print(a.index('a', 1, 4))
#
print(a.count('b'))
#
运算符
+
# 字符串
print("hello " + "xxx")
# hello xxx # 列表
print([1, 2] + [3, 4])
# [1, 2, 3, 4] # 元组
print(('a', 'b') + ('c', 'd'))
# ('a', 'b', 'c', 'd')
*
# 字符串
print('ab' * 4)
# 'ababab' # 列表
print([1, 2] * 4)
# [1, 2, 1, 2, 1, 2, 1, 2] # 元组
print(('a', 'b') * 4)
# ('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')
in & not in
# 字符串
print('itc' in 'hello itcast')
# True # 列表
print(3 in [1, 2])
# False # 元组
print(4 in (1, 2, 3, 4))
# True # 字典
print("name" in {"name": "Delron", "age": 24})
# True
6、Python-元组的更多相关文章
- Python元组
Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 = ('physi ...
- Python 元组内置函数
Python元组包含了以下内置函数 序号 方法及描述 1 cmp(tuple1, tuple2)比较两个元组元素. 2 len(tuple)计算元组元素个数. 3 max(tuple)返回元组中元素最 ...
- Python元组与字典详解
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup ...
- Python 元组 count() 方法
描述 Python 元组 count() 方法用于统计某个元素在元祖中出现的次数. 语法 count() 方法语法: T.count(obj) 参数 obj -- 元祖中统计的对象. 返回值 返回元素 ...
- Python 元组 index() 方法
描述 Python 元组 index() 方法用于从元祖中找出某个对象第一个匹配项的索引位置,如果这个对象不在元祖中会报一个异常. 语法 index() 方法语法: T.index(obj[,star ...
- Python 元组 tuple() 方法
描述 Python 元组 tuple() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为元组. 语法 tuple() 方法语法: tuple(iterable) 参数 iterable -- ...
- Python 元组 min() 方法
描述 Python 元组 min() 方法返回元组中元素最小值. 语法 min() 方法语法: min(T) 参数 T -- 指定的元组. 返回值 返回元组中元素最小值. 实例 以下实例展示了 min ...
- Python 元组 max() 方法
描述 Python 元组 max() 方法返回元组中元素最大值. 语法 max() 方法语法: max(T) 参数 T -- 指定的元组. 返回值 返回元组中元素最大值. 实例 以下实例展示了 max ...
- Python 学习笔记(九)Python元组和字典(一)
Python 元组 元组的定义 元组(tuple)是一种Python对象类型,元组也是一种序列 Python中的元组与列表类似,不同之处元组的元素不能修改 元组使用小括号,列表使用方括号 元组的创建 ...
- python 元组拾遗
python 元组拾遗 python 语法糖 def printall(*args): return args 一个以 * 开头的参数将参数聚集为一个元组. printall函数可以接收任意个数的 ...
随机推荐
- R和python语言如何求平均值,中位数和众数
均值是通过取数值的总和并除以数据序列中的值的数量来计算. R语言平均值公式: mean(x, trim = 0, na.rm = FALSE, ...)#x - 是输入向量.trim - 用于从排序的 ...
- 4-Python3从入门到实战—基础之数据类型(字符串-String)
Python从入门到实战系列--目录 字符串表示 在 Python 3版本中,字符串是以 Unicode 编码的:Python 中使用 ' '或者" "表示字符串 msg = 'H ...
- Spring Framework: @RestController vs @Controller
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annota ...
- Selenium Grid的Java调用方法
java -jar selenium-server-standalone-.jar -role hub explorer http://192.168.1.173:4444/grid/console ...
- From 简书 转帖一下如何安装k8s1.10 改天做下实验. https://www.jianshu.com/p/9c7e1c957752
centos7.3 kubernetes/k8s 1.10 离线安装 老菜_misa 关注 2018.04.25 23:57 字数 1243 阅读 266评论 1喜欢 3 本文介绍在centos7.3 ...
- vue-cli webpack项目npm run dev启动过程
前言 通过vue init webpack和npm install命令初始化项目后,执行npm run dev就打开了网站http://localhost:8080.初学者不知道index.html. ...
- Java微信二次开发(五)
消息加密 需要到入库:commons-io-2.4.jar,commons-codec-1.9.jar(在官网的Java微信加密demo下) 第一步:访问https://mp.weixin.qq.co ...
- delphi简单单向字符串加密函数
delphi用久了有的时候得给密码加密一下,简单点就行了,这个函数还是不错的. const XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$8 ...
- unwrap bug
https://cn.mathworks.com/matlabcentral/newsreader/view_thread/93276
- codeforces146A
Lucky Ticket CodeForces - 146A Petya loves lucky numbers very much. Everybody knows that lucky numbe ...