python运算符,,python中的运算
python运算符,,python中的运算
python中的运算符包括多种多样的,可以分为以下几个大类
第一种:算数运算
第二种:比较运算
第三种:赋值运算
第四种:逻辑运算
1,优先级
在没有()的情况下not 优先级高于and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。例题:判断下列逻辑语句的True,False。or中有一个为真,则为Trueand中有一个为假,则为false3>4 or 4<3 and 1==1 True1 < 2 and 3 < 4 or 1>2 True2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 True1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 False1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 Falsenot 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 <6 Falseprint(1>2 and 3 or 4 and 3 < 2) == 0
2 ,or 和and
x or y , x为真,值就是x,x为假,值是y;非0转换为布尔值为True,0转换为布尔值为False,除了0都显示前一位x and y, x为真,值是y,x为假,值是x。除了0,显示都是后一位print(int(True)) == 1print(int(False)) ==08 or 4 80 and 3 30 or 4 and 3 or 7 or 9 and 6 3print(2 or 1 < 3) == 2print(2 or 1 < 3 and 2) == 2
第五种:成员运算
in,not in :判断子元素是否在原字符串(字典,列表,集合)中:例如:#print(‘喜欢‘ in ‘dkfljadklf喜欢hfjdkas‘)#print(‘a‘ in ‘bcvd‘)#print(‘y‘ not in ‘ofkjdslaf‘)
第六种:身份运算
第七种:位运算
a = 60 # 60 = 0011 1100b = 13 # 13 = 0000 1101c = 0 c = a & b; # 12 = 0000 1100print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 #相同为0,不同为1print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111print "Line 6 - Value of c is ", c
总结运算符的优先级
python运算符
评论关闭