下表列出了從最高優先順序到最低優先順序的所有運算子,如下所示 -
序號 | 運算子 | 描述 |
---|---|---|
1 | ** |
指數(次冪)運算 |
2 | ~ + - |
二補數,一元加減(最後兩個的方法名稱是+@ 和-@ ) |
3 | * / % // |
乘法,除法,模數和地板除 |
4 | + - |
|
5 | >> << |
向右和向左位移 |
6 | & |
按位元與 |
7 | ^ |
按位元互斥或和常規的「OR 」 |
8 | <= < > >= |
比較運算子 |
9 | <> == != |
等於運算子 |
10 | = %= /= //= -= += *= **= |
賦值運算子 |
11 | is is not |
身份運算子 |
12 | in not in |
成員運算子 |
13 | not or and |
邏輯運算子 |
#! /usr/bin/env python
#coding=utf-8
#save file : operators_precedence_example.py
#!/usr/bin/python3
a = 20
b = 10
c = 15
d = 5
print ("a:%d b:%d c:%d d:%d" % (a,b,c,d ))
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)
將上面程式碼儲存到檔案: operators_precedence_example.py 中,執行結果如下 -
F:\worksp\python>python operators_precedence_example.py
a:20 b:10 c:15 d:5
Value of (a + b) * c / d is 90.0
Value of ((a + b) * c) / d is 90.0
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0