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
結論
本文演示瞭如何使用四個不同的示例建立元組。我們還學習瞭如何使用索引訪問元組元素。我們學習瞭如何使用負索引從末尾訪問元組元素。我們演示了元組如何在更新或刪除元素時丟擲錯誤。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP