例如x=7 + 3* 2;這里,x被賦值13,而不是20,因為運算符*的優(yōu)先級比+更高,所以它首先乘以3 * 2,然后加7。
這里,具有最高優(yōu)先級運算符出現(xiàn)在表格上方,那些最低的顯示在底部。在一個表達式,更高的優(yōu)先級運算符將首先計算。
例如:
試試下面的例子來理解Python編程語言可供選擇的運算符優(yōu)先級:
#!/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
當執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:
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
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com