有谁知道为什么下面的代码在Python中没有得到相同的结果?
为什么我需要括号才能得到正确的结果?
#example 1
print 1-4 %5
outcome: -3
#example 2
print (1-4)%5
outcome: 2
这是因为运算符优先级 https://docs.python.org/3/reference/expressions.html#operator-precedence. Mod (%
) 优先于-
, so:
1-4 % 5 == 1 - (4 % 5) == 1 - 4 == -3
but
(1-4) % 5 == -3 % 5 == 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)