Python中元组的声明语法是什么
Python提供了各种数据结构集合,如列表、元组、集合和字典。然而,这些元组与列表非常相似。因为列表是一种常用的数据结构,开发人员经常误解元组与列表的区别。
Python元组和列表一样,是由任何数据类型的项目组成的集合,但元组是不可变的,这意味着一旦被分配,我们不能改变元组的元素或元组本身,而列表的元素是可变的。
在本文中,我们将向您解释python元组是什么以及对它的各种操作。
创建元组
只有在分配时才能创建元组,所以将所有项目放在括号中,用逗号分隔,将创建一个元组。
示例1
# tuple consisting of strings
demoTuple_1 = ("TutorialsPoint", "python", "codes")
print("demoTuple_1:", demoTuple_1)
输出
执行上述程序后将生成如下输出——
demoTuple_1: ('TutorialsPoint', 'python', 'codes')
示例2
# tuple consisting of integer, float number, string
demoTuple_2 = (25, 6.8, "TutorialsPoint")
print("demoTuple_2:", demoTuple_2)
输出
在执行时,上述程序将生成以下输出-
demoTuple_2: (25, 6.8, 'TutorialsPoint')
示例3
# tuple consisting of string & list
demoTuple_3 = ("Welcome", [5, 8, 11])
print("demoTuple_3:", demoTuple_3)
输出
demoTuple_3: ('Welcome', [5, 8, 11])
示例4
# nested tuple(one tuple inside another tuple)
demoTuple_4 = ((100, 80, 60), (2, "TutorialsPoint"))
print("demoTuple_4:", demoTuple_4)
输出
demoTuple_4: ((100, 80, 60), (2, 'TutorialsPoint'))
访问元组元素
要访问元组中的元素,我们需要使用索引。
我们还可以在元组中使用负索引。由于索引从0开始,我们使用0来访问元组的第一个元素,1来访问第二个元素,依此类推。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的元组。
-
打印输入的元组。
-
使用 正索引 访问输入元组的第一个元素(元组索引始终从0开始)。
-
使用正索引,访问输入元组的第四个元素(inputTuple[3])。
代码
# tuple consisting of strings
inputTuple = ("TutorialsPoint", "python", "sample", "codes")
# printing tuple
print("Input Tuple: ", inputTuple)
# accessing the first element of the tuple
print("first element of tuple: ", inputTuple[0])
# accessing the fourth element of the tuple
print("fourth element of tuple: ", inputTuple[3])
输出
Input Tuple: ('TutorialsPoint', 'python', 'sample', 'codes')
first element of tuple: TutorialsPoint
fourth element of tuple: codes
使用负索引访问元组元素
与列表和字符串索引类似,负索引可以用于从末尾访问元组元素。
-1 表示最后一个元素,-2 表示倒数第二个元素,依此类推。
步骤
以下是执行所需任务的算法/步骤:
- 创建变量来存储输入的元组。
-
打印输入的元组。
-
使用负索引来访问输入元组的最后一个元素(负元组索引始终从 -1 开始)。
-
使用负索引来访问输入元组的倒数第二个元素(inputTuple[-1])。
代码
# tuple consisting of strings
inputTuple = ("TutorialsPoint", "python", "sample", "codes")
# printing tuple
print("Input Tuple: ", inputTuple)
# accessing the last element of the tuple
print("last element of tuple: ", inputTuple[-1])
# accessing the last second element of the tuple
print("last second element of tuple: ", inputTuple[-2])
输出
Input Tuple: ('TutorialsPoint', 'python', 'sample', 'codes')
last element of tuple: codes
last second element of tuple: sample
更新/删除元组元素
元组是不可变的即元组的项不能被改变。因此,一旦一个元组被创建,任何试图改变其项的操作都是受限的。
下面的程序试图更新/修改输入元组的元素,但由于元组是不可变的,所以会产生错误−
# tuple consisting of strings
inputTuple = ("TutorialsPoint", "python", "sample", "codes")
# changing the 1st index element of the input tuple(python) to 'Java'
# It raises a TypeError since the tuple is immutable(tuple items cannot be changed)
inputTuple[1] = 'Java'
输出
TypeError Traceback (most recent call last)
<ipython-input-6-92f3425fb83d> in <module>
4 # changing the 1st index element of the input tuple(python) to 'Java'
5 # It raises a TypeError since the tuple is immutable(tuple items cannot be changed)
----> 6 inputTuple[1] = 'Java'
TypeError: 'tuple' object does not support item assignment
以下程序试图删除输入元组的元素,但由于元组是不可变的,所以引发了一个错误 –
以下程序试图 删除 输入元组的元素,但由于元组是不可变的,因此引发了一个错误−
# tuple consisting of strings
inputTuple = ("TutorialsPoint", "python", "sample", "codes")
# deleting the 1st index element of the input tuple(python)
# It raises a TypeError since the tuple is immutable(tuple items cannot be changed)
del inputTuple[1]
输出
TypeError Traceback (most recent call last)
<ipython-input-7-924b49eaac45> in <module>
4 # deleting the 1st index element of the input tuple(python)
5 # It raises a TypeError since the tuple is immutable(tuple items cannot be changed)
----> 6 del inputTuple[1]
TypeError: 'tuple' object doesn't support item deletion
结论
本文展示了如何使用四个不同的示例创建元组。我们还学会了如何使用索引来访问元组的元素。我们学习了如何使用负索引来从末尾访问元组的元素。我们还演示了当更新或删除元素时,元组会抛出错误。