什麼是操作符?
簡單的回答可以使用表達式4 + 5等於9,在這裏4和5被稱爲操作數,+被稱爲操符。 Python語言支持操作者有以下幾種類型。
算術運算符
比較(即關係)運算符
賦值運算符
邏輯運算符
位運算符
會員操作符
標識操作符
讓我們逐一看看所有的運算符。
Python算術運算符:
假設變量a持有10和變量b持有20,則:
操作符 | 描述符 | 例子 |
---|---|---|
+ | 加法 - 對操作符的兩側增加值 | a + b = 30 |
- | 減法 - 減去從左側操作數右側操作數 | a - b = -10 |
* | 乘法 - 相乘的運算符兩側的值 | a * b = 200 |
/ | 除 - 由右側操作數除以左側操作數 | b / a = 2 |
% | 模 - 由右側操作數和餘返回除以左側操作數 | b % a = 0 |
** | 指數- 執行對操作指數(冪)的計算 | a**b = 10 的冪 20 |
// | 地板除 - 操作數的除法,其中結果是將小數點後的位數被除去的商。 | 9//2 = 4 而 9.0//2.0 = 4.0 |
#!/usr/bin/pythona = 21b = 10c = 0c = 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 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 8 Line 7 - Value of c is 2
Python的比較操作符:
假設變量a持有10和變量b持有20,則:
運算符 | 描述 | 示例 |
---|---|---|
== | 檢查,兩個操作數的值是否相等,如果是則條件變爲真。 | (a == b) 不爲 true. |
!= | 檢查兩個操作數的值是否相等,如果值不相等,則條件變爲真。 | (a != b) 爲 true. |
<> | 檢查兩個操作數的值是否相等,如果值不相等,則條件變爲真。 | (a <> b) 爲 true。這個類似於 != 運算符 |
> | 檢查左操作數的值是否大於右操作數的值,如果是,則條件成立。 | (a > b) 不爲 true. |
< | 檢查左操作數的值是否小於右操作數的值,如果是,則條件成立。 | (a < b) 爲 true. |
>= | 檢查左操作數的值是否大於或等於右操作數的值,如果是,則條件成立。 | (a >= b) 不爲 true. |
<= | 檢查左操作數的值是否小於或等於右操作數的值,如果是,則條件成立。 | (a <= b) 爲 true. |
#!/usr/bin/pythona = 21b = 10c = 0if ( 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 b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b
Python賦值運算符:
假設變量持有10和變量b持有20,則:
運算符 | 描述 | 示例 |
---|---|---|
= | 簡單的賦值運算符,賦值從右側操作數左側操作數 | c = a + b將指定的值 a + b 到 c |
+= | 加法AND賦值操作符,它增加了右操作數左操作數和結果賦給左操作數 | c += a 相當於 c = c + a |
-= | 減AND賦值操作符,它減去右邊的操作數從左邊操作數,並將結果賦給左操作數 | c -= a 相當於 c = c - a |
*= | 乘法AND賦值操作符,它乘以右邊的操作數與左操作數,並將結果賦給左操作數 | c *= a 相當於 c = c * a |
/= | 除法AND賦值操作符,它把左操作數與正確的操作數,並將結果賦給左操作數 | c /= a 相當於= c / a |
%= | 模量AND賦值操作符,它需要使用兩個操作數的模量和分配結果左操作數 | c %= a is equivalent to c = c % a |
**= | 指數AND賦值運算符,執行指數(功率)計算操作符和賦值給左操作數 | c **= a 相當於 c = c ** a |
//= | 地板除,並分配一個值,執行地板除對操作和賦值給左操作數 | c //= a 相當於 c = c // a |
#!/usr/bin/pythona = 21b = 10c = 0c = 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 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
Python位運算符:
位運算符作用於位和位操作執行位。假設,如果a =60;且b =13;現在以二進制格式它們將如下:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Python語言支持下位運算符
操作符 | 描述 | 示例 |
---|---|---|
& | 二進制和複製操作了一下,結果,如果它存在於兩個操作數。 | (a & b) = 12 即 0000 1100 |
| | 二進制或複製操作了一個比特,如果它存在一個操作數中。 | (a | b) = 61 即 0011 1101 |
^ | 二進制異或運算符的副本,如果它被設置在一個操作數而不是兩個比特。 | (a ^ b) = 49 即 0011 0001 |
~ | 二進制的補運算符是一元的,並有“翻轉”位的效果。 | (~a ) = -61 即 1100 0011以2的補碼形式由於帶符號二進制數。 |
<< | 二進位向左移位運算符。左操作數的值左移由右操作數指定的位數。 | a << 2 = 240 即 1111 0000 |
>> | 二進位向右移位運算符。左操作數的值是由右操作數指定的位數向右移動。 | a >> 2 = 15 即 0000 1111 |
#!/usr/bin/pythona = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0c = 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 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15
Python邏輯運算符:
Python語言支持以下邏輯運算符。假設變量a持有10和變量b持有20則:
運算符 | 描述 | 示例 |
---|---|---|
and | 所謂邏輯與運算符。如果兩個操作數都是真的,那麼則條件成立。 | (a and b) 爲 true. |
or | 所謂邏輯OR運算符。如果有兩個操作數都是非零然後再條件變爲真。 | (a or b) 爲 true. |
not | 所謂邏輯非運算符。用於反轉操作數的邏輯狀態。如果一個條件爲真,則邏輯非運算符將返回false。 | not(a and b) 爲 false. |
#!/usr/bin/pythona = 10b = 20c = 0if ( 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 - Either a is not true or b is not true"else: print "Line 5 - a and b are true"
當執行上面的程序它會產生以下結果:
Line 1 - a and b are true Line 2 - Either a is true or b is true or both are true Line 3 - Either a is not true or b is not true Line 4 - Either a is true or b is true or both are true Line 5 - Either a is not true or b is not true
Python成員運算符:
除了前面討論的運算符,Python成員運算符,在一個序列中成員資格的測試,如字符串,列表或元組。有兩個成員運算符解釋如下:
操作符 | 描述 | 示例 |
---|---|---|
in | 計算結果爲true,如果它在指定找到變量的順序,否則false。 | x在y中,在這裏產生一個1,如果x是序列y的成員。 |
not in | 計算結果爲true,如果它不找到在指定的變量順序,否則爲false。 | x不在y中,這裏產生結果不爲1,如果x不是序列y的成員。 |
#!/usr/bin/pythona = 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 list Line 2 - b is not available in the given list Line 3 - a is available in the given list
Python標識運算符:
標識符比較兩個對象的內存位置。兩個運算符標識解釋如下:
運算符 | 描述 | 例子 |
---|---|---|
is | 計算結果爲true,如果操作符兩側的變量指向相同的對象,否則爲false。 | x是y,這裏結果是1,如果id(x)的值爲id(y)。 |
is not | 計算結果爲false,如果兩側的變量操作符指向相同的對象,否則爲true。 | x不爲y,這裏結果不是1,當id(x)不等於id(y)。 |
#!/usr/bin/pythona = 20b = 20if ( 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 identity Line 2 - a and b have same identity Line 3 - a and b do not have same identity Line 4 - a and b do not have same identity
Python運算符優先級
下表列出了所有運算符從最高優先級到最低。
運算符 | 描述 |
---|---|
** | 冪(提高到指數) |
~ + - | 補碼,一元加號和減號(方法名的最後兩個+@和 - @) |
* / % // | 乘,除,取模和地板除 |
+ - | 加法和減法 |
>> << | 左,右按位轉移 |
& | 位'AND' |
^ | | 按位異'或`'和定期`或' |
<= < > >= | 比較運算符 |
<> == != | 等式運算符 |
= %= /= //= -= += *= **= | 賦值運算符 |
is is not | 標識運算符 |
in not in | 成員運算符 |
not or and | 邏輯運算符 |
#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (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 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