英文:
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 < 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 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 < 4) and (6 < 5)
,而是打印了 3 < 4
。
第一:你在调用 print 函数
在调用函数时,你可能记得要使用括号提供函数的参数。例如:
print("Hello world")
会打印 Hello World
,因为这是提供给它的参数。
同样地,当你使用 3 < 4
调用 print
函数时,这个表达式实际上是真的。
第二:你有一个格式错误的布尔表达式
当你说:
a and b
Python 检查 a
和 b
是否都为真。所以如果你这样做:
print(3 < 4) and (6 < 5)
a = print(3 < 4)
,而 b = (6 < 5)
。
第三:你错了,表达式的结果是 False
你说这个表达式的结果是 True
,但实际上不正确。这个表达式的结果是 False
,因为 (6 < 5)... 但你只打印了 3 < 4
。
第四:你不需要括号
Python 被设计成一种简单、干净和可读性强的语言。通过在代码中添加不必要的符号和标记,你会使它变得难以理解。这导致代码可读性降低,可能会导致许多错误,就像你的错误一样。
始终避免在不需要的地方使用括号。你可以通过这样写来实现相同的效果:
print(3 < 4) and 6 < 5
然而,你的意思完全不同。你的意思是要写成:
print(3 < 4 and 6 < 5)
英文:
Because you aren't printing (3 < 4) and (6 < 5)
, you are printing 3 < 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("Hello world")
Will print Hello World
because that is the argument provided to it.
Similarly, when you invoke the print
function with 3 < 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 < 4) and (6 < 5)
a = print(3 < 4)
and b = (6 < 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 < 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 < 4) and 6 < 5
However, you meant something totally different. You meant to write:*
print(3 < 4 and 6 < 5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论