目录假如我拿出下面的代码,阁下该做何应对? [code]if not reset_excuted and (terminated or truncated): ... else: ...[/code]运算符优先级在 Python 中,布尔运算符的优先级从高到低的顺序如下:
优先级解析示例示例 1: 使用括号[code]a = True b = False c = True result = (a and not b) or c[/code]在这个例子中:
示例 2: 不使用括号[code]x = False y = True z = False result = x or y and not z[/code]在这个例子中:
复杂示例[code]p = True q = False r = False result = not (p and q) or r[/code]在这个示例中:
复杂if语句判断在表达式 [code]if not a and b[/code] 中,[code]not[/code] 只对 [code]a[/code] 生效,不影响 [code]b[/code]。
回到开头的示例,细细的捋一捋,在这段代码中: 在这里,[code]not[/code]只对[code]para_A[/code]生效,而不对[code](para_B or para_C)[/code]生效 要推算在什么情况下进入上面的分支或下面的分支,可以分析条件的每个部分。 分析条件
进入上面的分支的条件团体条件为 [code]not para_A and (para_B or para_C)[/code],因此要进入上面的分支,必须满意以下条件:
进入下面的分支的条件为了进入下面的分支,条件需要不创建,即:
总结条件表
结论
多分支语句elif都写那么多了,干脆再补点东西显得更完整吧 在 Python 中,[code]elif[/code] 是 “else if” 的缩写,用于在 [code]if[/code] 语句中进行多重条件判断。它允许你在第一个 [code]if[/code] 条件为 [code]False[/code] 的情况下继承检查其他条件,从而实现更多的分支逻辑。 [code]if condition1: # 当 condition1 为 True 时执行的代码 elif condition2: # 当 condition1 为 False 且 condition2 为 True 时执行的代码 elif condition3: # 当 condition1 和 condition2 都为 False 且 condition3 为 True 时执行的代码 else: # 当上面的全部条件都为 False 时执行的代码[/code]德摩根定律在j实际代码应用中,你根本用不上这个定律,上面的东西已经可以解决绝大部分题目了。但假如程序非要在[code]if[/code]条件语句上向你发难,至少你也知道怎么应对 德摩根定律是布尔代数中的两个紧张定律,它们提供了关于逻辑运算(与、或和非)之间关系的紧张公式。这两个定律如下: 第一条定律: not(A or B)≡notA and notB 表明:否定 [code]A 或 B[/code] 相称于 [code]A[/code] 和 [code]B[/code] 的否定相与。 第二条定律: not(A and B)≡notA or notB 表明:否定 [code]A 且 B[/code] 相称于 [code]A[/code] 的否定或 [code]B[/code] 的否定。 举例说明我们可以通过几个示例来明白这些定律: 示例 1:第一条定律考虑 [code]A = True[/code] 和 [code]B = False[/code]:
结果是同等的:[code]not(A or B) = False[/code] 和 [code]not A and not B = False[/code]。 示例 2:第二条定律考虑 [code]A = True[/code] 和 [code]B = False[/code]:
同样,结果是相等的:[code]not(A and B) = True[/code] 和 [code]not A or not B = True[/code]。 到此这篇关于Python条件分支 if 语句全讲授的文章就先容到这了,更多相关Python条件分支 if 语句内容请搜索脚本之家从前的文章或继承欣赏下面的相关文章盼望各人以后多多支持脚本之家! 来源:https://www.jb51.net/python/328567pmy.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 19:19 , Processed in 0.032096 second(s), 19 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.