元組是不同數據類型的元素的不可變(不可改變)集合。這是一個有序集合,因此它保留了元素定義的順序。
元組由括號()
中的元素定義,用逗號分隔。 下面聲明一個元組類型變量。
Example: Tuple Variable Declaration
tpl=() # empty tuple
print(tpl)
names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tuple
print(names)
nums = (1, 2, 3, 4, 5) # int tuple
print(nums)
employee=(1, 'Steve', True, 25, 12000) # heterogeneous data tuple
print(employee)
Output:
()
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
但是,不必將元組元素括在括號中。元組對象可以包括由逗號分隔的元素,沒有括號。
Example: Tuple Variable Declaration
names = 'Jeff', 'Bill', 'Steve', 'Yash' # string tuple
print(names)
nums = 1, 2, 3, 4, 5 # int tuple
print(nums)
employee=1, 'Steve', True, 25, 12000 # heterogeneous data tuple
print(employee)
Output:
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
除非後跟逗號,否則元組不能用單個元素聲明。
Example: Tuple Variable Declaration
names = ('Jeff') # considered as string type
print(names)
print(type(names))
names = ('Jeff',) # tuple with single element
print(names)
print(type(names))
Output:
'Jeff'
<class 'string'>
(Jeff)
<class 'tuple'>
訪問元組元素
元組中的每個元素都由方括號[]中的索引訪問。索引以零開始,以(元素數- 1)結束,如下所示。
Example: Access Tuple Elements using Indexes
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[0]) # prints 'Jeff'
print(names[1]) # prints 'Bill'
print(names[2]) # prints 'Steve'
print(names[3]) # prints 'Yash'
nums = (1, 2, 3, 4, 5)
print(nums[0]) # prints 1
print(nums[1]) # prints 2
print(nums[4]) # prints 5
Output:
Jeff
Bill
Steve
Yash
1
2
5
元組也支持負索引,與列表類型相同。第一個元素的負指數從-number of elements
開始,最後一個元素以-1 結束。
Example: Negative Indexing
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[-4]) # prints 'Jeff'
print(names[-3]) # prints 'Bill'
print(names[-2]) # prints 'Steve'
print(names[-1]) # prints 'Yash'
Output:
Jeff
Bill
Steve
Yash
如果指定索引處的元素不存在,則將引發錯誤「索引超出範圍」。
>>> names[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
元組元素可以被解包並分配給變量,如下所示。但是,變量的數量必須與元組中元素的數量相匹配;否則,將引發錯誤。
Example: Access Tuple Elements using Indexes
names = ('Jeff', 'Bill', 'Steve', 'Yash')
a, b, c, d = names # unpack tuple
print(a, b, c, d)
Output:
Jeff Bill Steve Yash
更新或刪除元組元素
元組是不可更改的。因此,一旦創建了元組,任何試圖改變其內容的操作都是不允許的。例如,試圖修改或刪除names
元組的元素將導致錯誤。
>>> names = ('Jeff', 'Bill', 'Steve', 'Yash')
>>> names[0] = 'Swati'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del names[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
但是,您可以使用del
關鍵字刪除整個元組。
>>> del names
元組類
元組的基礎類型是元組類。使用type()
功能檢查變量的類型。
Example: Tuple Variable Declaration
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print('names type: ', type(names))
nums = (1,2,3,4,5)
print('nums type: ', type(nums))
Output:
names type: <class 'tuple'>
nums type: <class 'tuple'>
tuple()
構造器用於將任何可迭代類型轉換為元組類型。
Example: Tuple Variable Declaration
tpl = tuple('Hello') # converts string to tuple
print(tpl)
tpl = tuple([1,2,3,4,5]) # converts list to tuple
print(tpl)
tpl = tuple({1,2,3,4,5}) # converts set to tuple
print(tpl)
tpl = tuple({1:"One",2:"Two"}) # converts dictionary to tuple
print(tpl)
Output:
('H','e','l','l','o')
(1,2,3,4,5)
(1,2,3,4,5)
(1,2)
元組運算
像字符串一樣,元組對象也是一個序列。因此,用於字符串的運算符也可用於元組。
操作員 | 例子 |
---|---|
+ 運算符返回包含第一個和第二個元組對象的所有元素的元組。 |
>>> t1=(1,2,3)
>>> t2=(4,5,6)
>>> t1+t2
(1, 2, 3, 4, 5, 6)
>>> t2+(7,)
(4, 5, 6, 7)
|
| ***** 運算符連接同一個元組的多個副本。 |
>>> t1=(1,2,3)
>>> t1*4
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
|
| [] 運算符返回給定索引處的項目。負索引從右側開始計算位置。 |
>>> t1=(1,2,3,4,5,6)
>>> t1[3]
4
>>> t1[-2]
5
|
| [:] 運算符返回由兩個索引操作數指定的範圍內的項目,這兩個索引操作數由:
符號分隔。
如果省略第一個操作數,範圍從零開始。 如果省略第二個操作數,範圍將上升到元組的末尾。 |
>>> t1=(1,2,3,4,5,6)
>>> t1[1:3]
(2, 3)
>>> t1[3:]
(4, 5, 6)
>>> t1[:3]
(1, 2, 3)
|
| 如果給定元組中存在某項,則運算符中的返回真。 |
>>> t1=(1,2,3,4,5,6)
>>> 5 in t1
True
>>> 10 in t1
False
|
| 如果給定元組中不存在某項,則不在運算符中的返回真。 |
>>> t1=(1,2,3,4,5,6)
>>> 4 not in t1
False
>>> 10 not in t1
True
|
原創文章,作者:PIHFQ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/329809.html