和大多数语言一样,Python也有很多运算符,并且运算符跟其他语言的运算符大同小异接下来一一介绍:
算术运算符:
运算符描述实例
+加 - 两个对象相加a+b的输出结果是30
-减 - 得到复数或者一个数减去另外一个数a-b的输出结果是10
*乘 - 两个数相乘或是返回一个被重复若干次的字符串a*b的结果是200
/除 -x 除以ya/b的结果是2
%取模 - 返回除法的余数b/a的结果是0
..幂- 返回x的y次幂a**b为10的20次方
//取整除 - 返回商的整数部分9//2的结果是4,9.0//2.0的结果是4.0
比较运算符:
运算符描述实例
==等于-比较对象是否相等(a==b)返回False
!=不等于 - 比较两个对象是否不相等(a!=b)返回True
<>不等于 - 比较两个对象是否不相等(与!=等价)(a<>b)返回true,这个运算类似于!=
>大于 - 返回x是否大于y(a>b)返回False
>=大于等于- 返回x是否大于等于y(a>=b)返回False
<=小于等于 - 返回x是否小于等于y(a<=b)返回true
赋值运算符:
运算符描述实例
=简单的赋值运算符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
逻辑运算符:
运算符描述实例
and布尔"与"如果x为False,x and y 返回False,否则它返回y的计算值(a and b)返
回true
or布尔"或",如果x是True,它返回True,否则它返回y的值(a or b)返回true
not in布尔"非",如果x为True,返回False,如果x为False,它发挥Truenot(a and b) 返回false
成员运算:
运算符描述实例
in如果在指定的序列中找到值返回True,否则返回Falsex在y序列中,如果x在y序列中返回True
not in入股在指定的序列中没有找到返回True,否则返回Falsex不在y序列中,如果不在y序列中返回True
身份运算:
运算符描述实例
isis是判断两个标识符是不是引用同一个对象x is y,如果id(x)等于id(y),is返回结果1
is notis not 是判断两个标识符是不是引用不同的对象x is not y,如果id(x)不等于id(y)返回结果1
位运算符:运算符描述实例
&按位与运算符(a&b)输出结果12,二进制解释:0000 1100
|按位或运算符(a|b)输出结果61,二进制解释:0011 1101
^按位异或运算符(a^b)输出结果49,二进制解释:0011 0001
~按位取反运算符(~a)输出结果-61,二进制解释:1100 0011,在一个有符号二进制数的补码形式
<
>>右移运算符a>>2输出结果为15,二进制解释:0000 1111
代码如下:
#!/usr/bin/python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c
c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c
c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c
c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c
c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c
运算符优先级:运算符描述
**指数(最高优先级)
~、+、-按位翻转,一元加号和减号(最后两个方法名为+@he -@)
*、/、%、//乘、除、取模、取整除
+、-加法减法
>>、<
&位"AND"
^位运算符
<=、 <>、 >=比较运算符
<> 、== 、!=等于运算符
=、 %=、 /= 、//= 、-=、 +=、 *=、 **=赋值运算符
is 、is not身份运算符
in 、not in成员运算符
not 、or and逻辑运算符
注释:
在Python中给一个变量赋值用等号("="),如果判断两个变量是否相等或者值是否相等用等号("==")
如下示例代码:#!/usr/bin/python
a = 21 ===========>给变量赋值
b = 10 ===========>给变量赋值
c = 0 ===========>给变量赋值
if ( a == b ):#判断两个变量是否相等,即比较变量值是否相等
print "Line 1 - a is equal to b"
else:
print "Line 1 - a is not equal to b"
if ( a != b ):
print "Line 2 - a is not equal to b"
else:
print "Line 2 - a is equal to b"
if ( a <> b ):
print "Line 3 - a is not equal to b"
else:
print "Line 3 - a is equal to b"
if ( a
print "Line 4 - a is less than b"
else:
print "Line 4 - a is not less than b"
if ( a > b ):
print "Line 5 - a is greater than b"
else:
print "Line 5 - a is not greater than b"
a = 5;
b = 20;
if ( a <= b ):
print "Line 6 - a is either less than or equal to b"
else:
print "Line 6 - a is neither less than nor equal to b"
if ( b >= a ):
print "Line 7 - b is either greater than or equal to b"
else:
print "Line 7 - b is neither greater than nor equal to b"
输出结果:Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b