#基本运算符
#and or not
#优先级 ()>not>and>or
#and or not print(2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2) # True or True or False #True
print(3>4 or 4<3 and 1==1) #False print(1<2 and 3<4 or 1>2) #True print(2>1 and 3<4 or 4>5 and 2<1) #True print(1>2 and 3<4 or 4>5 and 2>1 or 9<8) #False print(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #False print(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #False
数字与布尔值转换
#int-->bool print(bool(2)) #True print(bool(-2)) #True print(bool(0)) #False #bool-->int print(int(True)) #1 print(int(False)) #0
x or y , x为真,值就是x,x为假,值是y
print(1 or 2) #1 print(3 or 2) #3 print(0 or 2) #2 print(0 or 100) #100
x and y, x为真,值是y,x为假,值是x
print(1 and 2) #2 print(0 and 2) #0
in,not in :
判断子元素是否在原字符串(字典,列表,集合)中:
print('喜欢' in 'dkfljadklf喜欢hfjdkas') print('a' in 'bcvd') print('y' not in 'ofkjdslaf')
#and or 组合
print(2 or 100 or 3 or 4) #2 print(0 or 4 and 3 or 2) #3
#综合 第一个为布尔型则返回布尔型,第一个为数值型,则返回数值型
print(1>2 and 3 or 4 and 3<2) #False print(2 or 1<3 and 2) #2
print(0 or 5<4) #False print(0 and 3>1) #0 print(3>1 and 0) #0 print(3>1 and 2 or 2<3 and 3 and 4 or 3>2) #2