Python3 条件控制
if / elif / else 分支、if 嵌套与 match...case 模式匹配。
if 语句
Python marks blocks with a colon plus indentation, not parentheses. The indented block runs when the condition is true; otherwise else runs. Chains use elif, evaluated top-down with the first match winning.
age = 20
if age >= 18:
print("adult")
else:
print("minor")
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 60:
grade = 'C'
else:
grade = 'D'
print("grade:", grade)adult
grade: Bif 嵌套
An if block may contain another if. Deep nesting hurts readability — merge conditions with and or return early to flatten it.
num = 12
if num % 2 == 0:
if num % 3 == 0:
print(num, "divisible by 6")
else:
print(num, "even but not divisible by 3")
else:
print(num, "odd")
# 扁平化写法
if num % 2 == 0 and num % 3 == 0:
print(num, "divisible by 6")12 divisible by 6
12 divisible by 6match...case
Python 3.10 adds match...case, which matches patterns rather than comparing values. | means "or", case _ is the catch-all, and sequence/mapping/class patterns can destructure data.
def http_status(code):
match code:
case 200:
return "OK"
case 301 | 302:
return "Redirect"
case 404:
return "Not Found"
case _:
return "Unknown"
for c in (200, 302, 404, 500):
print(c, http_status(c))
def describe(point):
match point:
case (0, 0):
return "origin"
case (x, 0):
return f"on x-axis at {x}"
case (0, y):
return f"on y-axis at {y}"
case (x, y):
return f"at {x},{y}"
print(describe((0, 0)), describe((5, 0)), describe((0, 7)), describe((2, 3)))200 OK
302 Redirect
404 Not Found
500 Unknown
origin on x-axis at 5 on y-axis at 7 at 2,3match is not switch: case 200 matches a *pattern*, so case some_name captures anything. Use literals or ALL_CAPS names to compare against constants.if 语句
Python 的 if 用冒号加缩进表示代码块,不需要括号。条件为真时执行缩进块,否则跳到 else;多分支用 elif 依次判断,命中即跳出。
age = 20
if age >= 18:
print("adult")
else:
print("minor")
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 60:
grade = 'C'
else:
grade = 'D'
print("grade:", grade)adult
grade: Bif 嵌套
if 块内部可以再写 if,形成嵌套判断。嵌套层数越多可读性越差,通常可以用 and 合并条件,或提前 return 来扁平化。
num = 12
if num % 2 == 0:
if num % 3 == 0:
print(num, "divisible by 6")
else:
print(num, "even but not divisible by 3")
else:
print(num, "odd")
# 扁平化写法
if num % 2 == 0 and num % 3 == 0:
print(num, "divisible by 6")12 divisible by 6
12 divisible by 6match...case
Python 3.10 引入 match...case,按模式匹配而不是逐个比较。| 表示「或」,case _ 是兜底分支,还可以用序列、映射、类模式解构数据。
def http_status(code):
match code:
case 200:
return "OK"
case 301 | 302:
return "Redirect"
case 404:
return "Not Found"
case _:
return "Unknown"
for c in (200, 302, 404, 500):
print(c, http_status(c))
def describe(point):
match point:
case (0, 0):
return "origin"
case (x, 0):
return f"on x-axis at {x}"
case (0, y):
return f"on y-axis at {y}"
case (x, y):
return f"at {x},{y}"
print(describe((0, 0)), describe((5, 0)), describe((0, 7)), describe((2, 3)))200 OK
302 Redirect
404 Not Found
500 Unknown
origin on x-axis at 5 on y-axis at 7 at 2,3match 不是 switch:case 200 匹配的是结构而不是常量比较,所以 case some_variable 会捕获任意值。要比较常量请用全大写命名或字面量。