“and”布尔运算符,如果其中一个表达式为False,预期结果是False。

huangapple go评论60阅读模式
英文:

Boolean operator 'and', expect False answer if one expression is False?

问题

I'm a 1-week self-taught Python3 guy, and I'm into the comparison and boolean operators and learn this:

True and True True
True and False False
False and True False
False and False False

So, I call

print(3 < 4) and (6 < 5)
True + False

So, why do I get 'True'?

I have tried all other booleans trying to make some similar error, changing the way I enter this ('<') sign, entering this line in Python tutor, and the result is the same, and makes me think 6 is minor than 5 somehow, so I think I'm not looking at what should I look, and it's gonna be embarrassing to know the answer. Thanks.

英文:

I´m a 1 week self taught python3 guy, and i´m into the comparison and boolean operators and learn this:

True   and   True   True
True   and   False  False
False  and   True   False
False  and   False  False

So, i call

print(3 &lt; 4) and (6 &lt; 5)
      True    +   False

So, why do i get True?

I have tried all other booleans trying to make some similar error, changing the way i enter this (&lt;) sign, entering this line in python tutor and the result is the same, and makes me think 6 is minor then 5 some how, so i think i´m not looking at what should i look and it´s gonna be embarrassing to know the answer. Thanks.

答案1

得分: 0

因为你没有打印 (3 &lt; 4) and (6 &lt; 5),而是打印了 3 &lt; 4

第一:你在调用 print 函数

在调用函数时,你可能记得要使用括号提供函数的参数。例如:

print("Hello world")

会打印 Hello World,因为这是提供给它的参数。

同样地,当你使用 3 &lt; 4 调用 print 函数时,这个表达式实际上是真的。

第二:你有一个格式错误的布尔表达式

当你说:

a and b

Python 检查 ab 是否都为真。所以如果你这样做:

print(3 &lt; 4) and (6 &lt; 5)

a = print(3 &lt; 4),而 b = (6 &lt; 5)

第三:你错了,表达式的结果是 False

你说这个表达式的结果是 True,但实际上不正确。这个表达式的结果是 False,因为 (6 < 5)... 但你只打印了 3 &lt; 4

第四:你不需要括号

Python 被设计成一种简单、干净和可读性强的语言。通过在代码中添加不必要的符号和标记,你会使它变得难以理解。这导致代码可读性降低,可能会导致许多错误,就像你的错误一样。

始终避免在不需要的地方使用括号。你可以通过这样写来实现相同的效果:

print(3 &lt; 4) and 6 &lt; 5

然而,你的意思完全不同。你的意思是要写成:

print(3 &lt; 4 and 6 &lt; 5)
英文:

Because you aren't printing (3 &lt; 4) and (6 &lt; 5), you are printing 3 &lt; 4.

First: You are calling the print function

When calling a function, you may recall that you use parenthesis to provide arguments to the function. For example:

print(&quot;Hello world&quot;)

Will print Hello World because that is the argument provided to it.

Similarly, when you invoke the print function with 3 &lt; 4 – this expression is in fact true.

Second: You have a malformed boolean expression

When you say:

a and b

Python checks to see if both a and b are true. So if you do:

print(3 &lt; 4) and (6 &lt; 5)

a = print(3 &lt; 4) and b = (6 &lt; 5)

Third: You are wrong: the expression evaluates to False

You said that the expression evaluates to True. But that is not actually correct. The expression evaluates to False because (6 < 5)... but you only print out 3 &lt; 4.

Fourth: You don't need parenthesis

Python was created to be a simple, clean, and readable language. By adding unnecessary symbols and tokens to the code, you obfuscate it. This leads to less readable code and can lead to many mistakes, such as yours.

Always avoid using parenthesis where not needed. You could have accomplished the same thing by writing:

print(3 &lt; 4) and 6 &lt; 5

However, you meant something totally different. You meant to write:*

print(3 &lt; 4 and 6 &lt; 5)

huangapple
  • 本文由 发表于 2023年5月18日 01:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定