Python 逻辑运算符,not in,in

一、逻辑运算符

and     or   not       

优先级 () > not > and > or

1 print(4 > 3 or 4 < 3 and 1!=1)
2 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
3 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
4 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
5 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
and or not
1 print(4 or 3)
2 print(2 or 3)
3 print(1 or 3)
4 print(0 or 3)
5 print(-1 or 3)
print(x or y), 如果 x 为真,则值为x,否则为y
print(x or y),
如果 x 为真,则值为y,否则为x
1 print(4 and 3)
2 print(2 and 3)
3 print(1 and 3)
4 print(0 and 3)
5 print(-1 and 3)

print(x and y) 和 or 相反
1 print( 2>3 or 3)
2 print( 2>2 or 0)
3 print( 2>3 or 3)

3 > 2  打印 True

 

二、bool 与 int 转换

  bool 转换成 int 只有 0 和1

  int 转换成 bool ,0为False,其他所有为True

三、in         not  in

1 str = 'abcdefghijklmn'
2 print('a' in str)
3 print('bcde' in str)
4 print('bm' in str)
1 comment = input('Please enter comment:')
2 if '台独' in comment:
3     print('Your comments contain illegal characters!')
4 elseprint('Comments increasing success')
原文地址:https://www.cnblogs.com/zhzhlong/p/7701134.html