Python 第二阶段:核心进阶(手把手代码教学)
我会用 **"概念 + 逐行代码解释 + 实战练习"** 的方式,把第二阶段所有知识点讲透。每一个知识点都有可直接运行的代码,你可以边看边敲,确保完全理解。
第二阶段总览
目标 :掌握 Python 四大核心数据结构和函数编程,能写中等复杂度的实用程序预计时间 :2-3 周学习方法:每个代码样例都亲手敲一遍运行,完成每个知识点后的小练习
第一部分:复合数据类型(Python 最核心的内容)
Python 有 4 种最常用的复合数据类型,它们是所有 Python 程序的基础:
- 列表 (list):有序、可变、可重复
- 元组 (tuple):有序、不可变、可重复
- 字典 (dict):键值对、无序、键唯一
- 集合 (set):无序、不可重复
1. 列表 (list):最常用的数据结构
1.1 列表的定义与基本操作
python
运行
# 定义一个列表
fruits = ["苹果", "香蕉", "橙子", "葡萄"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14] # 列表可以包含不同类型的元素
empty_list = [] # 空列表
# 访问列表元素(索引从0开始)
print(fruits[0]) # 输出:苹果
print(fruits[-1]) # 输出:葡萄(负索引表示从末尾开始数)
# 修改列表元素
fruits[1] = "草莓"
print(fruits) # 输出:['苹果', '草莓', '橙子', '葡萄']
# 列表切片(获取子列表)
print(fruits[1:3]) # 输出:['草莓', '橙子'](包含索引1,不包含索引3)
print(fruits[:2]) # 输出:['苹果', '草莓'](从开头到索引2)
print(fruits[2:]) # 输出:['橙子', '葡萄'](从索引2到末尾)
print(fruits[::2]) # 输出:['苹果', '橙子'](步长为2)
print(fruits[::-1]) # 输出:['葡萄', '橙子', '草莓', '苹果'](反转列表)
1.2 列表常用方法
python
运行
fruits = ["苹果", "香蕉", "橙子"]
# 增加元素
fruits.append("葡萄") # 在末尾添加一个元素
print(fruits) # ['苹果', '香蕉', '橙子', '葡萄']
fruits.insert(1, "草莓") # 在指定位置插入元素
print(fruits) # ['苹果', '草莓', '香蕉', '橙子', '葡萄']
fruits.extend(["西瓜", "芒果"]) # 在末尾添加多个元素
print(fruits) # ['苹果', '草莓', '香蕉', '橙子', '葡萄', '西瓜', '芒果']
# 删除元素
fruits.remove("香蕉") # 删除第一个匹配的元素
print(fruits) # ['苹果', '草莓', '橙子', '葡萄', '西瓜', '芒果']
popped = fruits.pop() # 删除并返回末尾的元素
print(popped) # 芒果
print(fruits) # ['苹果', '草莓', '橙子', '葡萄', '西瓜']
popped2 = fruits.pop(2) # 删除并返回指定索引的元素
print(popped2) # 橙子
print(fruits) # ['苹果', '草莓', '葡萄', '西瓜']
# 其他常用方法
print(fruits.index("草莓")) # 输出:1(查找元素的索引)
print(fruits.count("苹果")) # 输出:1(统计元素出现次数)
fruits.sort() # 排序(原地修改)
print(fruits) # ['苹果', '草莓', '西瓜', '葡萄']
fruits.reverse() # 反转(原地修改)
print(fruits) # ['葡萄', '西瓜', '草莓', '苹果']
fruits.clear() # 清空列表
print(fruits) # []
1.3 列表推导式(Pythonic 的写法)
列表推导式是 Python 特有的简洁写法,用来快速生成列表
python
运行
# 普通写法生成1-10的平方
squares = []
for i in range(1, 11):
squares.append(i ** 2)
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 列表推导式写法(一行搞定)
squares2 = [i ** 2 for i in range(1, 11)]
print(squares2) # 结果相同
# 带条件的列表推导式
even_squares = [i ** 2 for i in range(1, 11) if i % 2 == 0]
print(even_squares) # [4, 16, 36, 64, 100]
# 嵌套列表推导式
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
print(flattened) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
小练习:用列表推导式生成 1-100 中所有能被 3 整除的数
2. 元组 (tuple):不可变的列表
元组和列表几乎一样,唯一的区别是元组一旦创建就不能修改
python
运行
# 定义元组
point = (10, 20)
colors = ("red", "green", "blue")
single_tuple = (5,) # 注意:单个元素的元组必须加逗号
empty_tuple = ()
# 访问元组元素(和列表一样)
print(point[0]) # 输出:10
print(colors[1:3]) # 输出:('green', 'blue')
# 元组不可修改(下面这行代码会报错)
# point[0] = 15 # TypeError: 'tuple' object does not support item assignment
# 元组的常用方法
print(colors.count("red")) # 输出:1
print(colors.index("blue")) # 输出:2
# 元组解包
x, y = point
print(x) # 输出:10
print(y) # 输出:20
# 交换两个变量(Python特有的简洁写法)
a = 1
b = 2
a, b = b, a
print(a, b) # 输出:2 1
为什么要用元组?
- 性能更好:元组比列表快
- 更安全:防止数据被意外修改
- 可以作为字典的键(列表不行)
3. 字典 (dict):键值对存储
字典是 Python 中最重要的数据结构之一,用来存储键值对形式的数据
python
运行
# 定义字典
student = {
"name": "张三",
"age": 18,
"gender": "男",
"scores": [90, 85, 95]
}
empty_dict = {}
# 访问字典元素
print(student["name"]) # 输出:张三
print(student["scores"][0]) # 输出:90
# 使用get方法访问(推荐,不会报错)
print(student.get("age")) # 输出:18
print(student.get("address")) # 输出:None(键不存在时返回None)
print(student.get("address", "未知")) # 输出:未知(指定默认值)
# 修改和添加元素
student["age"] = 19 # 修改已有键的值
student["address"] = "北京" # 添加新的键值对
print(student)
# 输出:{'name': '张三', 'age': 19, 'gender': '男', 'scores': [90, 85, 95], 'address': '北京'}
# 删除元素
del student["gender"] # 删除指定键值对
print(student)
# 输出:{'name': '张三', 'age': 19, 'scores': [90, 85, 95], 'address': '北京'}
popped = student.pop("address") # 删除并返回指定键的值
print(popped) # 输出:北京
print(student) # 输出:{'name': '张三', 'age': 19, 'scores': [90, 85, 95]}
# 字典常用方法
print(student.keys()) # 获取所有键:dict_keys(['name', 'age', 'scores'])
print(student.values()) # 获取所有值:dict_values(['张三', 19, [90, 85, 95]])
print(student.items()) # 获取所有键值对:dict_items([('name', '张三'), ('age', 19), ('scores', [90, 85, 95])])
# 遍历字典
for key, value in student.items():
print(f"{key}: {value}")
# 输出:
# name: 张三
# age: 19
# scores: [90, 85, 95]
# 字典推导式
squares_dict = {i: i ** 2 for i in range(1, 6)}
print(squares_dict) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
小练习:创建一个字典,存储 3 个学生的姓名和成绩,然后计算他们的平均成绩
4. 集合 (set):去重神器
集合的主要特点是元素唯一、无序,最常用的功能是去重和集合运算
python
运行
# 定义集合
numbers = {1, 2, 3, 4, 5}
fruits = {"苹果", "香蕉", "橙子", "苹果"} # 重复元素会被自动去重
print(fruits) # 输出:{'苹果', '香蕉', '橙子'}
empty_set = set() # 注意:空集合必须用set(),不能用{}({}是空字典)
# 集合的基本操作
fruits.add("葡萄") # 添加元素
print(fruits) # {'苹果', '香蕉', '橙子', '葡萄'}
fruits.remove("香蕉") # 删除元素(元素不存在会报错)
print(fruits) # {'苹果', '橙子', '葡萄'}
fruits.discard("西瓜") # 删除元素(元素不存在不会报错)
print(fruits) # {'苹果', '橙子', '葡萄'}
# 集合运算
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print(set1 & set2) # 交集:{4, 5}
print(set1 | set2) # 并集:{1, 2, 3, 4, 5, 6, 7, 8}
print(set1 - set2) # 差集:{1, 2, 3}
print(set1 ^ set2) # 对称差集:{1, 2, 3, 6, 7, 8}
# 集合推导式
odd_set = {i for i in range(1, 11) if i % 2 != 0}
print(odd_set) # {1, 3, 5, 7, 9}
# 最常用的场景:列表去重
lst = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique_lst = list(set(lst))
print(unique_lst) # [1, 2, 3, 4]
第二部分:函数基础
函数是组织代码的基本单位,可以把重复的代码封装起来,提高代码的复用性和可读性
1. 函数的定义与调用
python
运行
# 定义函数
def greet():
"""这是一个简单的问候函数""" # 文档字符串(函数说明)
print("Hello, World!")
# 调用函数
greet() # 输出:Hello, World!
# 带参数的函数
def greet_person(name):
"""问候指定的人"""
print(f"Hello, {name}!")
greet_person("张三") # 输出:Hello, 张三!
# 带返回值的函数
def add(a, b):
"""计算两个数的和"""
result = a + b
return result # return语句返回结果
sum_result = add(3, 5)
print(sum_result) # 输出:8
# 函数可以返回多个值(本质是返回一个元组)
def get_name_and_age():
return "李四", 20
name, age = get_name_and_age()
print(name) # 输出:李四
print(age) # 输出:20
2. 函数参数的四种类型
这是函数部分最重要的知识点,一定要掌握
python
运行
# 1. 位置参数(最基本)
def power(base, exponent):
return base ** exponent
print(power(2, 3)) # 输出:8(2是base,3是exponent)
# 2. 关键字参数(调用时指定参数名)
print(power(exponent=3, base=2)) # 输出:8(顺序可以任意)
# 3. 默认参数(定义时指定默认值)
def power_default(base, exponent=2):
return base ** exponent
print(power_default(3)) # 输出:9(使用默认值2)
print(power_default(3, 3)) # 输出:27(覆盖默认值)
# 注意:默认参数必须放在位置参数后面
# 错误写法:def func(a=1, b): ...
# 4. 可变参数(接收任意数量的参数)
# 4.1 *args:接收任意数量的位置参数,打包成元组
def sum_all(*args):
print(args) # args是一个元组
total = 0
for num in args:
total += num
return total
print(sum_all(1, 2, 3)) # 输出:6
print(sum_all(1, 2, 3, 4, 5)) # 输出:15
# 4.2 **kwargs:接收任意数量的关键字参数,打包成字典
def print_info(**kwargs):
print(kwargs) # kwargs是一个字典
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="王五", age=22, gender="男")
# 输出:
# {'name': '王五', 'age': 22, 'gender': '男'}
# name: 王五
# age: 22
# gender: 男
# 四种参数可以组合使用,但顺序必须是:位置参数 -> *args -> 默认参数 -> **kwargs
def complex_func(a, b, *args, c=10, **kwargs):
print(f"a={a}, b={b}")
print(f"args={args}")
print(f"c={c}")
print(f"kwargs={kwargs}")
complex_func(1, 2, 3, 4, 5, d=20, e=30)
# 输出:
# a=1, b=2
# args=(3, 4, 5)
# c=10
# kwargs={'d': 20, 'e': 30}
3. 函数作用域
作用域指的是变量的有效范围
python
运行
# 全局变量:定义在函数外面,整个程序都能访问
global_var = "我是全局变量"
def test_scope():
# 局部变量:定义在函数里面,只能在函数内部访问
local_var = "我是局部变量"
print(local_var) # 输出:我是局部变量
print(global_var) # 输出:我是全局变量
test_scope()
# print(local_var) # 报错:NameError: name 'local_var' is not defined
# 在函数内部修改全局变量
count = 0
def increment():
global count # 声明使用全局变量
count += 1
increment()
print(count) # 输出:1
4. 匿名函数 (lambda)
lambda 是一种简洁的函数定义方式,适合定义简单的单行函数
python
运行
# 普通函数
def add(a, b):
return a + b
# lambda函数(等价于上面的add函数)
add_lambda = lambda a, b: a + b
print(add_lambda(3, 5)) # 输出:8
# lambda最常用的场景:作为参数传递给高阶函数
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # 输出:[1, 4, 9, 16, 25]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # 输出:[2, 4]
5. 高阶函数
高阶函数是指接收函数作为参数 或者返回函数的函数
python
运行
# 1. map():将函数应用于序列的每个元素
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, numbers))
print(result) # 输出:[2, 4, 6, 8, 10]
# 2. filter():过滤序列中满足条件的元素
result = list(filter(lambda x: x > 3, numbers))
print(result) # 输出:[4, 5]
# 3. reduce():对序列元素进行累积计算
from functools import reduce
result = reduce(lambda x, y: x + y, numbers)
print(result) # 输出:15(1+2+3+4+5)
# 自定义高阶函数
def apply_func(func, x):
return func(x)
print(apply_func(lambda x: x ** 2, 5)) # 输出:25
print(apply_func(lambda x: x + 10, 5)) # 输出:15
第三部分:模块与包
模块和包是组织大型 Python 项目的方式,可以把代码分成多个文件,方便管理和复用
1. 模块的导入
模块就是一个.py文件,里面可以定义函数、类和变量
python
运行
# 导入整个模块
import math
print(math.sqrt(16)) # 输出:4.0
# 导入模块中的特定函数或变量
from math import sqrt, pi
print(sqrt(25)) # 输出:5.0
print(pi) # 输出:3.141592653589793
# 导入模块并给别名
import math as m
print(m.sin(m.pi / 2)) # 输出:1.0
# 导入模块中的所有内容(不推荐,容易造成命名冲突)
from math import *
print(cos(0)) # 输出:1.0
2. 自定义模块
创建一个名为my_module.py的文件,内容如下:
python
运行
# my_module.py
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + b
PI = 3.1415926
然后在同一个目录下创建另一个文件main.py,导入并使用这个模块:
python
运行
# main.py
import my_module
print(my_module.greet("张三")) # 输出:Hello, 张三!
print(my_module.add(3, 5)) # 输出:8
print(my_module.PI) # 输出:3.1415926
3. 包的结构
包是一个包含多个模块的目录,目录下必须有一个__init__.py文件(可以是空的)
plaintext
my_package/
__init__.py
module1.py
module2.py
导入包中的模块:
python
运行
from my_package import module1
from my_package.module2 import func2
4. 常用内置模块
Python 自带了很多实用的内置模块,这里介绍几个最常用的
python
运行
# 1. random:生成随机数
import random
print(random.random()) # 生成0-1之间的随机浮点数
print(random.randint(1, 10)) # 生成1-10之间的随机整数
print(random.choice(["苹果", "香蕉", "橙子"])) # 从列表中随机选择一个元素
# 2. datetime:处理日期和时间
import datetime
now = datetime.datetime.now()
print(now) # 输出当前时间
print(now.year) # 输出年份
print(now.strftime("%Y-%m-%d %H:%M:%S")) # 格式化时间
# 3. os:操作系统接口
import os
print(os.getcwd()) # 获取当前工作目录
print(os.listdir()) # 列出当前目录下的文件和文件夹
# os.mkdir("test_dir") # 创建目录
# 4. json:处理JSON数据
import json
data = {"name": "张三", "age": 18}
json_str = json.dumps(data) # 将字典转换为JSON字符串
print(json_str) # 输出:{"name": "张三", "age": 18}
data2 = json.loads(json_str) # 将JSON字符串转换为字典
print(data2["name"]) # 输出:张三
第四部分:异常处理
异常处理可以让程序在遇到错误时不会崩溃,而是优雅地处理错误
1. 基本异常处理
python
运行
# 没有异常处理的情况(程序会崩溃)
# a = 10 / 0 # 报错:ZeroDivisionError: division by zero
# 有异常处理的情况
try:
a = 10 / 0
except ZeroDivisionError:
print("除数不能为零!")
print("程序继续运行") # 这行代码会执行
2. 捕获多个异常
python
运行
try:
num = int(input("请输入一个数字:"))
result = 10 / num
except ValueError:
print("输入的不是有效数字!")
except ZeroDivisionError:
print("除数不能为零!")
except Exception as e: # 捕获所有其他异常
print(f"发生了未知错误:{e}")
else:
# 没有异常时执行
print(f"结果是:{result}")
finally:
# 无论有没有异常都会执行
print("程序执行完毕")
3. 主动抛出异常
python
运行
def divide(a, b):
if b == 0:
raise ValueError("除数不能为零") # 主动抛出异常
return a / b
try:
divide(10, 0)
except ValueError as e:
print(e) # 输出:除数不能为零
4. 自定义异常
python
运行
# 自定义异常类,继承自Exception
class AgeError(Exception):
pass
def check_age(age):
if age < 0 or age > 150:
raise AgeError("年龄必须在0-150之间")
print("年龄合法")
try:
check_age(200)
except AgeError as e:
print(e) # 输出:年龄必须在0-150之间
第二阶段综合实战:学生管理系统
现在我们用第二阶段学到的所有知识,写一个完整的学生管理系统
python
运行
# 学生管理系统
students = []
def add_student():
"""添加学生"""
name = input("请输入学生姓名:")
age = int(input("请输入学生年龄:"))
gender = input("请输入学生性别:")
student = {
"name": name,
"age": age,
"gender": gender
}
students.append(student)
print("添加成功!")
def show_students():
"""显示所有学生"""
if not students:
print("没有学生信息")
return
print("-" * 30)
print("序号\t姓名\t年龄\t性别")
print("-" * 30)
for i, student in enumerate(students, 1):
print(f"{i}\t{student['name']}\t{student['age']}\t{student['gender']}")
print("-" * 30)
def search_student():
"""查找学生"""
name = input("请输入要查找的学生姓名:")
found = False
for student in students:
if student["name"] == name:
print(f"姓名:{student['name']}")
print(f"年龄:{student['age']}")
print(f"性别:{student['gender']}")
found = True
break
if not found:
print("未找到该学生")
def delete_student():
"""删除学生"""
name = input("请输入要删除的学生姓名:")
for i, student in enumerate(students):
if student["name"] == name:
del students[i]
print("删除成功!")
return
print("未找到该学生")
def main():
"""主函数"""
while True:
print("\n===== 学生管理系统 =====")
print("1. 添加学生")
print("2. 显示所有学生")
print("3. 查找学生")
print("4. 删除学生")
print("0. 退出系统")
choice = input("请输入您的选择:")
try:
choice = int(choice)
if choice == 1:
add_student()
elif choice == 2:
show_students()
elif choice == 3:
search_student()
elif choice == 4:
delete_student()
elif choice == 0:
print("感谢使用,再见!")
break
else:
print("输入错误,请输入0-4之间的数字")
except ValueError:
print("输入错误,请输入数字")
if __name__ == "__main__":
main()
运行这个程序,你会得到一个功能完整的学生管理系统,它用到了我们第二阶段学到的:
- 列表和字典存储数据
- 函数封装功能
- 循环和条件判断
- 异常处理
- 输入输出
下一步学习计划
恭喜你完成了 Python 第二阶段的学习!现在你已经掌握了 Python 的核心语法,可以写中等复杂度的程序了。
接下来我们将进入第三阶段:Python 高级特性,包括:
- 面向对象编程 (OOP)
- 装饰器详解
- 生成器与迭代器
- 上下文管理器
需要我现在开始讲解第三阶段面向对象编程的详细内容和代码样例吗?
把后面所有阶段都详细发出
已完成思考
Python 从入门到精通:第三至第七阶段完整教程
我会延续第二阶段的 **"概念 + 逐行代码解释 + 实战练习"** 教学风格,把剩余所有阶段的核心知识点讲透。每个知识点都有可直接运行的代码,你可以边看边敲,确保完全掌握。
第三阶段:Python 高级特性(3-4 周)
目标 :掌握 Python 高级语法,写出优雅、高效、专业的 Python 代码核心内容:面向对象编程、装饰器、生成器、迭代器、上下文管理器
第一部分:面向对象编程(OOP)
面向对象是 Python 最重要的编程范式,也是所有大型项目的基础
1. 类与对象基础
python
运行
# 定义一个类(类名通常用大驼峰命名法)
class Student:
# 类属性:所有对象共享
school = "清华大学"
# 构造方法:创建对象时自动调用
def __init__(self, name, age, score):
# 实例属性:每个对象独有
self.name = name
self.age = age
self.score = score
# 实例方法:第一个参数必须是self,代表对象本身
def introduce(self):
print(f"我叫{self.name},今年{self.age}岁,考试得了{self.score}分")
def study(self, hours):
print(f"{self.name}学习了{hours}小时")
self.score += hours * 2 # 学习可以提高分数
# 创建对象(实例化)
student1 = Student("张三", 18, 80)
student2 = Student("李四", 19, 75)
# 访问实例属性
print(student1.name) # 输出:张三
print(student2.score) # 输出:75
# 调用实例方法
student1.introduce() # 输出:我叫张三,今年18岁,考试得了80分
student2.study(3) # 输出:李四学习了3小时
print(student2.score) # 输出:81
# 访问类属性
print(Student.school) # 输出:清华大学
print(student1.school) # 输出:清华大学(对象也可以访问类属性)
# 修改类属性
Student.school = "北京大学"
print(student1.school) # 输出:北京大学(所有对象的类属性都改变了)
小练习 :定义一个Car类,包含品牌、颜色、速度属性,以及加速、减速、显示信息的方法
2. 类方法与静态方法
python
运行
class Student:
school = "清华大学"
def __init__(self, name, age):
self.name = name
self.age = age
# 实例方法:操作实例属性
def introduce(self):
print(f"我叫{self.name},今年{self.age}岁")
# 类方法:第一个参数是cls,代表类本身
@classmethod
def change_school(cls, new_school):
cls.school = new_school
# 静态方法:不需要self或cls参数,和普通函数一样
@staticmethod
def is_adult(age):
return age >= 18
# 调用类方法(通过类调用,也可以通过对象调用)
Student.change_school("复旦大学")
print(Student.school) # 输出:复旦大学
# 调用静态方法
print(Student.is_adult(18)) # 输出:True
print(Student.is_adult(17)) # 输出:False
3. 继承
继承可以让子类拥有父类的所有属性和方法,实现代码复用
python
运行
# 父类(基类)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"我叫{self.name},今年{self.age}岁")
# 子类(派生类):继承自Person
class Student(Person):
def __init__(self, name, age, student_id, score):
# 调用父类的构造方法
super().__init__(name, age)
# 子类独有的属性
self.student_id = student_id
self.score = score
# 子类独有的方法
def study(self):
print(f"{self.name}正在学习")
# 方法重写:覆盖父类的方法
def introduce(self):
print(f"我叫{self.name},今年{self.age}岁,学号是{self.student_id},成绩是{self.score}分")
# 创建子类对象
student = Student("王五", 20, "2023001", 90)
# 调用继承自父类的属性和方法
print(student.name) # 输出:王五
student.introduce() # 输出:我叫王五,今年20岁,学号是2023001,成绩是90分
# 调用子类独有的方法
student.study() # 输出:王五正在学习
4. 多态
多态指的是同一个方法,不同对象调用会有不同的行为
python
运行
class Animal:
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("汪汪汪")
class Cat(Animal):
def make_sound(self):
print("喵喵喵")
class Duck(Animal):
def make_sound(self):
print("嘎嘎嘎")
# 多态:同一个函数,接收不同的对象,执行不同的操作
def animal_sound(animal):
animal.make_sound()
# 创建不同的对象
dog = Dog()
cat = Cat()
duck = Duck()
# 调用同一个函数
animal_sound(dog) # 输出:汪汪汪
animal_sound(cat) # 输出:喵喵喵
animal_sound(duck) # 输出:嘎嘎嘎
5. 封装:私有属性与方法
Python 中没有真正的私有,用 ** 双下划线__** 表示私有,外部不能直接访问
python
运行
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner # 公有属性
self.__balance = balance # 私有属性
# 提供公有方法来访问和修改私有属性
def get_balance(self):
return self.__balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"存款成功,余额:{self.__balance}")
else:
print("存款金额必须大于0")
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"取款成功,余额:{self.__balance}")
else:
print("取款金额无效")
account = BankAccount("张三", 1000)
# 可以访问公有属性
print(account.owner) # 输出:张三
# 不能直接访问私有属性(会报错)
# print(account.__balance) # AttributeError
# 通过公有方法访问私有属性
print(account.get_balance()) # 输出:1000
# 通过公有方法修改私有属性
account.deposit(500) # 输出:存款成功,余额:1500
account.withdraw(300) # 输出:取款成功,余额:1200
6. 魔术方法(特殊方法)
魔术方法是 Python 中以双下划线开头和结尾的方法,会在特定情况下自动调用
python
运行
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
# __str__:打印对象时调用,返回字符串表示
def __str__(self):
return f"《{self.title}》- {self.author}"
# __repr__:对象的官方表示,通常和__str__一致
def __repr__(self):
return f"Book('{self.title}', '{self.author}', {self.pages})"
# __len__:调用len()函数时调用
def __len__(self):
return self.pages
# __eq__:判断两个对象是否相等时调用
def __eq__(self, other):
if isinstance(other, Book):
return self.title == other.title and self.author == other.author
return False
book1 = Book("Python编程:从入门到实践", "埃里克·马瑟斯", 544)
book2 = Book("Python编程:从入门到实践", "埃里克·马瑟斯", 544)
book3 = Book("流畅的Python", "卢西亚诺·拉马略", 768)
print(book1) # 输出:《Python编程:从入门到实践》- 埃里克·马瑟斯
print(repr(book1)) # 输出:Book('Python编程:从入门到实践', '埃里克·马瑟斯', 544)
print(len(book1)) # 输出:544
print(book1 == book2) # 输出:True
print(book1 == book3) # 输出:False
第二部分:装饰器详解
装饰器是 Python 最强大的特性之一,本质是函数嵌套 + 高阶函数 + 闭包,可以在不修改原函数代码的情况下,给函数添加额外功能
1. 装饰器的本质
python
运行
# 这是一个普通函数
def say_hello():
print("Hello!")
# 我们想给这个函数添加一个功能:在执行前打印"开始执行",执行后打印"执行完毕"
# 方法1:修改原函数(不好,违反开闭原则)
# 方法2:使用装饰器
# 定义装饰器函数
def my_decorator(func):
def wrapper():
print("开始执行")
func() # 调用原函数
print("执行完毕")
return wrapper
# 使用装饰器
@my_decorator
def say_hello():
print("Hello!")
# 等价于:say_hello = my_decorator(say_hello)
say_hello()
# 输出:
# 开始执行
# Hello!
# 执行完毕
2. 带参数的装饰器
python
运行
# 装饰器接收参数
def repeat(times):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(times):
result = func(*args, **kwargs)
return result
return wrapper
return decorator
@repeat(times=3)
def greet(name):
print(f"Hello, {name}!")
greet("张三")
# 输出:
# Hello, 张三!
# Hello, 张三!
# Hello, 张三!
3. 多个装饰器叠加
python
运行
def decorator1(func):
def wrapper():
print("装饰器1开始")
func()
print("装饰器1结束")
return wrapper
def decorator2(func):
def wrapper():
print("装饰器2开始")
func()
print("装饰器2结束")
return wrapper
# 装饰器执行顺序:从下到上
@decorator1
@decorator2
def say_hello():
print("Hello!")
say_hello()
# 输出:
# 装饰器1开始
# 装饰器2开始
# Hello!
# 装饰器2结束
# 装饰器1结束
4. 常用内置装饰器
python
运行
class Circle:
def __init__(self, radius):
self._radius = radius
# @property:将方法变成属性,可以直接访问
@property
def radius(self):
return self._radius
# @radius.setter:允许修改属性
@radius.setter
def radius(self, value):
if value > 0:
self._radius = value
else:
raise ValueError("半径必须大于0")
# 计算面积的属性
@property
def area(self):
return 3.14 * self._radius ** 2
circle = Circle(5)
print(circle.radius) # 输出:5(像访问属性一样访问方法)
print(circle.area) # 输出:78.5
circle.radius = 10 # 修改属性
print(circle.area) # 输出:314
# circle.radius = -5 # 报错:ValueError: 半径必须大于0
第三部分:生成器与迭代器
生成器和迭代器是 Python 中处理大数据集的高效方式,节省内存,因为它们不会一次性把所有数据加载到内存中
1. 迭代器
python
运行
# 可迭代对象:可以用for循环遍历的对象,如列表、元组、字典、字符串
# 迭代器:实现了__iter__()和__next__()方法的对象
# 创建迭代器
numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers) # 将可迭代对象转换为迭代器
# 使用next()函数获取下一个元素
print(next(iterator)) # 输出:1
print(next(iterator)) # 输出:2
print(next(iterator)) # 输出:3
# 自定义迭代器
class Countdown:
def __init__(self, start):
self.current = start
def __iter__(self):
return self
def __next__(self):
if self.current <= 0:
raise StopIteration
self.current -= 1
return self.current + 1
# 使用自定义迭代器
for num in Countdown(5):
print(num)
# 输出:
# 5
# 4
# 3
# 2
# 1
2. 生成器
生成器是一种特殊的迭代器,用yield关键字定义,比自定义迭代器更简单
python
运行
# 生成器函数
def countdown(start):
while start > 0:
yield start # 暂停函数,返回值
start -= 1
# 创建生成器对象
gen = countdown(5)
# 使用生成器
print(next(gen)) # 输出:5
print(next(gen)) # 输出:4
# 用for循环遍历生成器
for num in countdown(3):
print(num)
# 输出:
# 3
# 2
# 1
# 生成器表达式(类似列表推导式,但用圆括号)
gen = (i ** 2 for i in range(1, 6))
print(list(gen)) # 输出:[1, 4, 9, 16, 25]
# 生成器的优势:处理大文件时不会占用大量内存
# 例如:读取一个10GB的文件
def read_large_file(file_path):
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
yield line.strip()
# 每次只读取一行到内存
for line in read_large_file("large_file.txt"):
print(line)
第四部分:上下文管理器
上下文管理器用于自动管理资源,比如文件操作、数据库连接、网络连接等,确保资源在使用后被正确释放
1. with 语句
python
运行
# 传统的文件操作方式(容易忘记关闭文件)
f = open("test.txt", "w")
f.write("Hello, World!")
f.close() # 如果中间发生异常,这行代码不会执行,文件不会关闭
# 使用with语句(自动关闭文件)
with open("test.txt", "w") as f:
f.write("Hello, World!")
# 离开with块时,文件会自动关闭,即使发生异常
2. 自定义上下文管理器
python
运行
# 方法1:类实现(__enter__和__exit__方法)
class FileManager:
def __init__(self, file_path, mode):
self.file_path = file_path
self.mode = mode
self.file = None
def __enter__(self):
# 进入with块时执行
self.file = open(self.file_path, self.mode, encoding="utf-8")
return self.file
def __exit__(self, exc_type, exc_val, exc_tb):
# 离开with块时执行
if self.file:
self.file.close()
# 返回True表示异常已经处理,不会向上传播
return False
# 使用自定义上下文管理器
with FileManager("test.txt", "w") as f:
f.write("Hello, World!")
# 方法2:装饰器实现(@contextmanager)
from contextlib import contextmanager
@contextmanager
def file_manager(file_path, mode):
f = open(file_path, mode, encoding="utf-8")
try:
yield f # 这里的f会被赋值给as后面的变量
finally:
f.close()
# 使用
with file_manager("test.txt", "r") as f:
content = f.read()
print(content)
第三阶段综合实战:图书管理系统(面向对象版)
python
运行
class Book:
def __init__(self, book_id, title, author, quantity):
self.book_id = book_id
self.title = title
self.author = author
self.quantity = quantity
def __str__(self):
return f"ID: {self.book_id}, 书名: {self.title}, 作者: {self.author}, 库存: {self.quantity}"
class Library:
def __init__(self):
self.books = {} # 用字典存储图书,键是book_id
def add_book(self, book):
if book.book_id in self.books:
print("图书ID已存在")
else:
self.books[book.book_id] = book
print("图书添加成功")
def remove_book(self, book_id):
if book_id in self.books:
del self.books[book_id]
print("图书删除成功")
else:
print("图书不存在")
def search_book(self, title):
found = False
for book in self.books.values():
if title.lower() in book.title.lower():
print(book)
found = True
if not found:
print("未找到相关图书")
def borrow_book(self, book_id):
if book_id in self.books:
book = self.books[book_id]
if book.quantity > 0:
book.quantity -= 1
print(f"借阅成功,《{book.title}》剩余库存:{book.quantity}")
else:
print("图书库存不足")
else:
print("图书不存在")
def return_book(self, book_id):
if book_id in self.books:
book = self.books[book_id]
book.quantity += 1
print(f"归还成功,《{book.title}》剩余库存:{book.quantity}")
else:
print("图书不存在")
def show_all_books(self):
if not self.books:
print("图书馆没有图书")
return
print("-" * 60)
print(f"{'ID':<5} {'书名':<20} {'作者':<15} {'库存':<5}")
print("-" * 60)
for book in self.books.values():
print(f"{book.book_id:<5} {book.title:<20} {book.author:<15} {book.quantity:<5}")
print("-" * 60)
def main():
library = Library()
# 添加一些初始图书
library.add_book(Book(1, "Python编程:从入门到实践", "埃里克·马瑟斯", 5))
library.add_book(Book(2, "流畅的Python", "卢西亚诺·拉马略", 3))
library.add_book(Book(3, "Python深度学习", "弗朗索瓦·肖莱", 4))
while True:
print("\n===== 图书馆管理系统 =====")
print("1. 添加图书")
print("2. 删除图书")
print("3. 查找图书")
print("4. 借阅图书")
print("5. 归还图书")
print("6. 显示所有图书")
print("0. 退出系统")
choice = input("请输入您的选择:")
try:
choice = int(choice)
if choice == 1:
book_id = int(input("请输入图书ID:"))
title = input("请输入书名:")
author = input("请输入作者:")
quantity = int(input("请输入库存数量:"))
library.add_book(Book(book_id, title, author, quantity))
elif choice == 2:
book_id = int(input("请输入要删除的图书ID:"))
library.remove_book(book_id)
elif choice == 3:
title = input("请输入要查找的书名:")
library.search_book(title)
elif choice == 4:
book_id = int(input("请输入要借阅的图书ID:"))
library.borrow_book(book_id)
elif choice == 5:
book_id = int(input("请输入要归还的图书ID:"))
library.return_book(book_id)
elif choice == 6:
library.show_all_books()
elif choice == 0:
print("感谢使用,再见!")
break
else:
print("输入错误,请输入0-6之间的数字")
except ValueError:
print("输入错误,请输入数字")
if __name__ == "__main__":
main()
第四阶段:Python 标准库详解(2-3 周)
目标 :熟练使用 Python 标准库,提高开发效率,避免重复造轮子核心内容:数据处理、文件操作、网络编程、并发编程、正则表达式等
第一部分:数据处理相关库
1. collections:扩展数据类型
python
运行
from collections import Counter, defaultdict, OrderedDict, deque, namedtuple
# Counter:计数器,统计元素出现次数
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
word_count = Counter(words)
print(word_count) # 输出:Counter({'apple': 3, 'banana': 2, 'orange': 1})
print(word_count.most_common(2)) # 输出:[('apple', 3), ('banana', 2)]
# defaultdict:默认字典,访问不存在的键时返回默认值
d = defaultdict(int)
d["a"] += 1
d["b"] += 2
print(d) # 输出:defaultdict(int, {'a': 1, 'b': 2})
# deque:双端队列,两端都可以高效地添加和删除元素
dq = deque([1, 2, 3])
dq.appendleft(0) # 左端添加
dq.append(4) # 右端添加
print(dq) # 输出:deque([0, 1, 2, 3, 4])
dq.popleft() # 左端删除
dq.pop() # 右端删除
print(dq) # 输出:deque([1, 2, 3])
# namedtuple:命名元组,可以通过属性访问元素
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(p.x) # 输出:10
print(p.y) # 输出:20
2. itertools:迭代器工具集
python
运行
import itertools
# 无限迭代器
# count:从指定数字开始计数
for i in itertools.count(1, 2):
if i > 10:
break
print(i) # 输出:1, 3, 5, 7, 9
# cycle:循环迭代
colors = ["red", "green", "blue"]
for i, color in enumerate(itertools.cycle(colors)):
if i > 5:
break
print(color) # 输出:red, green, blue, red, green, blue
# 有限迭代器
# chain:连接多个迭代器
for num in itertools.chain([1, 2, 3], [4, 5, 6]):
print(num) # 输出:1, 2, 3, 4, 5, 6
# combinations:生成所有组合(不考虑顺序)
letters = ["a", "b", "c"]
for combo in itertools.combinations(letters, 2):
print(combo) # 输出:('a', 'b'), ('a', 'c'), ('b', 'c')
# permutations:生成所有排列(考虑顺序)
for perm in itertools.permutations(letters, 2):
print(perm) # 输出:('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')
3. functools:函数工具集
python
运行
import functools
# partial:偏函数,固定函数的部分参数
def power(base, exponent):
return base ** exponent
square = functools.partial(power, exponent=2)
cube = functools.partial(power, exponent=3)
print(square(5)) # 输出:25
print(cube(5)) # 输出:125
# lru_cache:缓存装饰器,缓存函数的返回结果
@functools.lru_cache(maxsize=None)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(100)) # 瞬间得到结果,没有缓存会非常慢
第二部分:正则表达式(re 模块)
正则表达式是处理字符串的强大工具,用于匹配、查找、替换字符串
python
运行
import re
# 1. 匹配字符串
# match:从字符串开头匹配
result = re.match(r"hello", "hello world")
print(result.group()) # 输出:hello
# search:在字符串中查找第一个匹配
result = re.search(r"world", "hello world")
print(result.group()) # 输出:world
# findall:查找所有匹配
result = re.findall(r"\d+", "我有2个苹果和3个橙子")
print(result) # 输出:['2', '3']
# 2. 常用元字符
# .:匹配任意字符(除了换行)
# ^:匹配字符串开头
# $:匹配字符串结尾
# *:匹配前面的字符0次或多次
# +:匹配前面的字符1次或多次
# ?:匹配前面的字符0次或1次
# {n}:匹配前面的字符n次
# {n,}:匹配前面的字符至少n次
# {n,m}:匹配前面的字符n到m次
# []:匹配括号中的任意一个字符
# |:或
# ():分组
# 3. 常用示例
# 验证邮箱
email_pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
print(re.match(email_pattern, "test@example.com")) # 匹配成功
print(re.match(email_pattern, "invalid-email")) # 匹配失败
# 验证手机号
phone_pattern = r"^1[3-9]\d{9}$"
print(re.match(phone_pattern, "13812345678")) # 匹配成功
print(re.match(phone_pattern, "12345678901")) # 匹配失败
# 提取URL中的域名
url = "https://www.example.com/path/to/page.html"
domain_pattern = r"https?://([^/]+)"
result = re.search(domain_pattern, url)
print(result.group(1)) # 输出:www.example.com
# 4. 替换字符串
text = "我的电话号码是13812345678,邮箱是test@example.com"
# 替换电话号码为***
new_text = re.sub(r"1[3-9]\d{9}", "***", text)
print(new_text) # 输出:我的电话号码是***,邮箱是test@example.com
第三部分:文件与目录操作
1. pathlib:面向对象的路径处理(Python3.4 + 推荐)
python
运行
from pathlib import Path
# 创建Path对象
current_dir = Path.cwd() # 当前工作目录
print(current_dir)
# 路径拼接
file_path = current_dir / "test.txt"
print(file_path)
# 检查路径是否存在
print(file_path.exists()) # 输出:False
# 创建文件
file_path.touch()
print(file_path.exists()) # 输出:True
# 写入文件
file_path.write_text("Hello, World!", encoding="utf-8")
# 读取文件
content = file_path.read_text(encoding="utf-8")
print(content) # 输出:Hello, World!
# 创建目录
new_dir = current_dir / "test_dir"
new_dir.mkdir(exist_ok=True) # exist_ok=True表示如果目录已存在不报错
# 遍历目录
for file in current_dir.iterdir():
if file.is_file():
print(f"文件:{file.name}")
elif file.is_dir():
print(f"目录:{file.name}")
# 删除文件
file_path.unlink()
# 删除空目录
new_dir.rmdir()
2. shutil:高级文件操作
python
运行
import shutil
# 复制文件
shutil.copy("source.txt", "destination.txt")
# 复制目录
shutil.copytree("source_dir", "destination_dir")
# 移动文件/目录
shutil.move("source.txt", "destination.txt")
# 删除目录(包括里面的所有文件)
shutil.rmtree("test_dir")
第四部分:并发编程
Python 提供了多线程、多进程和协程三种并发编程方式
1. 多线程(threading)
适合 I/O 密集型任务(如网络请求、文件读写)
python
运行
import threading
import time
def task(name, delay):
print(f"任务{name}开始")
time.sleep(delay)
print(f"任务{name}结束,耗时{delay}秒")
# 创建线程
thread1 = threading.Thread(target=task, args=("A", 2))
thread2 = threading.Thread(target=task, args=("B", 3))
# 启动线程
start_time = time.time()
thread1.start()
thread2.start()
# 等待线程结束
thread1.join()
thread2.join()
end_time = time.time()
print(f"所有任务完成,总耗时:{end_time - start_time:.2f}秒")
# 输出:总耗时约3秒(两个线程并行执行)
2. 多进程(multiprocessing)
适合 CPU 密集型任务(如数值计算、数据处理)
python
运行
import multiprocessing
import time
def cpu_task(name, numbers):
print(f"进程{name}开始计算")
result = sum([i ** 2 for i in numbers])
print(f"进程{name}计算完成,结果:{result}")
return result
if __name__ == "__main__":
numbers1 = range(1, 1000000)
numbers2 = range(1000000, 2000000)
# 创建进程
process1 = multiprocessing.Process(target=cpu_task, args=("A", numbers1))
process2 = multiprocessing.Process(target=cpu_task, args=("B", numbers2))
# 启动进程
start_time = time.time()
process1.start()
process2.start()
# 等待进程结束
process1.join()
process2.join()
end_time = time.time()
print(f"所有进程完成,总耗时:{end_time - start_time:.2f}秒")
3. 线程池与进程池(concurrent.futures)
python
运行
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
def task(name, delay):
print(f"任务{name}开始")
time.sleep(delay)
print(f"任务{name}结束")
return f"任务{name}的结果"
# 线程池
with ThreadPoolExecutor(max_workers=3) as executor:
# 提交任务
futures = [executor.submit(task, f"A{i}", 2) for i in range(5)]
# 获取结果
for future in futures:
print(future.result())
# 进程池用法和线程池一样,只是把ThreadPoolExecutor换成ProcessPoolExecutor
第五部分:其他常用标准库
1. logging:日志记录
python
运行
import logging
# 配置日志
logging.basicConfig(
level=logging.DEBUG, # 日志级别:DEBUG < INFO < WARNING < ERROR < CRITICAL
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("app.log"), # 输出到文件
logging.StreamHandler() # 输出到控制台
]
)
# 记录日志
logging.debug("这是调试信息")
logging.info("这是普通信息")
logging.warning("这是警告信息")
logging.error("这是错误信息")
logging.critical("这是严重错误信息")
2. argparse:命令行参数解析
python
运行
import argparse
# 创建参数解析器
parser = argparse.ArgumentParser(description="这是一个简单的命令行程序")
# 添加参数
parser.add_argument("name", help="你的名字")
parser.add_argument("-a", "--age", type=int, help="你的年龄")
parser.add_argument("-g", "--gender", choices=["男", "女"], default="男", help="你的性别")
# 解析参数
args = parser.parse_args()
# 使用参数
print(f"你好,{args.name}!")
if args.age:
print(f"你今年{args.age}岁")
print(f"你的性别是{args.gender}")
# 运行方式:python script.py 张三 -a 20 -g 男
第五阶段:Python 第三方库生态(4-6 周)
目标 :掌握 Python 主流第三方库,能解决实际工作中的各种问题核心内容:数据分析、机器学习、Web 开发、爬虫等
第一部分:数据分析与科学计算
1. NumPy:数值计算基础
python
运行
import numpy as np
# 创建数组
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr1.shape) # 输出:(5,)
print(arr2.shape) # 输出:(2, 3)
# 数组运算
print(arr1 + 2) # 输出:[3 4 5 6 7]
print(arr1 * 3) # 输出:[ 3 6 9 12 15]
print(arr1 + arr1) # 输出:[ 2 4 6 8 10]
# 常用函数
print(np.mean(arr1)) # 输出:3.0(平均值)
print(np.sum(arr1)) # 输出:15(求和)
print(np.max(arr1)) # 输出:5(最大值)
print(np.min(arr1)) # 输出:1(最小值)
# 数组索引与切片
print(arr2[0, 1]) # 输出:2
print(arr2[:, 1]) # 输出:[2 5]
2. Pandas:数据处理与分析
python
运行
import pandas as pd
# 创建DataFrame
data = {
"name": ["张三", "李四", "王五", "赵六"],
"age": [18, 19, 20, 21],
"score": [85, 90, 78, 92]
}
df = pd.DataFrame(data)
print(df)
# 输出:
# name age score
# 0 张三 18 85
# 1 李四 19 90
# 2 王五 20 78
# 3 赵六 21 92
# 基本信息
print(df.info())
print(df.describe())
# 数据筛选
print(df[df["score"] > 85])
# 输出:
# name age score
# 1 李四 19 90
# 3 赵六 21 92
# 数据排序
print(df.sort_values("score", ascending=False))
# 输出:
# name age score
# 3 赵六 21 92
# 1 李四 19 90
# 0 张三 18 85
# 2 王五 20 78
# 分组统计
df["grade"] = ["A", "A", "B", "A"]
print(df.groupby("grade")["score"].mean())
# 输出:
# grade
# A 89.0
# B 78.0
# Name: score, dtype: float64
3. Matplotlib:数据可视化
python
运行
import matplotlib.pyplot as plt
import numpy as np
# 折线图
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, label="sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.title("正弦函数")
plt.legend()
plt.grid(True)
plt.show()
# 柱状图
names = ["张三", "李四", "王五", "赵六"]
scores = [85, 90, 78, 92]
plt.figure(figsize=(10, 6))
plt.bar(names, scores, color=["red", "green", "blue", "yellow"])
plt.xlabel("姓名")
plt.ylabel("成绩")
plt.title("学生成绩柱状图")
plt.show()
第二部分:机器学习与深度学习
1. Scikit-learn:传统机器学习
python
运行
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载数据集
iris = load_iris()
X = iris.data # 特征
y = iris.target # 标签
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建模型
model = KNeighborsClassifier(n_neighbors=3)
# 训练模型
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print(f"模型准确率:{accuracy:.2f}") # 输出:模型准确率:1.00
2. PyTorch:深度学习框架
python
运行
import torch
import torch.nn as nn
import torch.optim as optim
# 定义简单的神经网络
class SimpleNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
# 创建模型
model = SimpleNN(input_size=4, hidden_size=10, output_size=3)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 模拟训练数据
X = torch.randn(100, 4)
y = torch.randint(0, 3, (100,))
# 训练模型
for epoch in range(100):
# 前向传播
outputs = model(X)
loss = criterion(outputs, y)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f"Epoch [{epoch+1}/100], Loss: {loss.item():.4f}")
第三部分:Web 开发与爬虫
1. Requests:HTTP 请求库
python
运行
import requests
# 发送GET请求
response = requests.get("https://api.github.com/users/octocat")
print(response.status_code) # 输出:200
print(response.json()) # 输出:JSON格式的响应数据
# 发送POST请求
data = {"username": "test", "password": "123456"}
response = requests.post("https://httpbin.org/post", json=data)
print(response.json())
2. BeautifulSoup4:HTML 解析
python
运行
from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 查找元素
title = soup.find("title").text
print(title) # 输出:Example Domain
# 查找所有链接
links = soup.find_all("a")
for link in links:
print(link.get("href")) # 输出:https://www.iana.org/domains/example
3. Flask:轻量级 Web 框架
python
运行
from flask import Flask, jsonify, request
app = Flask(__name__)
# 定义路由
@app.route("/")
def home():
return "Hello, Flask!"
@app.route("/api/users", methods=["GET"])
def get_users():
users = [
{"id": 1, "name": "张三", "age": 18},
{"id": 2, "name": "李四", "age": 19}
]
return jsonify(users)
@app.route("/api/users", methods=["POST"])
def add_user():
data = request.get_json()
new_user = {
"id": 3,
"name": data["name"],
"age": data["age"]
}
return jsonify(new_user), 201
if __name__ == "__main__":
app.run(debug=True)
第六阶段:Python 工程实践(3-4 周)
目标 :掌握 Python 项目开发的最佳实践,能开发和维护大型项目核心内容:项目结构、虚拟环境、版本控制、测试、文档、部署
1. 标准 Python 项目结构
plaintext
my_project/
├── my_project/ # 项目源代码
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
│ └── subpackage/
│ ├── __init__.py
│ └── module3.py
├── tests/ # 测试代码
│ ├── __init__.py
│ ├── test_module1.py
│ └── test_module2.py
├── docs/ # 文档
├── examples/ # 示例代码
├── requirements.txt # 依赖列表
├── pyproject.toml # 项目配置文件(现代Python推荐)
├── README.md # 项目说明
├── LICENSE # 许可证
└── .gitignore # Git忽略文件
2. 虚拟环境管理
bash
运行
# 使用venv创建虚拟环境(Python3.3+内置)
python -m venv myenv
# 激活虚拟环境
# Windows
myenv\Scripts\activate
# Mac/Linux
source myenv/bin/activate
# 安装依赖
pip install requests flask
# 导出依赖
pip freeze > requirements.txt
# 从requirements.txt安装依赖
pip install -r requirements.txt
# 退出虚拟环境
deactivate
3. 版本控制(Git)
bash
运行
# 初始化Git仓库
git init
# 添加文件到暂存区
git add .
# 提交更改
git commit -m "Initial commit"
# 添加远程仓库
git remote add origin https://github.com/username/my_project.git
# 推送到远程仓库
git push -u origin main
# 创建分支
git branch feature
# 切换分支
git checkout feature
# 合并分支
git checkout main
git merge feature
# 查看提交历史
git log
4. 单元测试(pytest)
python
运行
# 创建测试文件:test_calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0
def test_subtract():
assert subtract(5, 3) == 2
assert subtract(3, 5) == -2
assert subtract(0, 0) == 0
运行测试:
bash
运行
pytest test_calculator.py -v
5. 代码质量工具
bash
运行
# 代码格式化:black
pip install black
black my_project/
# 代码风格检查:flake8
pip install flake8
flake8 my_project/
# 类型检查:mypy
pip install mypy
mypy my_project/
第七阶段:Python 进阶方向(按需学习)
目标:根据职业发展选择深入方向,成为领域专家
1. 数据科学与人工智能
- 深度学习进阶:计算机视觉(YOLO、CNN)、自然语言处理(Transformer、BERT)、大模型(LLaMA、ChatGLM)
- 大数据处理:Spark、Hadoop、Dask
- 数据可视化:Plotly、Dash、Streamlit
- MLOps:模型部署、监控、版本管理
2. Web 开发
- 前后端分离:Vue.js、React、TypeScript
- 微服务架构:FastAPI、Docker、Kubernetes
- 数据库:MySQL、PostgreSQL、MongoDB、Redis
- 安全:认证授权、XSS、CSRF、SQL 注入防护
3. 自动化运维与 DevOps
- 脚本自动化:Ansible、SaltStack
- 容器化:Docker、Docker Compose
- 容器编排:Kubernetes
- 监控与告警:Prometheus、Grafana
- CI/CD:GitHub Actions、GitLab CI、Jenkins
4. 其他方向
- 桌面应用开发:PyQt、PySide、Tkinter
- 游戏开发:Pygame、Pyglet
- 嵌入式开发:MicroPython、CircuitPython
- 量化交易:Backtrader、Zipline、vn.py
学习建议与下一步
- 边学边练:每个知识点都要亲手敲代码运行,不要只看理论
- 项目驱动:通过实际项目巩固所学知识,比如做一个个人博客、数据分析项目、简单的机器学习模型
- 阅读优秀代码:学习 GitHub 上的开源项目,了解别人的代码风格和设计思想
- 持续学习:Python 生态更新很快,要关注最新的技术动态
- 社区交流:参与 Stack Overflow、GitHub、Python 中文社区等,解决问题和分享经验