目录
5.1 元组介绍
- 元组与列表类似,不同之处在于元组的元素不能修改。
- 列表使用的是中括号“[]”,元组使用的是小括号“()”。
- 列表属于可变类型,元组属于不可变类型。
- Python内部对元组进行了大量的优化,访问喝处理速度都比列表快。
- 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
- 当函数返回了多个值时,Python内部会把多个值放在一个元组中返回。
下面以增删改查来介绍元组
5.2 创建元组
创建空元组
tuple = ()
创建元组的方式
tuple1 = tuple((1,2,3)) #创建了一个包括3个元素的元组对象
print(tuple1)
tuple2 = (1,2,3,4,5,6,7,8,9,10)
print(tuple2)
元组中只包含一个元素时,需要在元素后面添加逗号 , ,否则括号会被当作运算符使用:
tuple1= (1, ) #创建一个元素的元组时,必须带有逗号
tuple2= (1)
print(tuple1[0])
#print(tuple2[0]) 无法正确打印元组中第一个元素
#error: TypeError: 'int' object is not subscriptable
5.3 删除元组
元组不可更改,因此您无法从中删除元素,但可以完全删除元组:
tup = ('Google', 'Runoob', 1997, 2000)
print (tup)
del tup
print ("删除后的元组 tup : ")
print (tup)
'''
删除后的元组 tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name 'tup' is not defined
'''
以上实例元组被删除后,输出变量会有异常信息
5.4 更新元组
不能通过下标之间更改元组里面的值,元组属于不可变的数据类型,否则会报错。
tuple2 = (1,2,3,4,5,6,7,8,9,10)
tuple2[0] = 100
#error: TypeError: 'tuple' object does not support item assignment
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# 以下修改元组元素操作是非法的。
# tup1[0] = 100
# 创建一个新的元组
tup3 = tup1 + tup2
print (tup3)
# (12, 34.56, 'abc', 'xyz')
5.5 查看元组
元组的访问和列表的访问类似。可以访问打印整个元组中的元素,也可以使用下标索引来访问元组中的值。
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
# tup1[0]: Google
# tup2[1:5]: (2, 3, 4, 5)
5.6 数据类型
查看某一变量的数据类型,元组的数据类型就是元组(tuple)
tuple1 = tuple((1,2,3)) #创建了一个包括3个元素的元组对象
print("元组的数据类型是:",type(tuple1))
print("将元组转换成列表:",list(tuple1))
#打印结果:
#元组的数据类型是: <class 'tuple'>
# 将元组转换成列表: [1, 2, 3]
可将元组转为列表,就可以使用列表的内置函数了
5.7 元组运算
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python 表达式 | 结果 | 描述 |
---|---|---|
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print (x, end=" ") | 1 2 3 | 迭代 |
5.8 元组遍历
在Python中,遍历元组的方法有很多种,下面介绍一些常用的遍历方法:
- 使用推导式进行遍历
tuple1 = (value for value in range(1,11)) #返回一个迭代的对象
#访问
print(tuple1)
for i in tuple1:
print(i)
- 使用for循环遍历元组
print("Python设计理念:")
python = ("优雅","明确","简单")
for linian in python:
print(linian)
- 使用for循环和enumerate()函数遍历元组
print("Python设计理念:")
python = ("优雅","明确","简单")
for index,linian in enumerate(python):
print(index,linian)
- 使用for循环和tuple()函数遍历元组
print("Python设计理念:")
python = ("优雅","明确","简单")
for linian in tuple(python):
print(linian)
- 使用for循环和range()函数遍历元组
number = (1122,2366,4400,5577,8888)
for i in range(len(number)):
print(i,number[i])
number = (1122,2366,4400,5577,8888)
for i in range(len(number)):
print(number[i])
- 使用for循环和iter()函数遍历元组
print("Python设计理念:")
python = ("优雅","明确","简单")
for linian in iter(python):
print(linian)
- 使用while循环遍历元组
number = (1122,2366,4400,5577,8888)
i = 0
while i < len(number):
print(number[i])
i = i + 1
5.9 不可变性
所谓元组的不可变指的是元组所指向的内存中的内容不可变。
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 # 内存地址不一样了
5.10 内置函数
Python元组包含了以下内置函数
序号 | 方法及描述 | 实例 |
---|---|---|
1 | len(tuple) 计算元组元素个数。 | >>> tuple1 = ('Google', 'Runoob', 'Taobao') >>> len(tuple1) 3 >>> |
2 | max(tuple) 返回元组中元素最大值。 | >>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>> |
3 | min(tuple) 返回元组中元素最小值。 | >>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>> |
4 | tuple(iterable) 将可迭代系列转换为元组。 | >>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'Runoob', 'Baidu') |