MrThinco 发表于 2019-10-7 11:37:13

Python中的逻辑运算题,一起来分析一下吧

在Python中
请用最快速度说出答案:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9

答案是:4
对于这道题,我是这样分析的不知道对不对。参考了PHP和js的理解,希望大家看看有没有什么问题,希望给我指正一下。感谢您们的帮助。
首先,我人为的加上圆括号帮助分析:(not 1) or (0 and 1) or (3 and 4) or (5 and 6) or ((7 and 8) and 9),
其次,我的理解是:逻辑运算符,首先优先级,not>and>or。其次,x or y 如果x为:true的值,则返回x的值,否则返回y值。 x and y 如果x为false,则返回x的值,否则返回y值。

再次感谢大家

zltzlt 发表于 2019-10-7 11:49:21

   (not 1) or (0 and 1) or (3 and 4) or (5 and 6) or ((7 and 8) and 9)
== 0 or 0 or 4 or 6 or 9
== 0 or 4 or 6 or 9
== 4 or 6 or 9
== 4 or 9
== 4

MrThinco 发表于 2019-10-7 19:25:58

zltzlt 发表于 2019-10-7 11:49
(not 1) or (0 and 1) or (3 and 4) or (5 and 6) or ((7 and 8) and 9)
== 0 or 0 or 4 or 6 or 9
...

感谢您的分析,非常感谢。明白了,和我思考的一样。谢谢
页: [1]
查看完整版本: Python中的逻辑运算题,一起来分析一下吧