以下代码的Python True/False
如何计算Python代码:以下代码的Python True/False,python,boolean,Python,Boolean,如何计算Python代码: not not True or False and not True 至于我自己,我有两个猜测: 方法1: 步骤代码 1不正确或错误且不正确 2不False或False和False 3True或False和False 4正确和错误 5False 方法2: 步骤代码 1不正确或错误且不正确 2不False或False和False 3正确或错误 4正确 从以下表格中: 或的优先级低于 和的优先级依次较低 而不是not 据认为: not not True or
not not True or False and not True
至于我自己,我有两个猜测:
方法1:
步骤代码1不正确或错误且不正确
2不False或False和False 3True或False和False
4正确和错误
5False
方法2: 步骤代码
1不正确或错误且不正确
2不False或False和False 3正确或错误
4正确
从以下表格中:
的优先级低于或
的优先级依次较低和
- 而不是
not
据认为:
not not True or False and not True
相当于
((not (not True)) or (False and (not True)))
编辑:正如Martijn Pieters在下面的评论中所注意到的,值得一提的是Python有短路操作符。这意味着
和
和或
保证从左到右进行评估,并且:
- 如果
的左项为或
则永远不会计算右项(作为布尔逻辑中真
真或任何
为
的结果)真
- 如果
的左项为和
则永远不会计算右项(作为布尔逻辑中假
为假和任何
假的结果)
非真
评估为假
:
((not (not True)) or (False and (not True)))
^^^^^^^^
False
非False
计算为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
True或…
Python立即停止计算,结果为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
的优先级低于或
的优先级依次较低和
- 而不是
not
据认为:
not not True or False and not True
相当于
((not (not True)) or (False and (not True)))
编辑:正如Martijn Pieters在下面的评论中所注意到的,值得一提的是Python有短路操作符。这意味着
和
和或
保证从左到右进行评估,并且:
- 如果
的左项为或
则永远不会计算右项(作为布尔逻辑中真
真或任何
为
的结果)真
- 如果
的左项为和
则永远不会计算右项(作为布尔逻辑中假
为假和任何
假的结果)
非真
评估为假
:
((not (not True)) or (False and (not True)))
^^^^^^^^
False
非False
计算为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
True或…
Python立即停止计算,结果为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
的优先级低于或
的优先级依次较低和
- 而不是
not
据认为:
not not True or False and not True
相当于
((not (not True)) or (False and (not True)))
编辑:正如Martijn Pieters在下面的评论中所注意到的,值得一提的是Python有短路操作符。这意味着
和
和或
保证从左到右进行评估,并且:
- 如果
的左项为或
则永远不会计算右项(作为布尔逻辑中真
真或任何
为
的结果)真
- 如果
的左项为和
则永远不会计算右项(作为布尔逻辑中假
为假和任何
假的结果)
非真
评估为假
:
((not (not True)) or (False and (not True)))
^^^^^^^^
False
非False
计算为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
True或…
Python立即停止计算,结果为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
的优先级低于或
的优先级依次较低和
- 而不是
not
据认为:
not not True or False and not True
相当于
((not (not True)) or (False and (not True)))
编辑:正如Martijn Pieters在下面的评论中所注意到的,值得一提的是Python有短路操作符。这意味着
和
和或
保证从左到右进行评估,并且:
- 如果
的左项为或
则永远不会计算右项(作为布尔逻辑中真
真或任何
为
的结果)真
- 如果
的左项为和
则永远不会计算右项(作为布尔逻辑中假
为假和任何
假的结果)
非真
评估为假
:
((not (not True)) or (False and (not True)))
^^^^^^^^
False
非False
计算为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
True或…
Python立即停止计算,结果为True
:
((not False ) or (False and (not True)))
^^^^^^^^^^^^^^
True
( True or (False and (not True)))
^^^^^^^^^^^^^^ ......................
True (right-hand side ignored)
要理解这一点,您应该看到 您的代码相当于:
((not (not True)) or (False and (not True)))
现在,您应该能够知道什么答案是正确的,为什么是正确的。要理解这一点,您应该看到 您的代码相当于:
((not (not True)) or (False and (not True)))
现在,您应该能够知道什么答案是正确的,为什么是正确的。要理解这一点,您应该看到 您的代码相当于:
((not (not True)) or (False and (not True)))
现在你应该能够说出什么答案是正确的,为什么是正确的。要理解这一点,你应该