
- 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 - Socket 程式設計
- 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 - Monkey Patching
- 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 是一種基於直譯器的語言。在 Linux 系統中,Python 的可執行檔案安裝在 /usr/bin/ 目錄下。對於 Windows,可執行檔案 (python.exe) 位於安裝資料夾中(例如 C:\python311)。
本教程將教你Python 直譯器如何在互動模式和指令碼模式下工作。Python 程式碼一次執行一條語句。Python 直譯器有兩個元件。翻譯器檢查語句的語法。如果正確,它會生成中間位元組碼。然後有一個 Python 虛擬機器將位元組碼轉換為原生二進位制程式碼並執行它。下圖說明了該機制。

Python 直譯器具有互動模式和指令碼模式。
Python 直譯器 - 互動模式
從命令列終端啟動 Python 時,如果沒有附加任何選項,則會出現 Python 提示符 >>>,並且 Python 直譯器的工作原理是REPL(讀取、評估、列印、迴圈)。在 Python 提示符前面輸入的每個命令都會被讀取、翻譯和執行。一個典型的互動式會話如下所示。
>>> price = 100 >>> qty = 5 >>> total = price*qty >>> total 500 >>> print ("Total = ", total) Total = 500
要關閉互動式會話,請輸入行尾字元(Linux 為 ctrl+D,Windows 為 ctrl+Z)。您也可以在 Python 提示符前面鍵入quit() 並按 Enter 鍵返回到作業系統提示符。
>>> quit() $
標準 Python 發行版提供的互動式 shell 不具備行編輯、歷史搜尋、自動完成等功能。您可以使用其他高階互動式直譯器軟體,例如IPython 和bpython,以獲得更多功能。
Python 直譯器 - 指令碼模式
與在互動式環境中一次輸入並獲取一條指令的結果不同,可以將一組指令儲存在文字檔案中,確保其具有.py副檔名,並將檔名用作 Python 命令的命令列引數。
使用任何文字編輯器(例如 Linux 上的 vim 或 Windows 上的記事本)將以下幾行儲存為prog.py。
print ("My first program") price = 100 qty = 5 total = price*qty print ("Total = ", total)
當我們在Windows機器上執行上述程式時,將產生以下結果
C:\Users\Acer>python prog.py My first program Total = 500
請注意,儘管Python一次性執行整個指令碼,但在內部它仍然是逐行執行的。
對於Java之類的基於編譯器的語言,只有在整個程式碼沒有錯誤的情況下,原始碼才會轉換為位元組碼。而在Python中,語句會一直執行到遇到第一個錯誤。
讓我們在上述程式碼中故意引入一個錯誤。
print ("My first program") price = 100 qty = 5 total = prive*qty #Error in this statement print ("Total = ", total)
請注意變數名prive拼寫錯誤,應為price。嘗試像以前一樣再次執行指令碼:
C:\Users\Acer>python prog.py My first program Traceback (most recent call last): File "C:\Python311\prog.py", line 4, in <module> total = prive*qty ^^^^^ NameError: name 'prive' is not defined. Did you mean: 'price'?
請注意,錯誤語句之前的語句已執行,然後出現錯誤訊息。因此,現在可以清楚地看到Python指令碼是以解釋方式執行的。
Python直譯器 - 使用Shebang #!
除了像上面那樣執行Python指令碼外,該指令碼本身也可以像shell指令碼一樣在Linux中自執行。您必須在指令碼頂部新增一行shebang。Shebang指示哪個可執行檔案用於解釋指令碼中的Python語句。指令碼的第一行以#!開頭,後跟Python可執行檔案的路徑。
修改prog.py指令碼如下:
#! /usr/bin/python3.11 print ("My first program") price = 100 qty = 5 total = price*qty print ("Total = ", total)
要將指令碼標記為自執行,請使用chmod命令
$ chmod +x prog.py
現在,您可以直接執行指令碼,而無需將其用作命令列引數。
$ ./hello.py
互動式Python - IPython
IPython(代表Interactive Python)是Python的一個增強型且功能強大的互動式環境,與標準Python shell相比具有許多功能。IPython最初由Fernando Perez於2001年開發。
IPython具有以下重要特性:
IPython 的物件自省能力,可以在執行時檢查物件的屬性。
其語法高亮顯示功能有助於識別語言元素,例如關鍵字、變數等。
互動歷史會被內部儲存並可以重現。
關鍵字、變數和函式名的Tab自動補全是最重要的特性之一。
IPython的魔法命令系統可用於控制Python環境和執行作業系統任務。
它是Jupyter Notebook和其他Jupyter專案前端工具的主要核心。
使用PIP安裝程式實用程式安裝IPython。
pip3 install ipython
從命令列啟動IPython
C:\Users\Acer>ipython Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32 Type 'copyright', 'credits' or 'license' for more information IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help. In [1]:
與標準直譯器中的常規>>>提示符不同,您會注意到兩個主要的IPython提示符,如下所述:
In[1]出現在任何輸入表示式之前。
Out[1]出現在輸出之前。
In [1]: price = 100 In [2]: quantity = 5 In [3]: total = price*quantity In [4]: total Out[4]: 500 In [5]:
Tab自動補全是由IPython提供的最有用的增強功能之一。當您在物件前面的點後按下Tab鍵時,IPython會彈出相應的函式列表。
IPython透過在物件前面新增?來提供任何物件的資訊(自省)。它包括文件字串、函式定義和類的建構函式詳細資訊。例如,要探索上面定義的字串物件var,請在輸入提示符中輸入var?
In [5]: var = "Hello World" In [6]: var? Type: str String form: Hello World Length: 11 Docstring: str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
IPython的魔法函式非常強大。行魔法允許您在IPython內部執行DOS命令。讓我們在IPython控制檯中執行dir命令
In [8]: !dir *.exe Volume in drive F has no label. Volume Serial Number is E20D-C4B9 Directory of F:\Python311 07-02-2023 16:55 103,192 python.exe 07-02-2023 16:55 101,656 pythonw.exe 2 File(s) 204,848 bytes 0 Dir(s) 105,260,306,432 bytes free
Jupyter Notebook是一個基於Web的介面,用於Python、Julia、R和許多其他程式設計環境。對於Python,它使用IPython作為其主要核心。