List 串列
List 是 Python 最常用的資料結構,有序且可變。
建立串列
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14]
empty = []
存取元素
fruits = ["蘋果", "香蕉", "橘子"]
print(fruits[0]) # 蘋果
print(fruits[-1]) # 橘子
常用方法
lst = [1, 2, 3]
lst.append(4) # [1, 2, 3, 4]
lst.insert(0, 0) # [0, 1, 2, 3, 4]
lst.remove(2) # [0, 1, 3, 4]
lst.pop() # 移除並回傳最後一個
練習
建立一個串列並排序
💻 程式碼編輯器
📤 執行結果
等待執行...
登入後可追蹤進度
下一課 →