Python 相關
Python Programming Language
Basics
程式碼/字串 太長時怎麼換行?
Pause (Press the <ENTER> key to continue…)
Definitions
函式 (function) 和 方法 (method)
Variable Scope: global, nonlocal
確認某變數/函式/方法是否已定義
UnboundLocalError (Variable Scope inside a Function)
Unpacking 解包, *identifier / **identifier
iterator & generator
functools: @lru_cache…
Operations
Bitwise Operation
多重賦值 (Multiple Assignment) 時的計算順序
⭐️ Assignment Expressions 「:=」(賦值後執行 Expressions)
運算子優先序 Operator Precedence
Chaining Comparison Operators (a > b and b > c → a > b > c)
判斷是否為 空(None) (0 為 False)
any(), all()
Membership Test Operations (in / not in)
lambda parameter: expression
Statements
⭐️ assert 斷言
match-case: Python 中的 Switch-case
do-while Loop in Python
Error/Exception Processing (try statement; try-except)
快速套用 try-except 到 method 上 (使用 functools)
iterable / Collections
list, set, dict, collections (deque, defaultdict, Counter)
Enumerate a Multi-Dimensional List
COUNTIF in Python
計算某元素出現的次數 count()
List
List Comprehensions [ … for … in … ]
逆序存取 (for e in lst[::-1])
複製物件 (lst_b = lst_a[:])
String
f-string (String Formatting)
串接字串 “…”.join()
填充字串長度 rjust()
str 先比較 len 再比較 Lexicographic Order
import 相關
requirements.txt
from … import *
一鍵導入自定義 Library 資料夾
常用 Libraries / Algorithms
os
遍歷資料夾 (os.walk(), os.scandir(), os.listdir())
Get the Current Directory/Folder
Get the Root Directory/Drive
Date & Time
⭐️ datetime
使用 signal 和 decorator 讓函式執行超過時限時中斷
測量片段代碼執行所需時間
File Processing
Read All Lines in Text File by Loop (with … as …:)
Data Structure
heapq (優先權佇列 Priority Queue)
Math
math: inf, prod(), floor(), ceil(), isqrt()…
Sorting
Sorting 排序
⭐️ Sort by Multiple Keys
⭐️ Python 中的 Binary Search (bisect, insort)
RE
正則表達式 Regular Expression (re)
並行計算
asyncio (並行計算)
Web Crawler
Scrapy
requests
Jupyter
在 Jupyter 中顯示圖片
不停止 Kernal 而於指定 Cell 中斷
Last Updated on 2025/05/13 by A1go