Python運算符
Python中運算符需要特殊注意的,取整型運算符“//”,和除運算符“/”,兩者的區(qū)別在于如果是浮點數(shù)相除,前者是整除的數(shù)值,后者是精確地值。
邏輯運算符中and和or和C中的&&和||類似。另外不等于運算符“<>”返回值為True(不同)和False(相同)。
#Pyton運算符
a = 21.0
b = 10.0
c = 0
c = a + b
print "加和賦值運算符 c is ", c
c = a - b
print "減和賦值運算符c is ", c?
c = a * b
print "乘和賦值運算符c is ", c?
c = a / b
print "除和賦值運算符c is ", c?
c = a % b
print "取模和賦值運算符c is ", c
a = 2
b = 3
c = a**b?
print "冪和賦值運算符c is ", c
a = 10
b = 5
c = a//b?
print "取整除和賦值運算符c is ", c
#判斷a和b是否不相等,不相等為True,相等為False
c= a<>b
print "比較不等和賦值運算符c is ", c
#Python邏輯運算符
c=0
if not a:
? ? print "a and c :",(a and c)
? ? print"c and a :",(a and c)
? ? print "a or c:",(a or c)
? ? print "c or a:",(c or a)
else:
? ? print "a,b,c:",a,b,c
? ? print "a and c :",(a and c)
? ? print"c and a :",(c and a)
? ? print "a or c:",(a or c)
? ? print "c or a:",(c or a)
? ? print "a and b :",(a and b)
? ? print"b and a :",(b and a)
? ? print "a or b:",(a or b)
? ? print "b or a:",(b or a)
#成員運算符
list = [1, 2, 3, 4, 5 ];
if b in list:
? ? print "b in list:",b in list
if a in list:
? ? print "a not in list:",a not in list
print "a not in list:",a in list
#身份運算符
#a和b是列表
a=[10]
b=[10]
c=d=10
if a is b:
? ? print "a is b:",a is b
else:
? ? print "a is b:",a is b ? ?
if c is d:
? ? print "c is d:",c is d ?
#a和b是單個的數(shù)值 ? ?
a=10
b=10
c=d=10
if a is b:
? ? print "a is b:",a is b
else:
? ? print "a is b:",a is b ? ?
if c is d:
? ? print "c is d:",c is d?
輸出結(jié)果:
加和賦值運算符 c is ?31.0
減和賦值運算符c is ?11.0
乘和賦值運算符c is ?210.0
除和賦值運算符c is ?2.1
取模和賦值運算符c is ?1.0
冪和賦值運算符c is ?8
取整除和賦值運算符c is ?2
比較不等和賦值運算符c is ?True
a,b,c: 10 5 0
a and c : 0
c and a : 0
a or c: 10
c or a: 10
a and b : 5
b and a : 10
a or b: 10
b or a: 5
b in list: True
a not in list: False
a is b: False
c is d: True
a is b: True
c is d: True
上述標有紅色的進行特別說明,當倆個值為數(shù)值時分別賦相同的值,進行身份驗證時True,當倆個值為列表時身份驗證時為False
歡迎提出問題,指出不足,一起學習一起進步!