Published on

[Python 學習筆記] 4-條件判斷

Authors
  • avatar
    Name
    Vic Chen
    Twitter

條件判斷(if / elif / else)

Python 的條件判斷語法相當直覺,使用 ifelifelse 配合縮排(indentation)來表達程式的邏輯分支。


基本語法

x = 10
if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")

IMPORTANT

Python 中 縮排是語法的一部分,不能混用 tab 與空格,通常使用 4 個空格。

布林判斷(Truthy / Falsy)

以下物件會被視為 False:

  • 0 或 0.0
  • 空字串 ""
  • 空容器,如 []、、set()
  • None

其他值則都會被視為 True:

if "Python":
    print("This is True")  # 因為非空字串被視為 True

if 0:
    print("Never print")   # 因為 0 是 False

巢狀條件

條件語句可以巢狀使用:

age = 20
if age >= 18:
    if age >= 65:
        print("Senior")
    else:
        print("Adult")
else:
    print("Minor")

條件運算子(Ternary Expression)

適合單行判斷的情境:

x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result)  # Even

使用 in / not in

用於判斷元素是否存在於容器(list、dict、set、string)中:

name = "Alice"
if "A" in name:
print("Contains A")

if "z" not in name:
print("No z found")