2022年 11月 3日

Python运算符——布尔运算符

在python的学习中,我们知道了布尔这一特殊的数据类型,那么对于这个特殊的数据类型来说,也有相应的运算,即称之为布尔运算。

以上图中显示出来的即是Python布尔运算中所有的逻辑关系:

and:逻辑与,并且

  1. a,b=1,2
  2. print("-----------and 并且-------")
  3. print(a==1 and b==2)
  4. print(a!=1 and b==2)
  5. print(a==1 and b<2)

or:逻辑或,或者

  1. c,d=10,20
  2. print('-------or 或者----------')
  3. print(c==10 or d!=20)
  4. print(c==10 or d==20)
  5. print(c!=10 or d!=20)

not:对逻辑结果取反

  1. m=True
  2. n=False
  3. print(not m)#not True -> False
  4. print(not n)#not False -> True

in / not in:判断你要的序列是否在指定的序列中,如果是返回True,不是则返回False

  1. print('--------in /not in------')
  2. s='helloworld'
  3. print('h' in s)#True
  4. print('l' in s)#True
  5. print('mm' not in s)#True
  6. print('H' in s)#False