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))
posted @ 2022-10-10 20:38  yuexiuping  阅读(24)  评论(0编辑  收藏  举报