当前位置: 代码网 > it编程>前端脚本>Python > Python条件分支 if 语句全讲解(一文掌握)

Python条件分支 if 语句全讲解(一文掌握)

2024年10月11日 Python 我要评论
如果我拿出下面的代码,阁下该做何应对?if not reset_excuted and (terminated or truncated):...else: ...运算符优先级在 python

如果我拿出下面的代码,阁下该做何应对?

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),因此要进入上面的分支,必须满足以下条件:

  • 条件 1para_a 是 false
  • 条件 2para_b 是 true 或 para_c 是 true(至少有一个为 true)。

进入下面的分支的条件

为了进入下面的分支,条件需要不成立,即:

  • 条件 apara_a 是 true

    • 这时 not para_a 为 false,条件就不成立。
  • 条件 bpara_a 是 false,但 para_b 和 para_c 都是 false

    • 这时 (para_b or para_c) 为 false,条件也不成立。

总结条件表

para_apara_bpara_c结果
falsetruefalse进入上面的分支
falsefalsetrue进入上面的分支
falsetruetrue进入上面的分支
truefalsefalse进入下面的分支
truetruetrue进入下面的分支
falsefalsefalse进入下面的分支

结论

  • 进入上面的分支:当 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 是 true
    • not(a or b) 是 false
  • 计算 not a and not b
    • not a 是 false
    • not b 是 true
    • not 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 是 false
    • not(a and b) 是 true
  • 计算 not a or not b
    • not a 是 false
    • not b 是 true
    • not a or not b 是 true

同样,结果是相等的:not(a and b) = true 和 not a or not b = true

到此这篇关于python条件分支 if 语句全讲解的文章就介绍到这了,更多相关python条件分支 if 语句内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com