運算子 | 描述 | 範例 |
---|---|---|
= |
將操作符的右側運算元賦值給左側的運算元
|
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 |
//= |
這對操作符兩側的運算元進行地板除並賦值給左運算元
|
c //= a 相當於 c = c // a |
#!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("Line 1 - Value of c is ", c) c += a print ("Line 2 - Value of c is ", c ) c *= a print ("Line 3 - Value of c is ", c ) c /= a print ("Line 4 - Value of c is ", c ) c = 2 c %= a print ("Line 5 - Value of c is ", c) c **= a print ("Line 6 - Value of c is ", c) c //= a print ("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.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864