[Python教學] List 清單 和 Tuple元組

2023/09/25閱讀時間約 8 分鐘

List 清單 和 Tuple元組

清單在Python裡面非常的常用,大家一定要熟練這些基礎的元素。

List 清單

raw-image

在Python中,列表(List)是一種常用的資料類型,用於儲存一組有序的元素。列表是可變的(Mutable),這意味著你可以在列表中新增、刪除和修改元素。列表使用方括號 [] 來表示,元素之間用逗號 , 分隔。以下是列表常用的操作:

創建

fruits = ['apple', 'banana', 'orange', 'grape'] 
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, 'apple', True, 3.14]

獲取

fruits = ['apple', 'banana', 'orange', 'grape']
print(fruits[0]) # 輸出:'apple'
print(fruits[2]) # 輸出:'orange'

修改

fruits = ['apple', 'banana', 'orange', 'grape']
fruits[1] = 'pear'
print(fruits) # 輸出:['apple', 'pear', 'orange', 'grape']

增加

fruits = ['apple', 'banana', 'orange', 'grape']
fruits.append('watermelon')
print(fruits) # 輸出:['apple', 'banana', 'orange', 'grape', 'watermelon']

刪除

fruits = ['apple', 'banana', 'orange', 'grape']
del fruits[1]
print(fruits) # 輸出:['apple', 'orange', 'grape']

切片

raw-image


fruits = ['apple', 'banana', 'orange', 'grape']
slice_fruits = fruits[1:3]
print(slice_fruits) # 輸出:['banana', 'orange']

長度

fruits = ['apple', 'banana', 'orange', 'grape']
length = len(fruits)
print(length) # 輸出:4

列表檢查

fruits = ['apple', 'banana', 'orange', 'grape']
print('banana' in fruits) # 輸出:True
print('watermelon' not in fruits) # 輸出:True

常用方法

raw-image

列表是一種非常有用的資料類型,它可以用來儲存各種不同類型的元素。你可以使用這些操作來創建、修改、添加、刪除和查詢列表中的元素。


Tuple 元組

raw-image

在Python中,元組(Tuple)是另一種常用的資料類型,用於儲存一組有序的元素。不同於列表,元組是不可變的(Immutable),這意味著一旦創建了元組,就無法修改其內容。元組使用圓括號 () 來表示,元素之間用逗號 , 分隔。以下是元組常用的操作:

創建

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
numbers_tuple = (1, 2, 3, 4, 5)
mixed_tuple = (1, 'apple', True, 3.14)

獲取

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
print(fruits_tuple[0]) # 輸出:'apple'
print(fruits_tuple[2]) # 輸出:'orange'

不可改變

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
# 以下操作會引發 TypeError: 'tuple' object does not support item assignment
fruits_tuple[1] = 'pear'
del fruits_tuple[2]

元組解包

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
fruit1, fruit2, fruit3, fruit4 = fruits_tuple
print(fruit1) # 輸出:'apple'
print(fruit2) # 輸出:'banana'

元組長度

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
length = len(fruits_tuple)
print(length) # 輸出:4

元組檢查

fruits_tuple = ('apple', 'banana', 'orange', 'grape')
print('banana' in fruits_tuple) # 輸出:True
print('watermelon' not in fruits_tuple) # 輸出:True

常用方法

raw-image

元組的不可變性使其在某些情況下比列表更適合使用,特別是在希望保證資料的不可變性時。你可以使用這些操作來創建、查詢元組中的元素以及進行元組解包。

List 和 Tuple 比較

列表(List)和元組(Tuple)是Python中兩種常用的資料結構,它們都用於儲存一組有序的元素。然而,它們之間有幾個主要的不同點:

  1. 可變性:
    • 列表是可變的資料類型,這意味著你可以在創建後修改列表的內容,添加、刪除或修改元素。
    • 元組是不可變的資料類型,一旦創建後就不能修改元組的內容,元組中的元素是固定的。
  2. 創建方式:
    • 列表使用方括號 [] 來創建,元素之間用逗號 , 分隔。
    • 元組使用圓括號 () 來創建,元素之間用逗號 , 分隔。
  3. 性能:
    • 元組比列表更輕量,因為元組是不可變的,所以它們需要更少的內存空間和處理時間。
    • 列表由於是可變的,可能需要更多的內存空間和處理時間。
  4. 適用情況:
    • 使用列表當你需要在資料結構中添加、刪除或修改元素時,或者需要保持順序且元素可能重複出現。
    • 使用元組當你需要保護資料免於被意外修改,或者需要在多個函式之間傳遞不可變的資料結構。

系列文章


[Python教學]開發工具介紹

[Python經驗分享]為什麼要學Python? 我回不去了

Google Colab 介紹

[Python教學] 資料型態

[Python教學] IF判斷式

[Python教學] List 清單 和 Tuple元組

最後最後有一件小小的請求,請大家幫我填寫一下問卷,
讓我們知道你想上怎麼樣課程,感激不盡。
問卷這邊

Facebook 粉絲頁 - TechMasters 工程師養成記

程式教育 - 工程師養成記

同步分享到部落格










10會員
16內容數
最近當一個講師,把自己會的技能分享出去, 從平常工作就在使用的Python開始教學 我會陸續把資料分享上來,希望可以透過教學相長的方式,互相學習進步。 教學單元有Colab介紹與使用、ChatGPT助教、變數與資料型態、IF判斷式、List清單、For迴圈、字典與集合、函式與模組、Class類別與物件、標準函示庫與套件
留言0
查看全部
發表第一個留言支持創作者!