Python tuple 用法與範例

本篇 ShengYu 要介紹 python tuple 用法與範例,tuple 元組可以用來存放一組資料(collection),存放資料的個數不限,資料型別也不用一樣,
tuple 是序列就像 list 一樣,tuple 與 list 的差別是 tuple 是不可改變的(immutable),一旦建立的 tuple 物件,就無法修改 tuple 物件的內容。
tuple 的元素與元素之間是以 , 逗號作為分隔,元素放在小括號()內。
tuple 裡面存放的元素不只侷限於整數,還可以放字串、或是混和整數跟字串。

以下 Python tuple 內容將分為這幾部份,

  • Python 建立 tuple
  • Python 讀取 tuple 的元素
  • Python 更新 tuple
  • Python tuple 索引值為 -1 或 -n
  • Python tuple 切片
  • for 迴圈遍歷巡訪 tuple 裡的元素
  • Python 建立空的 tuple
  • Python 建立只有一個元素的 tuple
  • Python tuple 實際應用範例. 網路 socket 程式

Python 建立 tuple

以下為 Python 建立 tuple 元組的範例,前三種寫法都是初始 tuple 的寫法,
第四種寫法表示不限制存放元素的型別,tuple 可以放不同型別的元素,例如放一個整數跟一個字串。

Python 元組可以使用 len() 來計算元組的長度,如下範例,

1
2
3
4
5
6
7
8
9
t = (1, 2, 3)
t = 1, 2, 3
t = tuple([1, 2, 3])
# 可以存放不同資料型別
t = 101, 'Amy'

print(t)
print(type(t))
print(len(t))

輸出結果如下,

1
2
3
(101, 'Amy')
<class 'tuple'>
2

Python 讀取 tuple 的元素

這邊介紹 Python 利用 tuple 索引值 index 來取得元素,索引值從 0 開始,第一個元素的索引值為 0,第二個元素的索引值為 1,依此類推,

1
2
3
4
t = (1, 2, 3 ,4 ,5)
print(t[0])
print(t[1])
print(t[2])

輸出結果如下,

1
2
3
1
2
3

Python 更新 tuple

tuple 一旦建立後就無法修改裡面的元素,tuple 雖然是不可改變的,但是你可以結合現存的多個 tuple 去建立一個新的 tuple,如下範例,

1
2
3
4
5
6
7
8
9
t1 = (1, 2, 3)
t2 = ('apple','banana','orange')
t3 = t1 + t2

# t1[0] = 4 # 不允許修改 tuple 的元素

print(t1)
print(t2)
print(t3)

輸出結果如下,

1
2
3
(1, 2, 3)
('apple', 'banana', 'orange')
(1, 2, 3, 'apple', 'banana', 'orange')

Python tuple 索引值為 -1 或 -n

tuple 索引值 -1 是表示 tuple 的最後一個元素,索引值是 -2 表示 tuple 的倒數第二個元素,索引值是 -n 表示 tuple 的倒數第 n 個元素,依此類推,

1
2
3
4
t = (1, 2, 3 ,4 ,5)
print(t[-1])
print(t[-2])
print(t[-3])

輸出結果如下,

1
2
3
5
4
3

Python tuple 切片

這邊介紹 Python tuple 使用切片 slicing 來取得指定範圍內元素,範例如下,

1
2
3
4
5
6
t = (1, 2, 3 ,4 ,5)
print(t[0:3]) # tuple 索引值0到索引值3
print(t[2:7]) # tuple 索引值2到索引值7
print(t[2:]) # tuple 索引值2到最後
print(t[:4]) # tuple 前4個元素
print(t[:]) # tuple 所有元素

輸出結果如下,

1
2
3
4
5
(1, 2, 3)
(3, 4, 5)
(3, 4, 5)
(1, 2, 3, 4)
(1, 2, 3, 4, 5)

更多 tuple 切片 slicing 的教學範例請看這篇

for 迴圈遍歷巡訪 tuple 裡的元素

這邊示範用 for 迴圈來遍歷巡訪 tuple 裡的所有元素並印出來,範例如下,

1
2
3
t = ('apple','banana','orange','lemon','tomato')
for i in t:
print(i)

輸出結果如下,

1
2
3
4
5
apple
banana
orange
lemon
tomato

更多 for 迴圈用法請看這篇

Python 建立空的 tuple

Python 建立空的 tuple 範例如下,

1
2
3
4
t = tuple()
print(t)
print(type(t))
print(len(t))

輸出結果如下,

1
2
3
()
<class 'tuple'>
0

Python 建立只有一個元素的 tuple

Python 要建立只有一個元素的 tuple 需要在該元素後加上一個逗號(comma),否則不會被辨識為 tuple,如下範例,

1
2
3
t = ('hello', )
print(type(t))
print(len(t))

輸出結果如下,

1
2
<class 'tuple'>
1

Python tuple 實際應用範例. 網路 socket 程式

在寫網路 socket 程式時,socket.bind()socket.connect() 的參數就是由 host 與 port 所組成的 tuple,

1
2
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 80))

下一篇將介紹 range 的用法

以上就是 Python tuple 用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其他參考
https://www.tutorialspoint.com/python/python_tuples.htm
https://openhome.cc/Gossip/Python/TupleType.html
https://www.w3schools.com/python/python_tuples.asp
https://sites.google.com/site/ezpythoncolorcourse/tuple

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python 元組切片
Python 檢查 tuple 元組是否為空
Python 新手入門教學懶人包
Python str 字串用法與範例
Python list 串列用法與範例
Python set 集合用法與範例
Python dict 字典用法與範例