布尔类型的值和类型
a = True
b = False
print(type(a)) #
print(type(b)) #
布尔类型的整数表现
print(int(True)) # 1
print(int(False)) # 0
使用 bool() 函数进行转换
print(bool(0)) # False
print(bool(42)) # True
print(bool('')) # False
print(bool('Python')) # True
print(bool([])) # False
print(bool([1, 2, 3])) # True
布尔逻辑运算
print(True and False) # False
print(True or False) # True
print(not True) # False
布尔比较运算
print(5 > 3) # True
print(2 == 2) # True
print(7 < 4) # False
布尔值在控制流中的应用
if True:
print("This will always print")
if not False:
print("This will also always print")
x = 10
if x:
print("x is non-zero and thus True in a boolean context")