算数运算:
比较运算:
赋值运算:
逻辑运算:
成员运算:
身份运算:
位运算:
#!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 按位与,每一位上两者均为真才为真,即a,b两项上均为1才为1. print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101 按位或,每一位上一项为真即为真,即a,b两项上对应的bit上一个为1即为1. print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 按位异或,每一位上,对应的位上两者皆相同的相同为0,不同为1。即0-0为0,1-1为0,1-0或0-1为1. 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 c = a >> 2; # 15 = 0000 1111 右移动,同上。 print "Line 6 - Value of c is ", c