判斷式結果是布林(True/False),決定程式走哪個區塊。由兩類運算組成:
< <= > >= == != 值比較;in/not in 從屬;is/is not 物件and/or/not;優先序 not > and > orand 遇 False 即停、or 遇 True 即停。把便宜/關鍵條件放前面。5 < x < 10 等同 x>5 and x<10。由上往下逐一判斷,命中其一就執行、其餘跳過。互斥分支用 elif(不是多個獨立 if)。
if score < 60:
print('不及格')
print('=====') # 屬 ifif score < 60:
print('不及格')
print('=====') # 一定印Python 用縮排界定區塊(4 空格);縮排錯 → 區塊錯/IndentationError。
值1 if 條件 else 值2if 後忘記冒號 : → SyntaxError;用 = 當判斷(應 ==)。elif,不要排多個獨立 if。not > and > or,複合條件加括號釐清。5<x<10 或 x>5 and x<10。寫任何條件:先確認冒號與縮排、互斥用 elif、布林優先序、邊界值對不對。