你真的会 Python 吗?这题怎么解?

preview.jpg

最近学习 Python,发现一个很尴尬的问题。实在是觉得羞耻,我也是学过 Java、C#,了解 C、C++ ,项目经验有五年以上的猿了

说正事

Python 中的与或非运算符相信各位大神已经非常熟悉了
下面代码请做心里运算(坦然面对自己)

print(True and False)
print(True and False or False)
print(False and False or True and not 1 == 1)
print(True and False or not False)
print(False and True or not False)

揭晓谜底

True and False = False
True and False or False = False
False and False or True and not 1 == 1 = False
True and False or not False = True
False and True or not False = True

坦白的说,前面两句的结果我是知道的,但是当看到第3行时我彻底石化了,我敲了这好几年的假代码?内心世界真的崩塌了

大神看到这种题可能就是会心一笑看煞笔的心情,但是就算只有我一个人栽在这里,我也想提醒各位,基础真的很重要

上面这些代码其实主要涉及到 Python 中的运算符优先级

优先级排序如下

not > and > or
not > and > or
not > and > or

过于羞耻,大神勿喷,溜了溜了

你可能感兴趣的:(你真的会 Python 吗?这题怎么解?)