
- Python 基礎
- Python - 首頁
- Python - 概覽
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式 & 模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類 & 物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤 & 異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 執行緒連線
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期 & 時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 不可變資料結構
Python 的不可變資料結構是指一旦建立後就無法更改的資料結構。這意味著任何嘗試修改資料結構的操作都將導致建立一個新的例項,而不是修改原始例項。不可變資料結構對於確保資料在程式執行過程中保持不變非常有用,這有助於防止錯誤並使程式碼更易於理解和維護。
在深入探討這個主題之前,讓我們快速回顧一下什麼是資料結構?資料結構是用於組織、處理、檢索和儲存資料的特殊格式。它們定義了資料在記憶體中的排列方式以及如何有效地執行諸如訪問、插入、刪除和更新等操作。
Python 中不同的不可變資料結構
不可變資料結構在 Python 中因其穩定性、執行緒安全性以及易用性而至關重要。以下是 Python 中不同的不可變資料結構:
- 元組:這些是有序的專案集合,建立後無法更改。它們可以包含混合資料型別,並且對於表示固定相關的專案集合很有用。
- 字串:這些資料結構是字元序列,並且是不可變的。任何修改字串的操作都將建立一個新的字串。
- 凍結集:這些是集合的不可變版本。與普通集合不同,凍結集在建立後不允許修改。
- 命名元組:這些是元組的子類,具有命名欄位,可以提供更具可讀性和自文件化的程式碼。它們像普通元組一樣是不可變的。
現在,讓我們詳細介紹每個不可變資料結構。
元組
Python 中的元組是不可變的元素序列,這意味著一旦建立,它們就無法修改。它們使用括號 '()' 定義,並且可以儲存各種專案,例如數字、字串甚至其他元組。
建立元組
元組使用括號 '()' 建立,元素之間用逗號 ',' 分隔。即使只有一個元素的元組也需要尾隨逗號來將其與分組表示式區分開來。
以下是透過將括號 '()' 賦值給變數來建立元組的示例:
empty_tuple = () single_element_tuple = (5,) # Note the comma after the single element print("Single element tuple:", single_element_tuple) multi_element_tuple = (1, 2, 'Tutorialspoint', 3.14) print("Multi elements tuple:", multi_element_tuple) nested_tuple = (1, (2, 3), 'Learning') print("Nested tuple:", nested_tuple)
執行上述程式碼後,我們將得到以下輸出:
Single element tuple: (5,) Multi elements tuple: (1, 2, 'Tutorialspoint', 3.14) Nested tuple: (1, (2, 3), 'Learning')
理解 Python 中元組的不可變性
在這裡,我們將瞭解 Python 中元組的不可變性。以下是一個示例:
# Define a tuple my_tuple = (1, 2, 3, 'hello') # Attempt to modify an element (which is not possible with tuples) # This will raise a TypeError try: my_tuple[0] = 10 except TypeError as e: print(f"Error: {e}") # Even trying to append or extend a tuple will result in an error try: my_tuple.append(4) except AttributeError as e: print(f"Error: {e}") # Trying to reassign the entire tuple to a new value is also not allowed try: my_tuple = (4, 5, 6) except TypeError as e: print(f"Error: {e}") print("Original tuple:", my_tuple)
執行上述程式碼後,我們將得到以下輸出:
Error: 'tuple' object does not support item assignment Error: 'tuple' object has no attribute 'append' Original tuple: (4, 5, 6)
字串
Python 中的字串是字元序列,用於表示和操作文字資料。它們用單引號'或雙引號"括起來,也可以使用三引號"""來表示多行字串。
主要特徵包括不可變性,即建立後字串無法更改;有序索引,可以透過位置訪問字元;以及支援各種操作,例如連線、切片和迭代。
字串在 Python 中是基礎資料型別,用於文字處理、輸入/輸出操作以及應用程式中的資料表示,提供了一套功能強大的工具集,以及用於高效操作和格式化文字資訊的內建方法。
建立字串
每種字串建立方法(即 ', ", """)都有其自身的用例,具體取決於我們是否需要在字串中包含引號、處理多行文字或在 Python 程式碼中滿足其他特定格式要求。
以下是使用三種引號 ', ", """ 建立字串的示例:
# Single line string single_quoted_string = 'Hello, Welcome to Tutorialspoint' # Double quoted string double_quoted_string = "Python Programming" # Triple quoted string for multi-line strings multi_line_string = """This is a multi-line string""" print(single_quoted_string) print(double_quoted_string) print(multi_line_string)
執行上述程式碼後,我們將得到以下輸出:
Hello, Welcome to Tutorialspoint Python Programming This is a multi-line string
理解 Python 中的字串不可變性
透過以下示例,我們將瞭解 Python 中字串的不可變性。
# Example demonstrating string immutability my_string = "Hello" # Attempting to modify a string will create a new string instead of modifying the original modified_string = my_string + " Learners" print(modified_string) # Output: Hello Learners # Original string remains unchanged print(my_string) # Output: Hello # Trying to modify the string directly will raise an error try: my_string[0] = 'h' # TypeError: 'str' object does not support item assignment except TypeError as e: print(f"Error: {e}")
執行上述程式碼後,我們將得到以下輸出:
Hello Learners Hello Error: 'str' object does not support item assignment
凍結集
Python 中的凍結集是集合的不可變版本。建立後,其元素不能更改、新增或刪除。凍結集在我們需要一個在程式執行過程中保持不變的集合的情況下特別有用,尤其是在我們想要將其用作字典的鍵或另一個集合的元素時。
建立凍結集
我們可以使用frozenset()建構函式建立凍結集,並將可迭代物件(如列表或其他集合)作為引數傳遞。以下是建立凍結集的示例:
# Creating a frozen set fset = frozenset([1, 2, 3, 4]) # Printing the frozen set print(fset)
執行上述程式碼後,我們將得到以下輸出:
frozenset({1, 2, 3, 4})
理解 Python 中凍結集的不可變性
以下示例展示了凍結集的不可變性,以及建立後不允許修改。
# Creating a frozenset frozen_set = frozenset([1, 2, 3, 4]) # Attempting to add an element to the frozenset will raise an error try: frozen_set.add(5) except AttributeError as e: print(f"Error: {e}") # Attempting to remove an element from the frozenset will also raise an error try: frozen_set.remove(2) except AttributeError as e: print(f"Error: {e}") # The original frozenset remains unchanged print("Original frozenset:", frozen_set)
執行上述程式碼後,我們將得到以下輸出:
Error: 'frozenset' object has no attribute 'add' Error: 'frozenset' object has no attribute 'remove' Original frozenset: frozenset({1, 2, 3, 4})
命名元組
Python 中的命名元組是 collections 模組中提供的一種輕量級資料結構,其行為類似於元組,但允許我們使用命名屬性以及索引來訪問其元素。
它結合了元組的優點(例如不可變、記憶體效率)以及透過名稱引用元素的能力,從而提高了程式碼的可讀性和可維護性。
建立命名元組
我們可以使用 collections 模組中的namedtuple()工廠函式定義命名元組。它接受兩個引數:命名元組型別的名稱和欄位名稱的序列(即字串或字串的可迭代物件),用於指定其欄位的名稱。
from collections import namedtuple # Define a named tuple type 'Point' with fields 'x' and 'y' Point = namedtuple('Point', ['x', 'y']) # Create an instance of Point p1 = Point(1, 2) # Access elements by index (like a tuple) print(p1[0]) # Access elements by name print(p1.x) print(p1.y)
執行上述程式碼後,我們將得到以下輸出:
1 1 2
理解 Python 中命名元組的不可變性
Python 中的命名元組由collections.namedtuple工廠函式提供,確實是不可變的。它們的行為類似於普通元組,但具有命名欄位,使其更具可讀性和自文件性。
from collections import namedtuple # Define a named tuple called Point with fields 'x' and 'y' Point = namedtuple('Point', ['x', 'y']) # Create an instance of Point p = Point(x=1, y=2) print(p) # Attempt to modify the named tuple # This will raise an AttributeError since named tuples are immutable try: p.x = 10 except AttributeError as e: print(f"Error occurred: {e}") # Accessing elements in a named tuple is similar to accessing elements in a regular tuple print(p.x) print(p.y)
執行上述程式碼後,我們將得到以下輸出:
Point(x=1, y=2) Error occurred: can't set attribute 1 2