268 lines
7.7 KiB
Python
268 lines
7.7 KiB
Python
# 布尔类型的基本概念
|
||
# 布尔类型只有两个值:True 和 False,用于表示真和假。
|
||
is_sunny = True
|
||
is_raining = False
|
||
print(f"is_sunny:{is_sunny},类型:{type(is_sunny)}") # is_sunny:True,类型:<class 'bool'>
|
||
print(
|
||
f"is_raining:{is_raining},类型:{type(is_raining)}"
|
||
) # is_raining:False,类型:<class 'bool'>
|
||
|
||
# True 和1 是相等
|
||
print(f"True==1:{True==1}") # True==1:True
|
||
# False 和0 是相等
|
||
print(f"False==0:{False==0}") # False==0:True
|
||
# True和True的相加结果
|
||
print(f"True+True:{True + True}") # True+True:2
|
||
# True * 10
|
||
print(f"True*10:{True *10}") # True*10:10
|
||
# # False * 10
|
||
print(f"False*10:{False *10}") # False*10:0
|
||
|
||
# 布尔运算
|
||
# 逻辑运算符用于对布尔值进行逻辑运算,主要要三种
|
||
# and(与) 只有两个操作数都为 True 时,结果才为 True,否则为 False。
|
||
# or(或) 只要有一个操作数为 True,结果就为 True,只有两个都为 False 时,结果才为 False。
|
||
# not(非) 对单个布尔值取反,True 变为 False,False 变为 True。
|
||
|
||
# 逻辑运算
|
||
a, b = True, False
|
||
|
||
print("逻辑运算:")
|
||
print(f"{a} and {b} = {a and b}") # False
|
||
print(f"{a} or {b} = {a or b}") # True
|
||
print(f"not {a} = {not a}") # False
|
||
print(f"not {b} = {not b}") # True
|
||
|
||
# 真值表
|
||
print("\n真值表:")
|
||
print("AND运算:")
|
||
print(f"True and True = {True and True}") # True
|
||
print(f"True and False = {True and False}") # False
|
||
print(f"False and True = {False and True}") # False
|
||
print(f"False and False = {False and False}") # False
|
||
|
||
print("\nOR运算:")
|
||
print(f"True or True = {True or True}") # True
|
||
print(f"True or False = {True or False}") # True
|
||
print(f"False or True = {False or True}") # True
|
||
print(f"False or False = {False or False}") # False
|
||
|
||
print("\nNOT运算:")
|
||
print(f"not True = {not True}") # False
|
||
print(f"not False = {not False}") # True
|
||
|
||
# 逻辑运算符的短路特性
|
||
# 逻辑运算符在运算时具有“短路”特性,也叫“惰性求值”。
|
||
|
||
|
||
# 对于 and 运算符,如果第一个操作数为 False,就不会再计算第二个操作数,结果直接为 False。
|
||
# 对于 or 运算符,如果第一个操作数为 True,就不会再计算第二个操作数,结果直接为 True。
|
||
def expensive_operation():
|
||
print("执行了耗时操作")
|
||
return True
|
||
|
||
|
||
# and
|
||
result1 = False and expensive_operation() # 不执行
|
||
print(result1) # False
|
||
|
||
result2 = True and expensive_operation() # 执行
|
||
print(result2) # True
|
||
|
||
# or
|
||
result3 = True or expensive_operation() # 不执行
|
||
print(result3) # True
|
||
|
||
result4 = False or expensive_operation() # 执行
|
||
print(result4) # True
|
||
|
||
|
||
# 比较运算符
|
||
# 比较运算符
|
||
x, y = 10, 5
|
||
|
||
print("比较运算:")
|
||
print(f"{x} == {y}: {x == y}") # 等于: False
|
||
print(f"{x} != {y}: {x != y}") # 不等于: True
|
||
print(f"{x} > {y}: {x > y}") # 大于: True
|
||
print(f"{x} < {y}: {x < y}") # 小于: False
|
||
print(f"{x} >= {y}: {x >= y}") # 大于等于: True
|
||
print(f"{x} <= {y}: {x <= y}") # 小于等于: False
|
||
|
||
# 字符串比较
|
||
print(f"\n字符串比较:")
|
||
print(f"'apple' == 'apple': {'apple' == 'apple'}") # True
|
||
print(f"'apple' == 'banana': {'apple' == 'banana'}") # False
|
||
print(f"'apple' < 'banana': {'apple' < 'banana'}") # True (按字典序)
|
||
|
||
# 链式比较
|
||
z = 7
|
||
print(f"\n链式比较:")
|
||
print(f"{y} < {z} < {x}: {y < z < x}") # 5 < 7 < 10: True
|
||
print(f"{x} > {z} > {y}: {x > z > y}") # 10 > 7 > 5: True
|
||
print(f"{x} == {z} == {y}: {x == z == y}") # False
|
||
|
||
# 在上下文中很多用于布尔值
|
||
# Python中的假值
|
||
falsy_values = [
|
||
False, # 布尔假
|
||
None, # 空值
|
||
0, # 整数零
|
||
0.0, # 浮点数零
|
||
0j, # 复数零
|
||
"", # 空字符串
|
||
[], # 空列表
|
||
(), # 空元组
|
||
{}, # 空字典
|
||
set(), # 空集合
|
||
]
|
||
|
||
print("假值测试:")
|
||
for value in falsy_values:
|
||
if value:
|
||
print(f"{value!r:10} -> True")
|
||
else:
|
||
print(f"{value!r:10} -> False")
|
||
|
||
# !r是转成字符串
|
||
|
||
# 真值示例
|
||
truthy_values = [
|
||
True, # 布尔真
|
||
1, # 非零数字
|
||
0.1, # 非零浮点数
|
||
"hello", # 非空字符串
|
||
[1, 2, 3], # 非空列表
|
||
(1, 2), # 非空元组
|
||
{"key": "value"}, # 非空字典
|
||
{1, 2, 3}, # 非空集合
|
||
]
|
||
|
||
print("\n真值测试:")
|
||
for value in truthy_values:
|
||
if value:
|
||
print(f"{value!r:20} -> True")
|
||
else:
|
||
print(f"{value!r:20} -> False")
|
||
|
||
# 应用
|
||
# 条件语句
|
||
age = 20
|
||
has_license = True
|
||
has_car = False
|
||
|
||
# if 语句
|
||
if age >= 18:
|
||
print("已成年")
|
||
|
||
# if-else 语句
|
||
if has_license and has_car:
|
||
print("可以开车")
|
||
else:
|
||
print("不能开车")
|
||
|
||
# if-elif-else 语句
|
||
score = 85
|
||
if score >= 90:
|
||
grade = "A"
|
||
elif score >= 80:
|
||
grade = "B"
|
||
elif score >= 70:
|
||
grade = "C"
|
||
else:
|
||
grade = "D"
|
||
print(f"分数 {score} -> 等级 {grade}")
|
||
|
||
# 循环
|
||
# while 循环
|
||
count = 0
|
||
while count < 5:
|
||
print(f"while循环: count = {count}")
|
||
count += 1
|
||
|
||
# break 和 continue
|
||
print("\nbreak和continue演示:")
|
||
for i in range(10):
|
||
if i == 2:
|
||
continue # 跳过本次循环
|
||
if i == 7:
|
||
break # 终止循环
|
||
print(f"i = {i}")
|
||
|
||
|
||
# 布尔函数和操作
|
||
# bool() 函数用于将一个值转换为布尔类型(True 或 False)。在 Python 中,以下情况会被视为 False:
|
||
|
||
|
||
# 数值类型的 0(如 0, 0.0, 0j)
|
||
# 空序列或空集合(如 "", [], (), {})
|
||
# None
|
||
# 自定义对象的 bool 或 len 返回 False 或 0
|
||
# 其他情况都被视为 True。
|
||
# 定义一个函数用于演示bool()函数的用法
|
||
def bool_function():
|
||
# 定义一个包含多种类型测试值的列表
|
||
test_values = [0, 1, -1, 0.0, 0.1, "", "hello", [], [1], None]
|
||
|
||
# 打印测试标题
|
||
print("bool()函数测试:")
|
||
# 遍历每个测试值
|
||
for value in test_values:
|
||
# 打印每个值及其对应的bool()结果
|
||
print(f"bool({value!r:10}) = {bool(value)}")
|
||
|
||
|
||
# 调用上面定义的函数
|
||
bool_function()
|
||
|
||
|
||
# 定义一个银行账户类,用于演示自定义对象的布尔值
|
||
class BankAccount:
|
||
# 初始化方法,设置账户余额
|
||
def __init__(self, balance):
|
||
self.balance = balance
|
||
|
||
# 定义__bool__方法,余额大于0时对象为True,否则为False
|
||
def __bool__(self):
|
||
return self.balance > 0
|
||
|
||
# 定义__str__方法,返回账户余额的字符串表示
|
||
def __str__(self):
|
||
return f"BankAccount(余额: ${self.balance})"
|
||
|
||
|
||
# 打印自定义对象布尔值的测试标题
|
||
print("\n自定义对象布尔值:")
|
||
# 创建一个余额为100的账户对象
|
||
account1 = BankAccount(100)
|
||
# 创建一个余额为0的账户对象
|
||
account2 = BankAccount(0)
|
||
|
||
# 打印第一个账户对象及其bool()结果
|
||
print(f"{account1}: bool() = {bool(account1)}")
|
||
# 打印第二个账户对象及其bool()结果
|
||
print(f"{account2}: bool() = {bool(account2)}")
|
||
|
||
# any()和all()函数
|
||
# any() 和 all() 是 Python 内置的两个常用函数,用于判断可迭代对象中的元素布尔值。
|
||
# any(iterable): 只要 iterable 中有一个元素为 True(即"真值"),就返回 True;如果所有元素都为 False,则返回 False。
|
||
# all(iterable): 只要 iterable 中有一个元素为 False(即"假值"),就返回 False;只有所有元素都为 True 时才返回 True。
|
||
|
||
# any() 任意一个为True则为True
|
||
print(
|
||
f"any([False,False,False]):{any([False,False,False])}"
|
||
) # any([False,False,False]):False
|
||
print(
|
||
f"any([False,True,False]):{any([False,True,False])}"
|
||
) # any([False,True,False]):True
|
||
print(f"any([1,0,1,0]):{any([1,0,1,0])}") # any([1,0,1,0]):True
|
||
|
||
# all() 所有是true才为true
|
||
print(f"all([True, True, True]): {all([True, True, True])}") # True
|
||
print(f"all([True, False, True]): {all([True, False, True])}") # False
|
||
print(f"all([1, 1, 0]): {all([1, 1, 0])}") # False
|
||
|
||
numbers = [2, 4, 6, 8]
|
||
print(all(n % 2 == 0 for n in numbers)) # True
|
||
print(any(n % 2 != 0 for n in numbers)) # False
|