2024-06-02 05:12:05 发布
网友
有人知道为什么下面的代码在Python中没有相同的结果吗? 为什么我需要括号才能得到正确的结果
#example 1 print 1-4 %5 outcome: -3 #example 2 print (1-4)%5 outcome: 2
这是由于operator precedence。Mod(%)优先于-,因此:
%
-
1-4 % 5 == 1 - (4 % 5) == 1 - 4 == -3
但是
(1-4) % 5 == -3 % 5 == 2
Python运算符优先级的负刚低于模数
http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
*, /, % Multiplication, division, remainder +, - Addition, subtraction
这是由于operator precedence。Mod(
%
)优先于-
,因此:但是
Python运算符优先级的负刚低于模数
http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
相关问题 更多 >
编程相关推荐