pybool
"""
Boolean represent two values 'True' or 'False'.
"""
if name == 'main':
# When you run a condition in an if statement, Python returns True or False:
a = 200
b = 33
if a > b:
print("b is greater than a")
else:
print("b is not greater than a")
# Evaluate Values and Variables.
'''
The bool() function allows you to evaluate any value, and give you True or False in return
Most of the values are true : string,int(except '0'),list,'True'...
Some Values are false : False,0,"",(),[],{}.
'''
x = 'hello'
y = 15
print(bool(x))
print(bool(y))
# some special object are false.
class subway:
def __len__(self):
return 0
obj = subway()
print(bool(obj))
'''
This phenomenon of the false is not dut to the object itself but it's dut to the len() function return a '0',
if you change it to return a '1', it will become to true.
'''
class car:
def __len__(self):
return 1
obj = car()
print(bool(obj))
# Python also has many built-in functions that return a boolean value, like the isinstance() function,
# which can be used to determine if an object is of a certain data type:
x = 200
print(isinstance(x, int))
作者:yuexiuping
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.