运算符优先级来确定条件的表达式中的分组。这会影响一个表达式如何计算。某些运算符的优先级高于其他;例如,乘法运算符的优先级比加法运算更高。
例如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
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8