时间:2021-07-01 10:21:17 帮助过:8人阅读
例如x=7 + 3* 2;这里,x被赋值13,而不是20,因为运算符*的优先级比+更高,所以它首先乘以3 * 2,然后加7。
这里,具有最高优先级运算符出现在表格上方,那些最低的显示在底部。在一个表达式,更高的优先级运算符将首先计算。
例如:
试试下面的例子来理解Python编程语言可供选择的运算符优先级:
#!/usr/bin/python a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "Value of ((a + b) * c) / d is ", e e = (a + b) * (c / d); # (30) * (15/5) print "Value of (a + b) * (c / d) is ", e e = a + (b * c) / d; # 20 + (150/5) print "Value of a + (b * c) / d is ", e
当执行上面的程序,它会产生以下结果:
Value of (a + b) * c / d is 90 Value of ((a + b) * c) / d is 90 Value of (a + b) * (c / d) is 90 Value of a + (b * c) / d is 50