逻辑运算符“或"的错误? [英] Bug with logic operator "or"?

查看:138
本文介绍了逻辑运算符“或"的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习python语言.尽管确实很简单,但是我在IDLE和python中使用逻辑运算符时都得到了一些意外的结果.

I'm learning the language python. And although it is very simple indeed, I got some unexpected results using logic operators both in IDLE and in python.

我在IDLE中做了一个简单的测试,如下所示:

I made a simple test in IDLE like this:

(2 or 10) in range(1,6)

我的回答是True.到目前为止,一切都很好.但是,如果我这样做:

And my response was True. So far so good. However, if I do this:

(10 or 2) in range(1,6)

我的答复是False.即使"2"明显位于range(1,6)内.

My response is False. Even though "2" is clearly inside the range(1,6).

我在PyCharm中进行了相同的测试,这是我的回复:

I made the same test in PyCharm and here are my responces:

if (2 or 10) in range(1,6):
    print("True")
else:
    print("False")

结果:True

if (10 or 2) in range(1,6):
    print("True")
else:
    print("False")

结果:False

if 2 or 10 in range(1,6):
    print("True")
else:
    print("False")

结果:True

if 10 or 2 in range(1,6):
    print("True")
else:
    print("False")

结果:True

我想知道其背后的逻辑.

I would like to know the logic behind it please.

推荐答案

OR返回它遇到的第一个TRUE值.

OR returns the first TRUE value it encounters.

也就是说:

>>> (2 or 10) 
# returns 2
>>> (10 or 2)
# returns 10

更新

要在下面解决OP的评论:

To address OP's comment below:

truthy 值评估为True,有 falsey 值评估为False.例如,0为假值.其余整数是真实值.因此,10也是一个真实值.

There are truthy values which evaluate to True and there are falsey values which evaluate to False. For example, 0 is a falsey value. Rest of the integers are truthy values. Therefore, 10 is also a truthy value.

如果您这样做:

>>> if 10: # since 10 is truthy, this statement will execute.
        print("Okay!")
    else:
        print("Not okay!")

# prints "Okay!"

继续前进,10 or 2 in range(1, 6)的计算结果为10 or (2 in range(1, 6)).

Moving on, 10 or 2 in range(1, 6) evaluates to 10 or (2 in range(1, 6)).

 10     or     (2 in range(1, 6))
\__/           \________________/
True               True

# Returns 10 because it's a truthy value. 
# OR operator only evaluates until it finds a True value.

让我们看看另一个示例:

Let's see another example:

 0      or    10
\_/          \__/
False        True

# Returns 10 because 0 is a falsey value, so the 
# OR operator continues evaluating the rest of the expression

最后,让我们看一下if表达式:

Finally, let's see the if expression:

 >>> if 10 or 2 in range(1, 6):
        print("True")
     else:
        print("False")
 # prints "True"

它打印True,因为10 or 2 in range(1, 6)返回10,并且如上所述,if 10的值为True,因此,执行了if块.

It prints True because 10 or 2 in range(1, 6) returns 10 and as we saw above, if 10 evaluates to True, hence, the if block is executed.

其他:

正确的表达将是这样:

>>> 10 in range(1, 6) or 2 in range(1, 6)
# returns True

此表达式将返回True,因为即使10不在给定范围内,但2也在给定范围内.

This expression will return True because even though 10 is not in the given range, but 2 is.

10 in range(1, 6)   or   2 in range(1, 6)
\_______________/        \______________/ 
     False                     True

# Returns True because OR will keep on evaluating
# until it finds a True value

但是,如果要检查10和2是否都在范围内,则必须使用AND运算符:

But if you want to check if 10 and 2 both are in the range, you'll have to use the AND operator:

>>> 10 in range(1, 6) and 2 in range(1, 6)
# returns False

这篇关于逻辑运算符“或"的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆