Python运算符
扫描二维码
随时随地手机看文章
Python运算符
Python中运算符需要特殊注意的,取整型运算符“//”,和除运算符“/”,两者的区别在于如果是浮点数相除,前者是整除的数值,后者是精确地值。
逻辑运算符中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是单个的数值
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
输出结果:
加和赋值运算符 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
上述标有红色的进行特别说明,当俩个值为数值时分别赋相同的值,进行身份验证时True,当俩个值为列表时身份验证时为False
欢迎提出问题,指出不足,一起学习一起进步!