Python教程

Python——bool值(and、or、not、in、not in)

本文主要是介绍Python——bool值(and、or、not、in、not in),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

and

a,b=1,2
print('--------and(并且)-----------')
print(a==1 and b==2)  #True  True and True -->True
print(a==1 and b<2)  #False Ture and False-->False
#总结:只要有一个False,结果为False 。只有当全为True时,结果才为True

or

a, b = 1, 2
print('----------or(或者)----------------')
print(a==1 or b==2) #True or True -->True
print(a==1 or b<2)  #True or False -->True
print(a!=1 or b!=1) #False or False -->False
#总结:只要有一个True,结果为True。只有当全为False时,结果才为False

not

print('------------not(对bool类型操作数取反)---------------')
f1=True
f2=False
print(not f1)  #Flase
print(not f2)  #True

in   &   not   in

print('---------in与 not in------------')
s='helloworld'
print('w' in s)         #True
print('k' in s)         #False
print('k' not in s)     #True
print('k' not in s)     #True

这篇关于Python——bool值(and、or、not、in、not in)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!