小白教程
所有教程
关于
Search
172.69.7.132
172.69.7.132
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 元组”的源代码
本页内容
上一节:
Python3_列表
下一节:
Python3_字典
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Python3 元组}}[[Category:Python 教程|15]] = Python3 元组 = Python 的元组与列表类似,不同之处在于元组的元素不能修改。 元组使用小括号 ( ),列表使用方括号 [ ]。 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。 == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> >>> tup1 = ('Google', 'xiaoBai', 1997, 2000) >>> tup2 = (1, 2, 3, 4, 5 ) >>> tup3 = "a", "b", "c", "d" # 不需要括号也可以 >>> type(tup3) <class 'tuple'> </sample> 创建空元组 <sample title="" desc="" lang="python" hererun="1"> tup1 = () </sample> 元组中只包含一个元素时,需要在元素后面添加逗号 , ,否则括号会被当作运算符使用: == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> >>> tup1 = (50) >>> type(tup1) # 不加逗号,类型为整型 <class 'int'> >>> tup1 = (50,) >>> type(tup1) # 加上逗号,类型为元组 <class 'tuple'> </sample> 元组与字符串类似,下标索引从 0 开始,可以进行截取,组合等。 == 访问元组 == 元组可以使用下标索引来访问元组中的值,如下实例: == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 tup1 = ('Google', 'xiaoBai', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) </sample> 以上实例输出结果: <sample title="" desc="" lang="python" hererun="1"> tup1[0]: Google tup2[1:5]: (2, 3, 4, 5) </sample> == 修改元组 == 元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例: == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的。 # tup1[0] = 100 # 创建一个新的元组 tup3 = tup1 + tup2 print (tup3) </sample> 以上实例输出结果: <sample title="" desc="" lang="python" hererun="1"> (12, 34.56, 'abc', 'xyz') </sample> == 删除元组 == 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例: == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 tup = ('Google', 'xiaoBai', 1997, 2000) print (tup) del tup print ("删除后的元组 tup : ") print (tup) </sample> 以上实例元组被删除后,输出变量会有异常信息,输出如下所示: <sample title="" desc="" lang="python" hererun="1"> 删除后的元组 tup : Traceback (most recent call last): File "test.py", line 8, in <module> print (tup) NameError: name 'tup' is not defined </sample> == 元组运算符 == 与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。 {| class="table table-striped table-hover" !width="33%"| Python 表达式 !width="33%"| 结果 !width="33%"| 描述 |- | <sample title="" desc="" lang="python" hererun="1"> len((1, 2, 3)) </sample> | 3 | 计算元素个数 |- | <sample title="" desc="" lang="python" hererun="1"> (1, 2, 3) + (4, 5, 6) </sample> | (1, 2, 3, 4, 5, 6) | 连接 |- | <sample title="" desc="" lang="python" hererun="1"> ('Hi!',) * 4 </sample> | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |- | <sample title="" desc="" lang="python" hererun="1"> 3 in (1, 2, 3) </sample> | True | 元素是否存在 |- | <sample title="" desc="" lang="python" hererun="1"> for x in (1, 2, 3): print (x, end=" ") </sample> | 1 2 3 | 迭代 |} == 元组索引,截取 == 因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示: 元组: <sample title="" desc="" lang="python" hererun="1"> tup = ('Google', 'xiaoBai', 'Taobao', 'Wiki', 'Weibo','Weixin') </sample> {| class="table table-striped table-hover" ! Python 表达式 ! 结果 ! 描述 |- | tup[1] | 'xiaoBai' | 读取第二个元素 |- | tup[-2] | 'Weibo' | 反向读取,读取倒数第二个元素 |- | tup[1:] | ('xiaoBai', 'Taobao', 'Wiki', 'Weibo', 'Weixin') | 截取元素,从第二个开始后的所有元素。 |- | tup[1:4] | ('xiaoBai', 'Taobao', 'Wiki') | 截取元素,从第二个开始到第四个元素(索引为 3)。 |} 运行实例如下: <sample title="" desc="" lang="python" hererun="1"> >>> tup = ('Google', 'xiaoBai', 'Taobao', 'Wiki', 'Weibo','Weixin') >>> tup[1] 'xiaoBai' >>> tup[-2] 'Weibo' >>> tup[1:] ('xiaoBai', 'Taobao', 'Wiki', 'Weibo', 'Weixin') >>> tup[1:4] ('xiaoBai', 'Taobao', 'Wiki') >>> </sample> == 元组内置函数 == Python元组包含了以下内置函数 {| class="table table-striped table-hover" !width="33%"| 序号 !width="33%"| 方法及描述 !width="33%"| 实例 |- | 1 | len(tuple)计算元组元素个数。 | <sample title="" desc="" lang="python" hererun="1"> >>> tuple1 = ('Google', 'xiaoBai', 'Taobao') >>> len(tuple1) 3 >>> </sample> |- | 2 | max(tuple)返回元组中元素最大值。 | <sample title="" desc="" lang="python" hererun="1"> >>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>> </sample> |- | 3 | min(tuple)返回元组中元素最小值。 | <sample title="" desc="" lang="python" hererun="1"> >>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>> </sample> |- | 4 | tuple(iterable)将可迭代系列转换为元组。 | <sample title="" desc="" lang="python" hererun="1"> >>> list1= ['Google', 'Taobao', 'xiaoBai', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'xiaoBai', 'Baidu') </sample> |} === 关于元组是不可变的 === 所谓元组的不可变指的是元组所指向的内存中的内容不可变。 <sample title="" desc="" lang="python" hererun="1"> >>> tup = ('r', 'u', 'n', 'o', 'o', 'b') >>> tup[0] = 'g' # 不支持修改元素 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> id(tup) # 查看内存地址 4440687904 >>> tup = (1,2,3) >>> id(tup) 4441088800 # 内存地址不一样了 </sample> 从以上实例可以看出,重新赋值的元组 tup,绑定到新的对象了,不是修改了原来的对象。
返回至“
Python3 元组
”。
上一节:
Python3_列表
下一节:
Python3_字典