python3----运算符
1.算術運算符
- 除法運算,整數/整數=整數,浮點數/整數=浮點數,整數/浮點數=浮點數:
>>> 17/3
5
>>> 17/3.0
5.666666666666667
>>> 17.0/3
5.666666666666667
>>>
?
- 乘法運算,整數*整數=整數,浮點數*整數=浮點數:
>>> 17*10
170
>>> 17.0*10
170.0
>>> 17.00*10
170.0
>>> 12.3*0.3
3.69
- 加法運算,整數+整數=整數,整數+浮點數=浮點數
>>> 1+2
3
>>> 1.0+2
3.0
>>> 1.0+2.0
3.0
注意:有時候,加法運算的值可能有一定的誤差,例如:1+1.22并不等于2.22
>>> 1.22+1
2.2199999999999998
>>> 1.23+1
2.23
- 減法運算,整數-整數=整數,整數-浮點數=浮點數,浮點數-整數=浮點數:
>>> 10-2
8
>>> 10.0-2
8.0
>>> 10-2.0
8.0
注意:有時候,減法運算的值可能有一點誤差,例如:1.22-0.1并不等于1.12
>>> 1.22-0.1
1.1199999999999999
>>> 1.23-0.1
1.13
- ?Python的%是求模運算符(整數%整數=余數):
>>> 5%2
1
>>> 5.4%2
1.4000000000000004
>>> 5%0.2
0.19999999999999973
- 求冪運算符:**
>>> 10**2
100
>>> 10**2.0
100.0
- 取整除運算符為//, 返回商的整數部分:
>>> 10//2
5
>>> 10//3
3
>>> 10.0//3
3.0
?
2.邏輯運算符
- ?邏輯運算符與、或、非,對應的Python符號為:and 、or、not
? ? ? ? ? ?
>>> False and True
False
>>> True and True
True
>>> False and False
False
>>> False or True
True
>>> True or True
True
>>> False or False
False
>>> not True
False
>>> not False
True
- 移位運算符<<和>>,表示將數的二進制比特位向左或向右移動幾位:
>>> 4<<2
16
>>> 4>>2
1
>>> 4>>3
0
>>>
>>> 4>>4
0
>>> 4<<32
17179869184L
>>> 4<<64
注:向右無限移位可以將數移位為0,向左移位可以使數無限增大。 移位運算符兩端的數必須為整數,否則會報錯
>>> 0.2>>2
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
0.2>>2
TypeError: unsupported operand type(s) for >>: 'float' and 'int'
>>> 2>>0.1
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
2>>0.1
TypeError: unsupported operand type(s) for >>: 'int' and 'float'
?
- 按位與、按位或、按位異或、按位翻轉,對應的Python表示符號為:&、|、^、~
例子如下:
>>> 8&10
8
>>> 8|10
10
>>> 10^8
2
>>> ~10
-11
>>> ~-12
11
?
轉載于:https://www.cnblogs.com/jonm/p/8289268.html
總結
以上是生活随笔為你收集整理的python3----运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 部署 k8s Cluster(下)- 每
- 下一篇: redux中的小bug