python3----运算符
1.算術(shù)運(yùn)算符
- 除法運(yùn)算,整數(shù)/整數(shù)=整數(shù),浮點(diǎn)數(shù)/整數(shù)=浮點(diǎn)數(shù),整數(shù)/浮點(diǎn)數(shù)=浮點(diǎn)數(shù):
>>> 17/3
5
>>> 17/3.0
5.666666666666667
>>> 17.0/3
5.666666666666667
>>>
?
- 乘法運(yùn)算,整數(shù)*整數(shù)=整數(shù),浮點(diǎn)數(shù)*整數(shù)=浮點(diǎn)數(shù):
>>> 17*10
170
>>> 17.0*10
170.0
>>> 17.00*10
170.0
>>> 12.3*0.3
3.69
- 加法運(yùn)算,整數(shù)+整數(shù)=整數(shù),整數(shù)+浮點(diǎn)數(shù)=浮點(diǎn)數(shù)
>>> 1+2
3
>>> 1.0+2
3.0
>>> 1.0+2.0
3.0
注意:有時(shí)候,加法運(yùn)算的值可能有一定的誤差,例如:1+1.22并不等于2.22
>>> 1.22+1
2.2199999999999998
>>> 1.23+1
2.23
- 減法運(yùn)算,整數(shù)-整數(shù)=整數(shù),整數(shù)-浮點(diǎn)數(shù)=浮點(diǎn)數(shù),浮點(diǎn)數(shù)-整數(shù)=浮點(diǎn)數(shù):
>>> 10-2
8
>>> 10.0-2
8.0
>>> 10-2.0
8.0
注意:有時(shí)候,減法運(yùn)算的值可能有一點(diǎn)誤差,例如:1.22-0.1并不等于1.12
>>> 1.22-0.1
1.1199999999999999
>>> 1.23-0.1
1.13
- ?Python的%是求模運(yùn)算符(整數(shù)%整數(shù)=余數(shù)):
>>> 5%2
1
>>> 5.4%2
1.4000000000000004
>>> 5%0.2
0.19999999999999973
- 求冪運(yùn)算符:**
>>> 10**2
100
>>> 10**2.0
100.0
- 取整除運(yùn)算符為//, 返回商的整數(shù)部分:
>>> 10//2
5
>>> 10//3
3
>>> 10.0//3
3.0
?
2.邏輯運(yùn)算符
- ?邏輯運(yùn)算符與、或、非,對(duì)應(yīng)的Python符號(hào)為: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
- 移位運(yùn)算符<<和>>,表示將數(shù)的二進(jìn)制比特位向左或向右移動(dòng)幾位:
>>> 4<<2
16
>>> 4>>2
1
>>> 4>>3
0
>>>
>>> 4>>4
0
>>> 4<<32
17179869184L
>>> 4<<64
注:向右無(wú)限移位可以將數(shù)移位為0,向左移位可以使數(shù)無(wú)限增大。 移位運(yùn)算符兩端的數(shù)必須為整數(shù),否則會(huì)報(bào)錯(cuò)
>>> 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'
?
- 按位與、按位或、按位異或、按位翻轉(zhuǎn),對(duì)應(yīng)的Python表示符號(hào)為:&、|、^、~
例子如下:
>>> 8&10
8
>>> 8|10
10
>>> 10^8
2
>>> ~10
-11
>>> ~-12
11
?
轉(zhuǎn)載于:https://www.cnblogs.com/jonm/p/8289268.html
總結(jié)
以上是生活随笔為你收集整理的python3----运算符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 部署 k8s Cluster(下)- 每
- 下一篇: python面向对象进阶