
- 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 程式語言的語法與 Perl、C 和 Java 程式語言有很多相似之處。但是,這些語言之間也存在一些明顯的差異。
第一個 Python 程式
讓我們執行一個 Python 程式來列印“Hello, World!”,使用 Python 程式設計的兩種不同模式。(a) 互動模式程式設計 (b) 指令碼模式程式設計。
Python - 互動模式程式設計
我們可以透過在命令提示符下鍵入python來呼叫 Python 直譯器,如下所示:
$ python3 Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
這裡>>>表示 Python 命令提示符,您可以在其中鍵入命令。讓我們在 Python 提示符下鍵入以下文字並按 Enter 鍵:
>>> print ("Hello, World!")
如果您正在執行舊版本的 Python,例如 Python 2.4.x,則需要使用不帶括號的 print 語句,如print "Hello, World!"。但是,在 Python 3.x 版本中,這會產生以下結果:
Hello, World!
Python - 指令碼模式程式設計
我們可以使用指令碼引數呼叫 Python 直譯器,該引數開始執行指令碼並持續到指令碼完成。指令碼完成後,直譯器將不再處於活動狀態。
讓我們在一個簡單的文字檔案中編寫一個簡單的 Python 程式。Python 檔案的副檔名為.py。在test.py檔案中鍵入以下原始碼:
print ("Hello, World!")
我們假設您已在 PATH 變數中 設定了 Python 直譯器的路徑。現在,讓我們嘗試如下執行此程式:
$ python3 test.py
這會產生以下結果:
Hello, World!
讓我們嘗試另一種執行 Python 指令碼的方法。以下是修改後的 test.py 檔案:
#!/usr/bin/python3 print ("Hello, World!")
我們假設您在 /usr/bin 目錄中提供了 Python 直譯器。現在,嘗試如下執行此程式:
$ chmod +x test.py # This is to make file executable $./test.py
這會產生以下結果:
Hello, World!
Python 識別符號
Python 識別符號是用於標識 變數、函式、類、模組或其他物件的名稱。識別符號以字母 A 到 Z 或 a 到 z 或下劃線 (_) 開頭,後跟零個或多個字母、下劃線和數字 (0 到 9)。
Python 不允許在識別符號中使用諸如 @、$ 和 % 之類的標點符號。
Python 是一種區分大小寫的程式語言。因此,Manpower 和manpower 在 Python 中是兩個不同的識別符號。
以下是 Python 識別符號的命名規範:
Python 類名以大寫字母開頭。所有其他識別符號都以小寫字母開頭。
識別符號以單個下劃線開頭表示該識別符號為私有識別符號。
識別符號以兩個下劃線開頭表示一個強私有識別符號。
如果識別符號也以兩個下劃線結尾,則該識別符號為語言定義的特殊名稱。
Python 保留字
以下列表顯示了 Python 關鍵字。這些是保留字,您不能將它們用作常量或變數或任何其他識別符號名稱。所有 Python 關鍵字僅包含小寫字母。
and | as | assert |
break | class | continue |
def | del | elif |
else | except | False |
finally | for | from |
global | if | import |
in | is | lambda |
None | nonlocal | not |
or | pass | raise |
return | True | try |
while | with | yield |
Python 行和縮排
Python 程式設計沒有使用大括號來指示類和函式定義或流程控制的程式碼塊。程式碼塊由行縮排表示,並且嚴格執行。
縮排的空格數是可變的,但塊中的所有語句必須縮排相同的量。例如:
if True: print ("True") else: print ("False")
但是,以下程式碼塊會生成錯誤:
if True: print ("Answer") print ("True") else: print ("Answer") print ("False")
因此,在 Python 中,所有以相同空格數連續縮排的行都將構成一個程式碼塊。以下示例包含各種語句塊:
現在不必理解邏輯。只需確保您理解了各種程式碼塊,即使它們沒有使用大括號。
import sys try: # open file stream file = open(file_name, "w") except IOError: print "There was an error writing to", file_name sys.exit() print "Enter '", file_finish, print "' When finished" while file_text != file_finish: file_text = raw_input("Enter text: ") if file_text == file_finish: # close the file file.close break file.write(file_text) file.write("\n") file.close() file_name = raw_input("Enter filename: ") if len(file_name) == 0: print "Next time please enter something" sys.exit() try: file = open(file_name, "r") except IOError: print "There was an error reading file" sys.exit() file_text = file.read() file.close() print file_text
Python 多行語句
Python 中的語句通常以換行符結束。但是,Python 允許使用行延續字元 (\) 來表示該行應該繼續。例如:
total = item_one + \ item_two + \ item_three
包含在 []、{} 或 () 括號內的語句不需要使用行延續字元。例如,以下語句在 Python 中可以正常工作:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Python 中的引號
Python 接受單引號 (')、雙引號 (") 和三引號 (''' 或 """) 來表示字串文字,只要相同型別的引號開始和結束字串即可。
三引號用於跨多行擴充套件字串。例如,以下所有內容都是合法的:
word = 'word' print (word) sentence = "This is a sentence." print (sentence) paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" print (paragraph)
Python 中的註釋
註釋是在 Python 原始碼中程式設計師可讀的解釋或註釋。新增它們的目的是使原始碼更容易讓人理解,並且 Python 直譯器會忽略它們。
與大多數現代語言一樣,Python 支援單行(或行尾)和多行(塊)註釋。Python 註釋 與 PHP、BASH 和 Perl 程式語言中的註釋非常相似。
不在字串文字內的井號 (#) 開始一個註釋。井號 (#) 之後直到物理行結束的所有字元都是註釋的一部分,Python 直譯器會忽略它們。
# First comment print ("Hello, World!") # Second comment
這會產生以下結果:
Hello, World!
您可以在語句或表示式的同一行之後鍵入註釋:
name = "Madisetti" # This is again comment
您可以如下注釋多行:
# This is a comment. # This is a comment, too. # This is a comment, too. # I said that already.
以下三引號字串也會被 Python 直譯器忽略,可以用作多行註釋
''' This is a multiline comment. '''
在 Python 程式中使用空行
僅包含空格(可能還有註釋)的行稱為空行,Python 完全忽略它。
在互動式直譯器會話中,您必須輸入一個空物理行來終止多行語句。
等待使用者
程式的以下行顯示提示、說明“按 Enter 鍵退出”的語句,並等待使用者採取操作:
#!/usr/bin/python raw_input("\n\nPress the enter key to exit.")
這裡,"\n\n" 用於在顯示實際行之前建立兩行新行。使用者按下鍵後,程式結束。這是一個很好的技巧,可以使控制檯視窗保持開啟狀態,直到使用者完成應用程式。
單行上的多個語句
分號 (;) 允許在同一行上使用多個語句,前提是這兩個語句都不開始新的程式碼塊。以下是一個使用分號的示例程式碼片段:
import sys; x = 'foo'; sys.stdout.write(x + '\n')
多個語句組作為套件
構成單個程式碼塊的一組單獨語句在 Python 中稱為套件。複合語句或複雜語句(如 if、while、def 和 class)需要一個標題行和一個套件。
標題行以(關鍵字)語句開頭,以冒號 (:) 結束,後面跟著構成套件的一行或多行。例如:
if expression : suite elif expression : suite else : suite
Python 中的命令列引數
許多程式可以執行以提供有關如何執行它們的某些基本資訊。Python 允許您使用 -h 來做到這一點:
$ python3 -h usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -c cmd : program passed in as string (terminates option list) -d : debug output from parser (also PYTHONDEBUG=x) -E : ignore environment variables (such as PYTHONPATH) -h : print this help message and exit [ etc. ]
您還可以以某種方式對指令碼進行程式設計,使其能夠接受各種選項。命令列引數 是一個高階主題,應該在您學習了其他 Python 概念之後再學習。