
本課程闡述 Python 程式碼的組織方式與執行流程控制。Python 程式語句以換行結束,並強制使用 4 個空白作為程式碼區段的區隔。程式碼流程控制結構分為循序、選擇和重複三大類。
在選擇結構中,主要使用 if、if…else 或 if…elif…else 進行條件判斷,Python 3.10 版本後更提供 match 選擇式。重複結構則由 for 迴圈(用於遍歷資料集合)和 while 迴圈(用於條件判斷式循環)組成,可搭配 break 或 continue 控制循環。
此外,程式功能可透過 功能函式或自訂函式 (def) 管理。函式內的變數屬區域變數,離開區段即結束生命週期。若要在函式內修改外部的全域變數,必須顯式使用 global 關鍵字。
This course details Python code organization and execution flow control. Python statements end with a newline, and code blocks are strictly delimited by 4 spaces. Program flow control structures are categorized into Sequential, Selection, and Repetition.
The Selection structure primarily uses if, if…else, or if…elif…else for conditional judgment, with the match construct available since Python 3.10. The Repetition structure consists of the for loop (for iterating over data collections) and the while loop (for conditional looping), which can be managed using break or continue.
Furthermore, program functionality is managed via built-in functions or custom functions (def). Variables within a function are local variables, whose lifecycle ends upon exiting the block. To modify an external global variable from within a function, the global keyword must be explicitly used.