如果我拿出下面的代码,阁下该做何应对?
if not reset_excuted and (terminated or truncated):
...
else:
...运算符优先级
在 python 中,布尔运算符的优先级从高到低的顺序如下:
- 括号
():最高优先级,可以用于明确运算顺序。 not:次高优先级。and:次低优先级。or:最低优先级。
优先级解析示例
示例 1: 使用括号
a = true b = false c = true result = (a and not b) or c
在这个例子中:
- 括号 首先被计算:
a and not b中not b计算为not false,结果为true。- 然后,
true and true计算为true。
- 最后,整体表达式变为
true or c,结果是true。
示例 2: 不使用括号
x = false y = true z = false result = x or y and not z
在这个例子中:
- 优先级 按照
not>and>or:not z计算为not false,结果是true。
- 然后表达式转为
x or y and true。 - 接着
y and true计算为true。 - 最终计算为
x or true,结果是true。
复杂示例
p = true q = false r = false result = not (p and q) or r
在这个示例中:
- 括号 首先被计算:
p and q计算为true and false,结果是false。
- 然后,
not false计算为true。 - 最终表达式变为
true or r,结果是true。
复杂if语句判断
在表达式 if not a and b 中,not 只对 a 生效,不影响 b。
not的优先级高于and,这意味着它会先处理a的值。- 首先计算
not a,这将返回a的布尔值的相反值。 - 然后,使用
and运算符将结果与b进行比较。
if not para_a and (para_b or para_c):
print("进入上面的分支")
else:
print("进入下面的分支")回到开头的示例,细细的捋一捋,在这段代码中:
在这里,not只对para_a生效,而不对(para_b or para_c)生效
要推算在什么情况下进入上面的分支或下面的分支,可以分析条件的每个部分。
分析条件
not para_a:要求para_a为false。- 这意味着要进入上面的分支,
para_a必须是false。
- 这意味着要进入上面的分支,
(para_b or para_c):要求para_b或para_c至少有一个为true。- 这意味着只要
para_b为true或para_c为true,这个部分就成立。
- 这意味着只要
进入上面的分支的条件
整体条件为 not para_a and (para_b or para_c),因此要进入上面的分支,必须满足以下条件:
- 条件 1:
para_a是false。 - 条件 2:
para_b是true或para_c是true(至少有一个为true)。
进入下面的分支的条件
为了进入下面的分支,条件需要不成立,即:
条件 a:
para_a是true。- 这时
not para_a为false,条件就不成立。
- 这时
条件 b:
para_a是false,但para_b和para_c都是false。- 这时
(para_b or para_c)为false,条件也不成立。
- 这时
总结条件表
| para_a | para_b | para_c | 结果 |
|---|---|---|---|
| false | true | false | 进入上面的分支 |
| false | false | true | 进入上面的分支 |
| false | true | true | 进入上面的分支 |
| true | false | false | 进入下面的分支 |
| true | true | true | 进入下面的分支 |
| false | false | false | 进入下面的分支 |
结论
- 进入上面的分支:当
para_a为false,且para_b或para_c至少有一个为true。 - 进入下面的分支:当
para_a为true或者para_a为false,但para_b和para_c都为false。
多分支语句elif
都写那么多了,干脆再补点东西显得更完整吧
在 python 中,elif 是 “else if” 的缩写,用于在 if 语句中进行多重条件判断。它允许你在第一个 if 条件为 false 的情况下继续检查其他条件,从而实现更多的分支逻辑。
if condition1:
# 当 condition1 为 true 时执行的代码
elif condition2:
# 当 condition1 为 false 且 condition2 为 true 时执行的代码
elif condition3:
# 当 condition1 和 condition2 都为 false 且 condition3 为 true 时执行的代码
else:
# 当上面的所有条件都为 false 时执行的代码德摩根定律
在j实际代码应用中,你基本用不上这个定律,上面的东西已经可以解决绝大部分问题了。但如果程序非要在if条件语句上向你发难,至少你也知道怎么应对
德摩根定律是布尔代数中的两个重要定律,它们提供了关于逻辑运算(与、或和非)之间关系的重要公式。这两个定律如下:
第一条定律:
not(a or b)≡nota and notb
解释:否定 a 或 b 相当于 a 和 b 的否定相与。
第二条定律:
not(a and b)≡nota or notb
解释:否定 a 且 b 相当于 a 的否定或 b 的否定。
举例说明
我们可以通过几个示例来理解这些定律:
示例 1:第一条定律
考虑 a = true 和 b = false:
- 计算
not(a or b):a or b是truenot(a or b)是false
- 计算
not a and not b:not a是falsenot b是truenot a and not b是false
结果是一致的:not(a or b) = false 和 not a and not b = false。
示例 2:第二条定律
考虑 a = true 和 b = false:
- 计算
not(a and b):a and b是falsenot(a and b)是true
- 计算
not a or not b:not a是falsenot b是truenot a or not b是true
同样,结果是相等的:not(a and b) = true 和 not a or not b = true。
到此这篇关于python条件分支 if 语句全讲解的文章就介绍到这了,更多相关python条件分支 if 语句内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论