布尔类型基础
Python中的布尔类型只有两个值:True和False。理解布尔逻辑是编写条件语句和逻辑判断的基础。
1. 布尔值的基本概念
# 布尔值的基本使用
is_student = True
is_working = False
print(f"是学生: {is_student}")
print(f"在工作: {is_working}")
print(f"布尔值类型: {type(is_student)}")
# 布尔值转换
print(f"数字1的布尔值: {bool(1)}")
print(f"数字0的布尔值: {bool(0)}")
print(f"空字符串的布尔值: {bool('')}")
print(f"非空字符串的布尔值: {bool('hello')}")
真值判定(Truthiness)
Python中所有对象都有真值性,在条件判断中会被转换为布尔值。
1. 假值(Falsy Values)
# 以下值在条件判断中均为False
falsy_values = [
0, # 数字0
0.0, # 浮点数0
0j, # 复数0
'', # 空字符串
[], # 空列表
{}, # 空字典
set(), # 空集合
None, # None值
False # 布尔False
]
print("假值测试:")
for value in falsy_values:
print(f"{repr(value):>8} -> {bool(value)}")
2. 真值(Truthy Values)
# 除了假值之外的所有值都是真值
truthy_values = [
1, # 非零数字
-1, # 负数
3.14, # 浮点数
'hello', # 非空字符串
[1, 2, 3], # 非空列表
{'a': 1}, # 非空字典
{1, 2, 3}, # 非空集合
True # 布尔True
]
print("真值测试:")
for value in truthy_values:
print(f"{repr(value):>10} -> {bool(value)}")
3. 自定义对象的真值判定
class CustomObject:
def __init__(self, value):
self.value = value
def __bool__(self):
"""自定义布尔值判定"""
return bool(self.value)
def __len__(self):
"""定义长度,影响真值判定"""
return len(str(self.value))
# 测试自定义对象的真值判定
obj1 = CustomObject(0) # 假值
obj2 = CustomObject(42) # 真值
obj3 = CustomObject("") # 假值
obj4 = CustomObject("hello") # 真值
print(f"CustomObject(0): {bool(obj1)}")
print(f"CustomObject(42): {bool(obj2)}")
print(f"CustomObject(''): {bool(obj3)}")
print(f"CustomObject('hello'): {bool(obj4)}")
逻辑运算符
1. 基本逻辑运算符
# and 运算符:两个条件都为真时返回最后一个真值
result1 = True and True # True
result2 = True and False # False
result3 = False and True # False
result4 = False and False # False
print(f"True and True: {result1}")
print(f"True and False: {result2}")
print(f"False and True: {result3}")
print(f"False and False: {result4}")
# or 运算符:至少一个条件为真时返回第一个真值
result5 = True or True # True
result6 = True or False # True
result7 = False or True # True
result8 = False or False # False
print(f"True or True: {result5}")
print(f"True or False: {result6}")
print(f"False or True: {result7}")
print(f"False or False: {result8}")
# not 运算符:取反
result9 = not True # False
result10 = not False # True
print(f"not True: {result9}")
print(f"not False: {result10}")
2. 短路逻辑(Short-circuit Evaluation)
Python的逻辑运算符具有短路特性,即一旦确定结果就不再计算后续表达式。
def expensive_function():
"""模拟耗时函数"""
print("执行耗时函数...")
return True
def safe_division(a, b):
"""安全除法,利用短路逻辑避免除零错误"""
return b != 0 and a / b
# 短路逻辑示例
print("短路逻辑测试:")
# and 短路:第一个为假时,不执行第二个
result1 = False and expensive_function() # 不会打印"执行耗时函数..."
print(f"False and expensive_function(): {result1}")
# or 短路:第一个为真时,不执行第二个
result2 = True or expensive_function() # 不会打印"执行耗时函数..."
print(f"True or expensive_function(): {result2}")
# 安全除法示例
print(f"10 / 2 = {safe_division(10, 2)}")
print(f"10 / 0 = {safe_division(10, 0)}") # 返回False,不会除零
逻辑运算的实际应用
1. 默认值设置
def get_user_name(user_id, users_dict):
"""获取用户名,如果不存在则返回默认值"""
return users_dict.get(user_id) or "未知用户"
def get_config_value(key, config_dict, default=None):
"""获取配置值,支持嵌套键"""
return config_dict.get(key) or default
# 使用示例
users = {1: "张三", 2: "李四"}
config = {"debug": True, "timeout": 30}
print(f"用户1: {get_user_name(1, users)}")
print(f"用户3: {get_user_name(3, users)}") # 返回默认值
print(f"调试模式: {get_config_value('debug', config, False)}")
print(f"端口号: {get_config_value('port', config, 8080)}")
2. 条件判断优化
def validate_user(user):
"""验证用户信息,利用短路逻辑优化"""
# 检查用户是否存在
if not user:
return False, "用户不存在"
# 检查用户名是否有效
if not user.get('name') or not user['name'].strip():
return False, "用户名不能为空"
# 检查年龄是否有效
age = user.get('age')
if not age or not isinstance(age, int) or age < 0:
return False, "年龄必须是正整数"
return True, "验证通过"
# 测试用户验证
test_users = [
None, # 用户不存在
{}, # 空用户
{"name": ""}, # 空用户名
{"name": " "}, # 空白用户名
{"name": "张三", "age": -1}, # 负年龄
{"name": "张三", "age": "25"}, # 非数字年龄
{"name": "张三", "age": 25} # 有效用户
]
for user in test_users:
is_valid, message = validate_user(user)
print(f"用户: {user} -> {message}")
any() 和 all() 函数
1. any() 函数
any()函数检查可迭代对象中是否至少有一个真值。
def test_any_function():
"""测试any函数的使用"""
# 基本使用
numbers = [0, 1, 2, 3]
print(f"any([0, 1, 2, 3]): {any(numbers)}") # True
# 全假值
falsy_list = [0, '', [], None]
print(f"any([0, '', [], None]): {any(falsy_list)}") # False
# 空列表
empty_list = []
print(f"any([]): {any(empty_list)}") # False
# 实际应用:检查列表中是否有正数
def has_positive(numbers):
return any(x > 0 for x in numbers)
test_cases = [
[1, 2, 3], # 有正数
[-1, -2, -3], # 无正数
[0, 0, 0], # 无正数
[] # 空列表
]
for case in test_cases:
print(f"列表 {case} 是否有正数: {has_positive(case)}")
test_any_function()
2. all() 函数
all()函数检查可迭代对象中是否所有值都为真值。
def test_all_function():
"""测试all函数的使用"""
# 基本使用
numbers = [1, 2, 3, 4]
print(f"all([1, 2, 3, 4]): {all(numbers)}") # True
# 包含假值
mixed_list = [1, 2, 0, 4]
print(f"all([1, 2, 0, 4]): {all(mixed_list)}") # False
# 空列表
empty_list = []
print(f"all([]): {all(empty_list)}") # True(空列表的特殊情况)
# 实际应用:检查所有数字是否为正数
def all_positive(numbers):
return all(x > 0 for x in numbers)
test_cases = [
[1, 2, 3], # 全正数
[1, 2, 0], # 包含0
[-1, 2, 3], # 包含负数
[] # 空列表
]
for case in test_cases:
print(f"列表 {case} 是否全为正数: {all_positive(case)}")
test_all_function()
3. any() 和 all() 的高级应用
def advanced_any_all_examples():
"""any和all的高级应用示例"""
# 1. 检查字符串列表是否包含空字符串
def has_empty_string(strings):
return any(s == '' for s in strings)
# 2. 检查所有字符串是否非空
def all_non_empty_strings(strings):
return all(s.strip() for s in strings)
# 3. 检查数字列表是否满足条件
def check_numbers(numbers):
has_even = any(x % 2 == 0 for x in numbers)
all_positive = all(x > 0 for x in numbers)
return has_even, all_positive
# 测试示例
string_lists = [
["hello", "world", "python"],
["hello", "", "world"],
["", "", ""],
[]
]
print("字符串列表检查:")
for strings in string_lists:
has_empty = has_empty_string(strings)
all_non_empty = all_non_empty_strings(strings)
print(f"列表: {strings}")
print(f" 包含空字符串: {has_empty}")
print(f" 全为非空字符串: {all_non_empty}")
print()
# 数字列表检查
number_lists = [
[1, 2, 3, 4],
[1, 3, 5, 7],
[2, 4, 6, 8],
[-1, 2, 3, 4]
]
print("数字列表检查:")
for numbers in number_lists:
has_even, all_positive = check_numbers(numbers)
print(f"列表: {numbers}")
print(f" 包含偶数: {has_even}")
print(f" 全为正数: {all_positive}")
print()
advanced_any_all_examples()
条件表达式(三元运算符)
1. 基本语法
def conditional_expressions():
"""条件表达式示例"""
# 基本语法:值1 if 条件 else 值2
age = 20
status = "成年人" if age >= 18 else "未成年人"
print(f"年龄 {age} 岁,状态: {status}")
# 嵌套条件表达式
score = 85
grade = "优秀" if score >= 90 else "良好" if score >= 80 else "一般"
print(f"分数 {score},等级: {grade}")
# 与逻辑运算符结合
name = ""
display_name = name or "匿名用户"
print(f"显示名称: {display_name}")
conditional_expressions()
2. 条件表达式的实际应用
def practical_conditional_examples():
"""条件表达式的实际应用"""
# 1. 数据验证
def validate_age(age):
return "有效" if isinstance(age, int) and 0 <= age <= 150 else "无效"
# 2. 配置设置
def get_config_value(key, config, default):
return config.get(key) if key in config else default
# 3. 格式化输出
def format_number(num):
return f"{num:,}" if isinstance(num, (int, float)) else "无效数字"
# 测试示例
ages = [25, -5, 200, "25", None]
print("年龄验证:")
for age in ages:
print(f"年龄 {age}: {validate_age(age)}")
config = {"debug": True, "timeout": 30}
print("\n配置获取:")
print(f"debug: {get_config_value('debug', config, False)}")
print(f"port: {get_config_value('port', config, 8080)}")
numbers = [1234, 5678.9, "hello", None]
print("\n数字格式化:")
for num in numbers:
print(f"{num} -> {format_number(num)}")
practical_conditional_examples()
布尔运算的最佳实践
1. 可读性优化
def readability_best_practices():
"""布尔运算的可读性最佳实践"""
# 好的做法:使用有意义的变量名
def is_valid_user(user):
has_name = user and user.get('name')
has_valid_age = user and user.get('age', 0) > 0
return has_name and has_valid_age
# 避免的做法:复杂的嵌套条件
def is_valid_user_bad(user):
return user and user.get('name') and user.get('age', 0) > 0
# 测试
test_users = [
{"name": "张三", "age": 25},
{"name": "", "age": 25},
{"name": "李四", "age": 0},
None
]
print("用户验证结果:")
for user in test_users:
print(f"用户: {user} -> 有效: {is_valid_user(user)}")
readability_best_practices()
2. 性能优化
def performance_optimization():
"""布尔运算的性能优化"""
# 好的做法:利用短路逻辑
def safe_get_value(data, key, default=None):
return data and data.get(key) or default
# 更好的做法:使用get方法的默认值
def better_safe_get_value(data, key, default=None):
return data.get(key, default) if data else default
# 测试
test_data = {"name": "张三", "age": 25}
empty_data = {}
none_data = None
print("安全获取值:")
print(f"有效数据: {safe_get_value(test_data, 'name', '未知')}")
print(f"空数据: {safe_get_value(empty_data, 'name', '未知')}")
print(f"None数据: {safe_get_value(none_data, 'name', '未知')}")
performance_optimization()
总结
掌握Python的布尔与逻辑运算是编写高效代码的基础:
- 真值判定:理解哪些值是假值,哪些是真值
- 逻辑运算符:掌握
and、or、not的使用 - 短路逻辑:利用短路特性优化性能
- any/all函数:处理可迭代对象的逻辑判断
- 条件表达式:使用三元运算符简化代码
- 最佳实践:注重可读性和性能优化
通过系统学习这些概念,你将能够编写出更加清晰、高效的Python代码。
转载请注明:周志洋的博客 » Python基础知识-布尔与逻辑运算


