提问人:newnewbiiii 提问时间:9/24/2023 最后编辑:Goku - stands with Palestinenewnewbiiii 更新时间:9/24/2023 访问量:90
我不明白为什么它会这样工作
I cannot understand why it work like that
问:
x = 2
y = 2
if x % 2 == 1 & y % 2 == 0:
print(x, y)
2 2
我不明白结果。
当有时,我认为不可能存在。if x % 2 == 1 & y % 2 == 0:
2 2
答:
1赞
Goku - stands with Palestine
9/24/2023
#1
让我们揭开神秘面纱:
%
具有高于&
&
具有高于==
所以执行的顺序将是 , ,%
&
==
x = 2
y = 2
if x % 2 == 1 & y % 2 == 0: # x%2, y%2 will be 0
print(x, y)
将变成:
if 0 == 1 & 0 == 0: # 1 & 0 will be 0
print(x, y)
将变成:
if 0 == 0 == 0:
print(x, y)
将变成:
if True:
print(x, y)
输出
2 2
优先级表的快照:
0赞
Reilas
9/24/2023
#2
if 语句正在执行以下操作。
(x % 2) == 1 & (y % 2) == 0
评论
&
==
&&
and
&
&&
&&
and
&
是一个位运算符,用于逻辑,您需要使用and