標籤:
Python 運算子 什麼是運算子?
本章節主要說明Python的運算子。舉個簡單的例子 4 +5 = 9 。 例子中,4和5被稱為運算元,"+"號為運算子。
Python語言支援以下類型的運算子:
- 算術運算子
- 比較(關係)運算子
- 賦值運算子
- 邏輯運算子
- 位元運算符
- 成員運算子
- 身份運算子
- 運算子優先順序
接下來讓我們一個個來學習Python的運算子。
Python算術運算子
以下假設變數a為10,變數b為20:
| 運算子 |
描述 |
執行個體 |
| + |
加 - 兩個對象相加 |
a + b 輸出結果 30 |
| - |
減 - 得到負數或是一個數減去另一個數 |
a - b 輸出結果 -10 |
| * |
乘 - 兩個數相乘或是返回一個被重複若干次的字串 |
a * b 輸出結果 200 |
| / |
除 - x除以y |
b / a 輸出結果 2 |
| % |
模數 - 返回除法的餘數 |
b % a 輸出結果 0 |
| ** |
冪 - 返回x的y次冪 |
a**b 輸出結果 20 |
| // |
取整除 - 返回商的整數部分 |
9//2 輸出結果 4 , 9.0//2.0 輸出結果 4.0 |
以下執行個體示範了Python所有算術運算子的操作: #!/usr/bin/python a = 21b = 10c = 0 c = a + bprint "Line 1 - Value of c is ", c c = a - bprint "Line 2 - Value of c is ", c c = a * bprint "Line 3 - Value of c is ", c c = a / bprint "Line 4 - Value of c is ", c c = a % bprint "Line 5 - Value of c is ", c a = 2b = 3c = a**b print "Line 6 - Value of c is ", c a = 10b = 5c = a//b print "Line 7 - Value of c is ", c 以上執行個體輸出結果: Line 1 - Value of c is 31Line 2 - Value of c is 11Line 3 - Value of c is 210Line 4 - Value of c is 2Line 5 - Value of c is 1Line 6 - Value of c is 8Line 7 - Value of c is 2 Python比較子
以下假設變數a為10,變數b為20:
| 運算子 |
描述 |
執行個體 |
| == |
等於 - 比較對象是否相等 |
(a == b) 返回 False。 |
| != |
不等於 - 比較兩個對象是否不相等 |
(a != b) 返回 true. |
| <> |
不等於 - 比較兩個對象是否不相等 |
(a <> b) 返回 true。這個運算子類似 != 。 |
| > |
大於 - 返回x是否大於y |
(a > b) 返回 False。 |
| <</td> |
小於 - 返回x是否小於y。所有比較子返回1表示真,返回0表示假。這分別與特殊的變數True和False等價。注意,這些變數名的大寫。 |
(a < b) 返回 true。 |
| >= |
大於等於 - 返回x是否大於等於y。 |
(a >= b) 返回 False。 |
| <= |
小於等於 - 返回x是否小於等於y。 |
(a <= b) 返回 true。 |
以下執行個體示範了Python所有比較子的操作: #!/usr/bin/python a = 21b = 10c = 0 if ( a == b ): print "Line 1 - a is equal to b"else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b"else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b"else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" if ( a > b ): print "Line 5 - a is greater than b"else: print "Line 5 - a is not greater than b" a = 5;b = 20;if ( a <= b ): print "Line 6 - a is either less than or equal to b"else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b"else: print "Line 7 - b is neither greater than nor equal to b"以上執行個體輸出結果: Line 1 - a is not equal to bLine 2 - a is not equal to bLine 3 - a is not equal to bLine 4 - a is not less than bLine 5 - a is greater than bLine 6 - a is either less than or equal to bLine 7 - b is either greater than or equal to b Python賦值運算子
以下假設變數a為10,變數b為20:
| 運算子 |
描述 |
執行個體 |
| = |
簡單的賦值運算子 |
c = a + b 將 a + b 的運算結果賦值為 c |
| += |
加法賦值運算子 |
c += a 等效於 c = c + a |
| -= |
減法賦值運算子 |
c -= a 等效於 c = c - a |
| *= |
乘法賦值運算子 |
c *= a 等效於 c = c * a |
| /= |
除法賦值運算子 |
c /= a 等效於 c = c / a |
| %= |
模數賦值運算子 |
c %= a 等效於 c = c % a |
| **= |
冪賦值運算子 |
c **= a 等效於 c = c ** a |
| //= |
取整除賦值運算子 |
c //= a 等效於 c = c // a |
以下執行個體示範了Python所有賦值運算子的操作: #!/usr/bin/python a = 21b = 10c = 0 c = a + bprint "Line 1 - Value of c is ", c c += aprint "Line 2 - Value of c is ", c c *= aprint "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2c %= aprint "Line 5 - Value of c is ", c c **= aprint "Line 6 - Value of c is ", c c //= aprint "Line 7 - Value of c is ", c以上執行個體輸出結果: Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864 Python位元運算符
按位元運算符是把數字看作二進位來進行計算的。Python中的按位元運算法則如下:
| 運算子 |
描述 |
執行個體 |
| & |
按位與運算子 |
(a & b) 輸出結果 12 ,二進位解釋: 0000 1100 |
| | |
按位或運算子 |
(a | b) 輸出結果 61 ,二進位解釋: 0011 1101 |
| ^ |
按位異或運算子 |
(a ^ b) 輸出結果 49 ,二進位解釋: 0011 0001 |
| ~ |
按位取反運算子 |
(~a ) 輸出結果 -61 ,二進位解釋: 1100 0011, 在一個有符號位元的補碼形式。 |
| << |
左移動運算子 |
a << 2 輸出結果 240 ,二進位解釋: 1111 0000 |
| >> |
右移動運算子 |
a >> 2 輸出結果 15 ,二進位解釋: 0000 1111 |
以下執行個體示範了Python所有位元運算符的操作: #!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111print "Line 6 - Value of c is ", c以上執行個體輸出結果: Line 1 - Value of c is 12Line 2 - Value of c is 61Line 3 - Value of c is 49Line 4 - Value of c is -61Line 5 - Value of c is 240Line 6 - Value of c is 15 Python邏輯運算子
Python語言支援邏輯運算子,以下假設變數a為10,變數b為20:
| 運算子 |
描述 |
執行個體 |
| and |
布爾"與" - 如果x為False,x and y返回False,否則它返回y的計算值。 |
(a and b) 返回 true。 |
| or |
布爾"或" - 如果x是True,它返回True,否則它返回y的計算值。 |
(a or b) 返回 true。 |
| not |
布爾"非" - 如果x為True,返回False。如果x為False,它返回True。 |
not(a and b) 返回 false。 |
以下執行個體示範了Python所有邏輯運算子的操作: #!/usr/bin/python a = 10b = 20c = 0 if ( a and b ): print "Line 1 - a and b are true"else: print "Line 1 - Either a is not true or b is not true" if ( a or b ): print "Line 2 - Either a is true or b is true or both are true"else: print "Line 2 - Neither a is true nor b is true" a = 0if ( a and b ): print "Line 3 - a and b are true"else: print "Line 3 - Either a is not true or b is not true" if ( a or b ): print "Line 4 - Either a is true or b is true or both are true"else: print "Line 4 - Neither a is true nor b is true" if not( a and b ): print "Line 5 - a and b are true"else: print "Line 5 - Either a is not true or b is not true"以上執行個體輸出結果: Line 1 - a and b are trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - a and b are true Python成員運算子
除了以上的一些運算子之外,Python還支援成員運算子,測試執行個體中包含了一系列的成員,包括字串,列表或元組。
| 運算子 |
描述 |
執行個體 |
| in |
如果在指定的序列中找到值返回True,否則返回False。 |
x 在 y序列中 , 如果x在y序列中返回True。 |
| not in |
如果在指定的序列中沒有找到值返回True,否則返回False。 |
x 不在 y序列中 , 如果x不在y序列中返回True。 |
以下執行個體示範了Python所有成員運算子的操作: #!/usr/bin/python a = 10b = 20list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list"else: print "Line 1 - a is not available in the given list" if ( b not in list ): print "Line 2 - b is not available in the given list"else: print "Line 2 - b is available in the given list" a = 2if ( a in list ): print "Line 3 - a is available in the given list"else: print "Line 3 - a is not available in the given list"以上執行個體輸出結果: Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given list Python身份運算子
身份運算子用於比較兩個對象的儲存單元
| 運算子 |
描述 |
執行個體 |
| is |
is是判斷兩個標識符是不是引用自一個對象 |
x is y, 如果 id(x) 等於 id(y) , is 返回結果 1 |
| is not |
is not是判斷兩個標識符是不是引用自不同對象 |
x is not y, 如果 id(x) 不等於 id(y). is not 返回結果 1 |
以下執行個體示範了Python所有身份運算子的操作: #!/usr/bin/python a = 20b = 20 if ( a is b ): print "Line 1 - a and b have same identity"else: print "Line 1 - a and b do not have same identity" if ( id(a) == id(b) ): print "Line 2 - a and b have same identity"else: print "Line 2 - a and b do not have same identity" b = 30if ( a is b ): print "Line 3 - a and b have same identity"else: print "Line 3 - a and b do not have same identity" if ( a is not b ): print "Line 4 - a and b do not have same identity"else: print "Line 4 - a and b have same identity"以上執行個體輸出結果: Line 1 - a and b have same identityLine 2 - a and b have same identityLine 3 - a and b do not have same identityLine 4 - a and b do not have same identity Python運算子優先順序
以下表格列出了從最高到最低優先順序的所有運算子:
| 運算子 |
描述 |
| ** |
指數 (最高優先順序) |
| ~ + - |
按位翻轉, 一元加號和減號 (最後兩個的方法名為 [email protected] 和 [email protected]) |
| * / % // |
乘,除,模數和取整除 |
| + - |
加法減法 |
| >> << |
右移,左移運算子 |
| & |
位 ‘AND‘ |
| ^ | |
位元運算符 |
| <= < > >= |
比較子 |
| <> == != |
等於運算子 |
| = %= /= //= -= += *= **= |
賦值運算子 |
| is is not |
身份運算子 |
| in not in |
成員運算子 |
| not or and |
邏輯運算子 |
以下執行個體示範了Python所有運算子優先順序的操作: #!/usr/bin/python a = 20b = 10c = 15d = 5e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5print "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 90Value of ((a + b) * c) / d is 90Value of (a + b) * (c / d) is 90Value of a + (b * c) / d is 50
Python算術運算子