本系列文章是为 Python3 学习者精心设计的一套全面、实用的学习指南,旨在帮助读者从基础入门到项目实战,全面提升编程能力。文章结构由 5 个版块组成,内容层层递进,逻辑清晰。
无论你是 Python3 初学者,还是希望提升实战能力的开发者,本系列文章都能为你提供清晰的学习路径和实用的编程技巧,助你快速成长为 Python3 编程高手。
关系运算(Relational Operations)是编程语言中用于比较两个值之间关系的运算操作。在Python中,关系运算符用于对两个操作数进行大小比较,返回布尔值(True/False)来表示比较结果。这些运算构成了程序逻辑判断的基础,是条件语句和循环控制的核心要素。
Python支持以下8种关系运算符:
>
大于<
小于>=
大于等于<=
小于等于==
等于!=
不等于is
对象标识相等is not
对象标识不等所有关系运算符都返回布尔类型(bool)值:
print(5 > 3) # True
print(2.0 == 2) # True(类型不同但值相等)
print('a' != 97) # True(ASCII码比较)
不同数据类型间的比较规则:
print([1,2] > [1,1]) # True
print((1,3) > (1,2)) # True
Python支持数学表达式的链式比较写法:
x = 5
print(1 < x <= 10) # 等价于 (1 < x) and (x <= 10)
以下是 10 个关系运算的经典示例。
关系运算在流程控制中的核心作用:
age = 18
if age >= 18:
print("成年")
elif 13 <= age < 18:
print("青少年")
else:
print("儿童")
在循环结构中的典型应用:
# 找出100以内的平方数
squares = []
for num in range(1, 101):
if int(num**0.5)**2 == num:
squares.append(num)
在数据结构处理中的应用实例:
# 筛选字典中值大于50的项
data = {'a': 45, 'b': 68, 'c': 72}
filtered = {k:v for k,v in data.items() if v > 50}
用于函数参数的有效性检查:
def calculate_bmi(weight, height):
if weight <= 0 or height <= 0:
raise ValueError("参数必须为正数")
return weight / (height ** 2)
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
# 使用冒泡法排序
print(bubble_sort([64, 34, 25, 12, 22])) # [12, 22, 25, 34, 64]
def validate_user_input(input_data):
errors = []
if not (18 <= input_data['age'] <= 120):
errors.append("年龄必须在18-120之间")
if len(input_data['password']) < 8:
errors.append("密码至少8位")
if input_data['email'].count('@') != 1:
errors.append("邮箱格式错误")
return errors
# 定义一个包含用户输入数据的字典
user_input = {
'age': 17,
'password': 'defgh',
'email': '[email protected]'
}
# 调用 validate_user_input 函数进行验证
result = validate_user_input(user_input)
# 检查验证结果
if result:
print("验证失败,存在以下错误:")
for error in result:
print(error)
else:
print("验证成功,输入数据符合要求。")
# 执行结果
"""
验证失败,存在以下错误:
年龄必须在18-120之间
密码至少8位
"""
class GameObject:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def is_colliding(self, other):
return (self.x < other.x + other.width and
self.x + self.width > other.x and
self.y < other.y + other.height and
self.y + self.height > other.y)
# 创建两个 GameObject 对象
obj1 = GameObject(0, 0, 10, 10)
obj2 = GameObject(5, 5, 10, 10)
# 检测两个物体是否发生碰撞
if obj1.is_colliding(obj2):
print("两个物体发生了碰撞!")
else:
print("两个物体没有发生碰撞。")
# 两个物体发生了碰撞!
USER_ROLES = {
'admin': 4,
'editor': 3,
'author': 2,
'viewer': 1
}
def check_permission(current_role, required_role):
return USER_ROLES[current_role] >= USER_ROLES[required_role]
print(check_permission('editor', 'author')) # True
def clean_dataset(data):
cleaned = []
for record in data:
# 过滤异常值
if (record['temperature'] >= -50 and
record['temperature'] <= 60 and
record['humidity'] >= 0 and
record['humidity'] <= 100 and
len(record['sensor_id']) == 8):
cleaned.append(record)
return cleaned
# 示例数据集
data = [
{'temperature': 25, 'humidity': 50, 'sensor_id': 'ABCDEFGH'},
{'temperature': -60, 'humidity': 30, 'sensor_id': '12345678'},
{'temperature': 30, 'humidity': 110, 'sensor_id': 'IJKLMNOP'},
{'temperature': 20, 'humidity': 40, 'sensor_id': '12345678'}
]
# 调用 clean_dataset 函数进行数据清洗
cleaned_data = clean_dataset(data)
# 打印清洗后的数据集
print(cleaned_data)
# 输出结果
"""
[{'temperature': 25, 'humidity': 50, 'sensor_id': 'ABCDEFGH'},
{'temperature': 20, 'humidity': 40, 'sensor_id': '12345678'}]
"""
def optimize_algorithm(data_size):
if data_size < 100:
return "使用冒泡排序"
elif 100 <= data_size < 10_000:
return "使用快速排序"
elif 10_000 <= data_size < 1_000_000:
return "使用归并排序"
else:
return "使用外部排序"
# 测试案例
optimize_algorithm(9999) # 使用快速排序
下面是 10 常见的关系运算中的经典问题。
不同数据类型比较时的典型错误:
# 错误示例
try:
print("5" > 4)
except TypeError as e:
print(f"错误:{e}") # '>' not supported between 'str' and 'int'
解决方案:
# 正确处理方法
value = input("输入数字:")
if value.isdigit() and int(value) > 10:
print("有效输入")
处理浮点数比较的注意事项:
a = 0.1 + 0.1 + 0.1
b = 0.3
print(a == b) # False(实际输出:False)
# 正确比较方法
import math
print(math.isclose(a, b)) # True
None值的特殊比较行为:
x = None
print(x == None) # True(PEP8推荐使用is)
print(x is None) # True(更高效)
列表等可变对象的比较特性:
lst1 = [1, 2]
lst2 = [1, 2]
print(lst1 == lst2) # True(值相等)
print(lst1 is lst2) # False(不同对象)
错误现象:
if x = 5: # SyntaxError
print("x等于5")
原因分析:混淆赋值运算符=
与比较运算符==
解决方案:
if x == 5:
print("x等于5")
错误现象:
print('ß' == 'ss') # False(德语字母)
print('café' == 'cafe\u0301') # False(不同编码形式)
原因分析:Unicode规范化问题导致视觉相同字符比较失败
解决方案:
from unicodedata import normalize
str1 = normalize('NFC', 'café')
str2 = normalize('NFC', 'cafe\u0301')
print(str1 == str2) # True
错误现象:
numbers = [10, 20, 30]
for i in range(len(numbers)):
if numbers[i] > 15:
numbers.append(5) # 导致无限循环
原因分析:在遍历过程中修改被比较的集合
解决方案:
original = [10, 20, 30]
temp = original.copy()
for num in original:
if num > 15:
temp.append(5)
numbers = temp
错误现象:
flag = True
print(0 < flag < 2) # 实际输出:False
原因分析:布尔值True转换为1参与比较
正确理解:
# 等价于 (0 < flag) and (flag < 2)
# 转换为 (0 < 1) and (1 < 2) → True
错误现象:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p1 = Point(1,2)
p2 = Point(1,2)
print(p1 == p2) # False
原因分析:未实现__eq__
方法
解决方案:
class Point:
def __eq__(self, other):
return self.x == other.x and self.y == other.y
错误现象:
value = 5
if 3 < value < 7: # 正确写法
pass
if 3 < value == True: # 错误逻辑
pass
原因分析:混淆布尔上下文与数值比较
正确实践:
# 显式布尔转换
is_valid = (value > 3)
if is_valid:
pass
本试卷共计试题 30 题,满分 100 分。其中:选择题 15 题、填空题 10 题、编程题 5 题,每题后提供正确答案。
print(5 <= 5.0)
的输出结果是?
A) True
B) False
C) 报错
D) None
答案:A
以下哪个表达式会引发TypeError?
A) "10" > 5
B) [3,2] > [3,1]
C) None is None
D) 5 == 5.0
答案:A
math.isclose(0.1+0.2, 0.3)
的主要作用是?
A) 精确等于判断
B) 浮点数近似相等判断
C) 类型转换
D) 内存地址比较
答案:B
下列哪个表达式返回True?
A) 'apple' > 'Apple'
B) (1, 3) < (1, 2)
C) [5] == 5
D) 3 != 3.0
答案:A(ASCII码小写字母值更大)
print(0 < [] < {})
的输出结果是?
A) True
B) False
C) 报错
D) None
答案:C(容器类型不能跨类型比较)
关于is
运算符描述正确的是:
A) 比较对象的值
B) 比较对象的内存地址
C) 适用于所有数据类型
D) 与==
完全等价
答案:B
下列哪个表达式返回False?
A) True == 1
B) False == 0
C) True is 1
D) None is None
答案:C
print(2+3j > 1+4j)
的结果是:
A) True
B) False
C) 报错
D) None
答案:C(复数不支持比较运算)
下列哪种数据类型比较时自动转换数值类型?
A) int与str
B) int与float
C) bool与dict
D) list与tuple
答案:B
关于链式比较5 < x <= 10
的正确描述是:
A) 等价于5 < x and x <= 10
B) 等价于(5 < x) <= 10
C) 优先计算x <= 10
D) 需要显式使用括号
答案:A
表达式'a' in 'apple' == True
的返回值是:
A) True
B) False
C) 报错
D) None
答案:B(实际等价于('a' in 'apple') and ('apple' == True)
下列哪个表达式结果不同?
A) not (5 > 3)
B) 5 <= 3
C) 3 >= 5
D) 5 == 3+2
答案:D
关于浮点数比较,推荐做法是:
A) 直接使用==
B) 使用round()
四舍五入
C) 使用math.isclose()
D) 转换为字符串比较
答案:C
print({1:2} == {1:2})
的输出是:
A) True
B) False
C) 报错
D) None
答案:A
表达式(1, 2) > [1, 2]
的结果是:
A) True
B) False
C) 报错
D) None
答案:C(跨容器类型比较报错)
判断None值的推荐方法是使用______运算符
答案:is
链式比较表达式 5 < x <= 10
等价于______
答案:(5 < x) and (x <= 10)
比较两个浮点数a和b的推荐函数是______
答案:math.isclose(a, b)
表达式3 < 5 < 7
的等价写法是______
答案:(3 < 5) and (5 < 7)
判断两个变量是否指向同一对象应使用______运算符
答案:is
空列表的布尔等价值是______
答案:False
表达式bool(0.0) == ______
的结果为True
答案:False
比较两个字典内容相等应使用______运算符
答案:==
表达式'A' < 'a'
返回______
答案:True
禁用链式比较时需要使用的逻辑运算符是______
答案:and(用于连接多个独立比较)
def is_leap(year):
"""判断闰年规则:
1. 能被4整除但不能被100整除
2. 能被400整除
"""
# 考生需在此处编写代码
# 测试案例
# print(is_leap(2000)) # True
# print(is_leap(1900)) # False
参考答案:
return year%4 == 0 and year%100 != 0 or year%400 == 0
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# 请补全运算符重载方法
def __eq__(self, other): ...
# 测试案例
# v1 = Vector(3,4)
# print(v1 == Vector(3,4)) # True
参考答案:
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def filter_data(data, min_val, max_val):
"""筛选数值在[min_val, max_val]区间的元素"""
# 考生需在此处编写代码
# 测试案例
# print(filter_data([10, 25, 30, 5], 15, 30)) # [25, 30]
参考答案:
return [x for x in data if min_val <= x <= max_val]
def smart_sort(items):
"""排序规则:
1. 字符串按长度升序
2. 数字按数值降序
"""
# 考生需在此处编写代码
# 测试案例
# print(smart_sort(['apple', 30, 'a', 5])) # ['a', 'apple', 30, 5]
参考答案:
return sorted(items, key=lambda x: len(x) if isinstance(x, str) else -x)
class GameObject:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def is_collide(self, other):
"""AABB碰撞检测算法实现"""
# 考生需在此处编写代码
# 测试案例
# obj1 = GameObject(0,0,10,10)
# obj2 = GameObject(8,8,10,10)
# print(obj1.is_collide(obj2)) # True
参考答案:
return (self.x < other.x + other.w and
self.x + self.w > other.x and
self.y < other.y + other.h and
self.y + self.h > other.y)
以下是 3 项目开发样式程序。
class Student:
def __init__(self, name, scores):
self.name = name
self.scores = scores
def is_pass(self, subject, pass_score=60):
return self.scores.get(subject, 0) >= pass_score
# 这里修改为每个学生的成绩是包含课程的字典
math_scores = {
'Alice': {'math': 85},
'Bob': {'math': 58},
'Charlie': {'math': 72}
}
students = [Student(name, score) for name, score in math_scores.items()]
passed = [s for s in students if s.is_pass('math')]
print(f"及格学生:{[s.name for s in passed]}") # 及格学生:['Alice', 'Charlie']
def filter_products(products, min_price=None, max_price=None, category=None):
filtered = products.copy()
if min_price is not None:
filtered = [p for p in filtered if p['price'] >= min_price]
if max_price is not None:
filtered = [p for p in filtered if p['price'] <= max_price]
if category:
filtered = [p for p in filtered if p['category'] == category]
return filtered
# 测试数据
products = [
{'name': '鼠标', 'price': 99, 'category': '外设'},
{'name': '键盘', 'price': 199, 'category': '外设'},
{'name': '显示器', 'price': 899, 'category': '显示设备'}
]
print(filter_products(products, min_price=100, max_price=900))
# 显示结果
"""
[{'name': '键盘', 'price': 199, 'category': '外设'},
{'name': '显示器', 'price': 899, 'category': '显示设备'}]
"""
class Character:
def __init__(self, health=100, level=1):
self.health = health
self.level = level
@property
def is_alive(self):
return self.health > 0
def attack(self, target):
if not self.is_alive:
print("无法攻击:角色已死亡")
return
damage = 10 * self.level
if target.level - self.level >= 5:
damage *= 0.5
elif self.level - target.level >=5:
damage *= 1.5
target.health = max(0, target.health - damage)
print(f"造成{damage}点伤害!目标剩余生命:{target.health}")
# 使用示例
hero = Character(health=150, level=5)
enemy = Character(level=3)
while hero.is_alive and enemy.is_alive:
hero.attack(enemy)
if enemy.is_alive:
enemy.attack(hero)
# 结果显示
"""
造成50点伤害!目标剩余生命:50
造成30点伤害!目标剩余生命:120
造成50点伤害!目标剩余生命:0
"""