Skip to content

条件

一句话解释

条件 是 表达式 那个 evaluates 到 真 or 假, used 到 control 哪个 分支 的 code 执行.

用大白话说

条件是 表达式那个 evaluates 到布尔值 (真 or 假). 条件 control program flow 使用 if, elif (else 如果), and else statements.

Python evaluates conditions from top to bottom. The first true condition's block runs, and the rest are skipped. elif and else are optional. Every 如果 block must be indented.

Comparison operators produce conditions: == (equal), != (not equal), >, <, >=, <=. Logical operators combine conditions: and (both true), or (either true), 不 (invert). Membership (in) and identity (是) also produce booleans.

示例

python
score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

print(grade)  # B

# Combined conditions
age = 25
has_id = True
if age >= 18 and has_id:
    print("Entry allowed")

条件 use 如果/elif/else 使用 comparison and logical 运算符 到 control flow.

常见混淆

  • 容易混淆的对象: 循环

    关键区别: 条件 (如果) 运行 块 一次 如果 测试 是 真. 循环 (虽然) 运行 块 repeatedly as 长 as 测试 是 真. 条件 制作 decisions; 循环 制作 repetitions.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表