# Python 位运算符
a = 60 # a的二进制:0011 1100
b = 13 # b的二进制:0000 1101
binary1 = bin(a) # 把数据转为二进制字符串,但是转换过后的字符串会已‘0b’开头
binary2 = bin(b)
binaryStr1 = bin(a).replace('0b', '')
binaryStr2 = bin(b).replace('0b', '')
print(binaryStr1, binaryStr2) # print结果:111100 1101
print("a & b = ", bin(a & b).replace('0b', '')) # 与运算,print结果:a & b = 1100
print("a | b = ", bin(a | b).replace('0b', '')) # 或运算,print结果:a | b = 111101
print("a ^ b = ", bin(a ^ b).replace('0b', '')) # 异或运算,print结果:a ^ b = 110001
print("~a = ", bin(~a).replace('0b', '')) # 按位取反运算,print结果:~a = -111101
# 还有左移,右移运算符
# 左移:二进制向左移动,高位丢弃,低位补0(高位在左边,低位在右边)
# 右移:二进制向右移动
# Python 逻辑运算符
# and,与,两者都为true时返回true
# or, 或,先判断x是否为True,如果为True返回x的值,否则返回y的值
# not,非,如果x为True,则返回False
# 成员运算符 in 、not in
listAraay = [1, 2, 3, 4, 5]
tinyListArray = [1, 2, 3]
if (7 in listAraay):
print("exist")
else:
print("not exist")
if(tinyListArray in listAraay):
print("exist")
else:
print("not exist")
# 身份运算符 is, is not
a = 20
b = 20
c = a + 10
d = c - 10
if (a is d):
print("a = d")
else:
print(" a != d")
# 身份运算符is和==是有区别的,is判断的是否引用了同一个对象
# 而==判断的是指是否相等
# 对于数值来说,只要这两个数值一致,不论是用==判断,还是is判断,结果都是Ture
最后奉上在Python中各种操作符的优先级
这些操作符的优先级了解即可,最好用()按照要计算的顺序括起来,从而增加程序的健壮性,提高Coder容错率