的bool()在python返回提供给它的参数的一个布尔值。该参数可以是以下任意值,并且结果符合以下条件。除此处提到的值外,其余值返回True。
当传递的参数值如下时,返回False:没有
错误条件
任何数字类型的零
空序列(),[]等
空映射,如{}
具有__bool __()或__len()__方法且返回0或False的类的对象
示例
在下面的程序中,我们说明了所有这样的示例场景。print("None gives : ",bool(None))
print("True gives : ",bool(True))
print("Zero gives: ",bool(0))
# Expression evaluating to true
print("Expression evaluating to True: ",bool(0 == (18/3)))
# Expression evaluating to false
print("Expression evaluating to False: ",bool(0 == (18%3)))
s = ()
print("An mpty sequence: ",bool(s))
m = {}
print("An emty mapping: ",bool(m))
t = 'Tutoriaslpoint'
print("A non empty string: ",bool(t))
输出结果
运行上面的代码给我们以下结果-None gives : False
True gives : True
Zero gives: False
Expression evaluating to True: False
Expression evaluating to False: True
An mpty sequence: False
An emty mapping: False
A non empty string: True