Python 概述

Python 是一种解释型高级通用的编程语言由 Guido van Rossum 在 1989 年底开始创作并于 1991 年首次发布Python 的设计哲学强调代码的可读性其语法允许程序员用更少的代码行表达概念相比 C++ 或 Java 等语言更加简洁

⭐⭐特点

简单易学Python 的语法清晰简洁接近自然语言非常适合初学者入门


解释型语言Python 是解释型语言代码逐行执行便于调试和测试
跨平台Python 可以在 WindowsLinuxmacOS 等多种操作系统上运行
面向对象Python 支持面向对象编程包括类继承多态等特性
动态类型Python 是动态类型语言变量不需要声明类型运行时自动推断
自动内存管理Python 具有垃圾回收机制自动管理内存分配和释放
丰富的标准库Python 提供了"电池included"的标准库涵盖文件处理网络通信数据库操作等多个领域
强大的第三方库PyPIPython Package Index拥有数十万个第三方库几乎可以满足任何开发需求
可扩展性Python 可以调用 C/C++ 编写的库也可以被嵌入到其他应用程序中
社区活跃Python 拥有庞大且活跃的开发者社区提供丰富的学习资源和技术支持

⭐⭐应用领域

Web 开发使用 DjangoFlaskFastAPI 等框架构建 Web 应用和 API


数据科学与分析使用 NumPyPandasMatplotlib 等进行数据处理和可视化
人工智能与机器学习使用 TensorFlowPyTorchScikit-learn 等开发 AI 模型
自动化脚本编写系统管理文件处理网络爬虫等自动化脚本
桌面应用使用 TkinterPyQtKivy 等开发图形界面应用
游戏开发使用 Pygame 等库进行简单的游戏开发
网络编程开发网络服务器客户端应用网络工具等
科学计算用于物理学生物学天文学等领域的数值计算和模拟

环境安装

下载安装

💗💗 Python 官方网站提供了各种操作系统的安装包

🔗🔗 Python 官方下载页面

Windows 系统安装

  1. 访问 Python 官网下载最新版本的安装包
  2. 运行安装程序务必勾选”Add Python to PATH”选项
  3. 选择”Install Now”进行默认安装或选择”Customize installation”自定义安装
  4. 安装完成后打开命令提示符CMD输入 python --version 验证安装

macOS 系统安装

1
2
3
4
5
# 方法一使用 Homebrew推荐
brew install python3

# 方法二从官网下载安装包
# 访问 https://www.python.org/downloads/mac-osx/ 下载 pkg 安装包

Linux 系统安装

1
2
3
4
5
6
7
8
9
# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-pip

# CentOS/RHEL
sudo yum install python3 python3-pip

# Fedora
sudo dnf install python3 python3-pip

验证安装

1
2
3
4
5
6
7
8
9
10
# 检查 Python 版本
python3 --version

# 检查 pip 版本
pip3 --version

# 进入 Python 交互式环境
python3
>>> print("Hello, Python!")
>>> exit()

开发工具

💗💗 Python 有多种优秀的开发工具可供选择

  • Visual Studio Code轻量级功能强大的代码编辑器通过扩展支持 Python 开发
  • PyCharmJetBrains 出品的专业 Python IDE分为社区版免费和专业版
  • Jupyter Notebook适合数据科学和机器学习的交互式笔记本环境
  • Sublime Text轻量级文本编辑器通过插件支持 Python
  • AtomGitHub 开发的开源文本编辑器

VS Code 配置 Python 开发环境

🔗🔗 Visual Studio Code 安装文档

  1. 安装 VS Code
  2. 在扩展市场搜索并安装 “Python” 扩展由 Microsoft 提供
  3. 打开 Python 文件或文件夹
  4. VS Code 会自动检测系统中安装的 Python 解释器
  5. 可以通过左下角选择 Python 解释器版本

编程基础

基本结构

Python 程序的基本结构

1
2
3
4
5
6
7
8
9
# 这是一个简单的 Python 程序
print("Hello, Python!")

# 定义变量
name = "Python"
version = 3.11

# 输出变量
print(f"Welcome to {name} {version}")

关键字介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import      # 导入模块
from # 从模块中导入特定内容
def # 定义函数
class # 定义类
if # 条件判断
elif # 否则如果
else # 否则
for # for 循环
while # while 循环
try # 异常处理尝试块
except # 异常处理捕获块
finally # 异常处理最终块
return # 函数返回值
lambda # 匿名函数
yield # 生成器返回值
with # 上下文管理器
as # 别名
pass # 空语句占位符
break # 跳出循环
continue # 继续下一次循环
True # 布尔真值
False # 布尔假值
None # 空值
and # 逻辑与
or # 逻辑或
not # 逻辑非
in # 成员运算符
is # 身份运算符
del # 删除对象
global # 声明全局变量
nonlocal # 声明非局部变量
assert # 断言
raise # 抛出异常

注意事项

  1. Python 使用缩进来表示代码块通常使用 4 个空格
  2. 注释使用 # 符号
  3. 字符串可以使用单引号 ' 或双引号 "
  4. Python 严格区分大小写
  5. 语句末尾不需要分号但可以添加

代码注释

单行注释

1
2
# 这是单行注释
print("Hello") # 这也是单行注释

多行注释

1
2
3
4
5
6
7
8
9
10
"""
这是多行注释
使用三个双引号
可以跨越多行
"""

'''
这也是多行注释
使用三个单引号
'''

文档字符串Docstring

1
2
3
4
5
6
7
8
9
10
11
12
def greet(name):
"""
这是一个文档字符串
用于说明函数的功能

参数:
name (str): 要问候的人名

返回:
str: 问候语
"""
return f"Hello, {name}!"

变量&常量

变量

⭐⭐ 变量值可以改变不需要声明类型赋值时自动确定类型

  • Python 是动态类型语言变量类型由赋值的值决定
  • 变量名区分大小写
  • 变量可以随时重新赋值不同类型的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 定义变量不需要声明类型
name = "Python"
age = 30
height = 1.75
is_student = True

# 查看变量类型
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>

# 重新赋值不同类型的值
age = "thirty" # 现在是字符串
print(type(age)) # <class 'str'>

常量

⭐⭐Python 没有真正的常量机制但约定使用全大写命名表示常量

1
2
3
4
5
6
7
# 约定俗成的常量写法
PI = 3.14159
MAX_SIZE = 100
APP_NAME = "MyApp"

# 注意这仅仅是命名约定实际上仍然可以修改
PI = 3.14 # 虽然不推荐但语法上是允许的

命名规范

  • 变量名必须以字母或下划线开头
  • 变量名只能包含字母数字和下划线
  • 变量名不能使用 Python 关键字
  • 变量名区分大小写
  • 变量名应该具有描述性

💗💗 常用命名规范

  • 蛇形命名法snake_case单词之间用下划线连接全部小写用于变量名和函数名
    • student_name, get_user_info
  • 帕斯卡命名法PascalCase每个单词首字母大写用于类名
    • StudentName, UserInfo
  • 全大写命名法所有字母大写单词用下划线分隔用于常量
    • MAX_SIZE, PI_VALUE
  • 单下划线前缀表示受保护的成员约定
    • _internal_var
  • 双下划线前缀表示私有成员名称修饰
    • __private_var

数据类型

数值类型

💗💗Python 支持整数浮点数和复数三种数值类型

  • int整数类型没有大小限制Python 3
  • float浮点数类型精度约 15-16 位
  • complex复数类型格式为 a + bj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 整数
age = 25
negative_num = -10
big_number = 10**100 # Python 支持超大整数

# 浮点数
price = 99.99
scientific = 1.23e-4 # 科学计数法0.000123

# 复数
complex_num = 3 + 4j
print(complex_num.real) # 3.0实部
print(complex_num.imag) # 4.0虚部

# 类型转换
int_num = int(3.14) # 3
float_num = float(5) # 5.0
complex_num = complex(2, 3) # (2+3j)

布尔类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 布尔值
is_true = True
is_false = False

# 布尔运算
result = True and False # False
result = True or False # True
result = not True # False

# 其他类型的布尔转换
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hello")) # True
print(bool([])) # False
print(bool([1, 2])) # True
print(bool(None)) # False

字符串类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 字符串定义
single_quote = 'Hello'
double_quote = "World"
triple_quote = '''这是一个
多行字符串'''

# 字符串拼接
greeting = "Hello" + " " + "World"

# f-string格式化字符串Python 3.6+
name = "Python"
version = 3.11
message = f"Welcome to {name} {version}"

# 字符串方法
text = " Hello, World! "
print(text.strip()) # 去除两端空白"Hello, World!"
print(text.lower()) # 转小写" hello, world! "
print(text.upper()) # 转大写" HELLO, WORLD! "
print(text.replace("World", "Python")) # 替换
print(text.split(",")) # 分割[' Hello', ' World! ']

None 类型

1
2
3
4
5
6
# None 表示空值
value = None

# 检查是否为 None
if value is None:
print("值为空")

类型转换

隐式转换

Python 在某些情况下会自动进行类型转换

1
2
3
4
5
# 整数和浮点数运算结果为浮点数
result = 5 + 3.14 # 8.14

# 布尔值和整数
result = True + 1 # 2True 被视为 1

显式转换

使用内置函数进行类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 转换为整数
int_value = int("123") # 123
int_value = int(3.14) # 3截断小数部分

# 转换为浮点数
float_value = float("3.14") # 3.14
float_value = float(5) # 5.0

# 转换为字符串
str_value = str(123) # "123"
str_value = str(3.14) # "3.14"

# 转换为列表
list_value = list("hello") # ['h', 'e', 'l', 'l', 'o']

# 转换为元组
tuple_value = tuple([1, 2, 3]) # (1, 2, 3)

# 转换为集合
set_value = set([1, 2, 2, 3]) # {1, 2, 3}

# 安全转换使用 try-except
try:
num = int("abc")
except ValueError:
print("无法转换为整数")

运算符

算术运算符

运算符示例结果描述
+x + y两数之和加法
-x - y两数之差减法
*x * y两数之积乘法
/x / y两数之商浮点数除法
//x // y商的整数部分整除
%x % y余数取模
**x ** yx 的 y 次幂幂运算
1
2
3
4
5
6
7
8
9
10
11
# 算术运算符示例
a = 10
b = 3

print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000

比较运算符

运算符示例结果描述
==x == yTrue/False等于
!=x != yTrue/False不等于
>x > yTrue/False大于
<x < yTrue/False小于
>=x >= yTrue/False大于等于
<=x <= yTrue/False小于等于
1
2
3
4
5
6
7
8
9
10
# 比较运算符示例
a = 10
b = 5

print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
print(a >= b) # True
print(a <= b) # False

逻辑运算符

运算符示例结果描述
andx and yTrue/False逻辑与
orx or yTrue/False逻辑或
notnot xTrue/False逻辑非
  • a and b 只要 a 或者 b 有一个是 False, 那么 a and b 的结果就是 False只有 a 和 b 都是 True, 那么 a and b 的结果才是 True
  • a or b 只要 a 或者 b 有一个是 True, 那么 a or b 的结果就是 True只有 a 和 b 都是 False, 那么 a or b 的结果才是 False
aba and ba or b
TrueTrueTrueTrue
FalseTrueFalseTrue
TrueFalseFalseTrue
FalseFalseFalseFalse
1
2
3
4
5
6
7
# 逻辑运算符示例
a = True
b = False

print(a and b) # False
print(a or b) # True
print(not a) # False

赋值运算符

运算符示例等价于
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
//=x //= yx = x // y
%=x %= yx = x % y
**=x **= yx = x ** y

成员运算符

运算符示例结果描述
inx in yTrue/Falsex 在 y 序列中
not inx not in yTrue/Falsex 不在 y 序列中
1
2
3
4
5
# 成员运算符示例
my_list = [1, 2, 3, 4, 5]

print(3 in my_list) # True
print(6 not in my_list) # True

身份运算符

运算符示例结果描述
isx is yTrue/Falsex 和 y 是同一个对象
is notx is not yTrue/Falsex 和 y 不是同一个对象
1
2
3
4
5
6
7
8
# 身份运算符示例
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b) # True指向同一对象
print(a is c) # False不同对象即使值相同
print(a == c) # True值相同

运算符优先级

💗💗优先级由高到低

优先级运算符描述
1()括号
2**幂运算
3+x, -x, ~x正号负号按位取反
4*, /, //, %整除取模
5+, -
6<<, >>位移
7&按位与
8^按位异或
9|按位或
10in, not in, is, is not, <, <=, >, >=, !=, ==比较成员身份运算符
11not逻辑非
12and逻辑与
13or逻辑或

输入输出

输出print

1
2
3
4
5
6
7
8
9
10
11
12
13
# 基本输出
print("Hello, World!")

# 输出多个值
print("Name:", "Python", "Version:", 3.11)

# 格式化输出
name = "Python"
version = 3.11
print(f"{name} version {version}")

# 指定分隔符和结束符
print("Hello", "World", sep="-", end="!\n") # Hello-World!

输入input

1
2
3
4
5
6
7
# 获取用户输入
name = input("请输入你的名字")
print(f"你好{name}")

# 输入数字需要类型转换
age = int(input("请输入你的年龄"))
height = float(input("请输入你的身高"))

条件语句

if 语句

1
2
3
4
5
6
7
8
9
# 基本 if 语句
age = 18

if age >= 18:
print("成年人")
elif age >= 13:
print("青少年")
else:
print("儿童")

三元表达式

1
2
3
4
# 条件表达式三元运算符
age = 20
status = "成年" if age >= 18 else "未成年"
print(status) # 成年

match 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 基本 match 语句
def http_status(code):
match code:
case 200:
return "OK"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
case _:
return "Unknown Status"

print(http_status(200)) # OK
print(http_status(404)) # Not Found
print(http_status(999)) # Unknown Status

💗💗模式匹配支持多种模式

1
2
3
4
5
6
7
8
9
10
11
12
# 匹配多个值
def check_day(day):
match day:
case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
return "工作日"
case "Saturday" | "Sunday":
return "周末"
case _:
return "无效日期"

print(check_day("Monday")) # 工作日
print(check_day("Saturday")) # 周末
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 匹配数据结构
def describe_point(point):
match point:
case (0, 0):
return "原点"
case (x, 0):
return f"X轴上的点 ({x}, 0)"
case (0, y):
return f"Y轴上的点 (0, {y})"
case (x, y):
return f"普通点 ({x}, {y})"
case _:
return "无效点"

print(describe_point((0, 0))) # 原点
print(describe_point((5, 0))) # X轴上的点 (5, 0)
print(describe_point((3, 4))) # 普通点 (3, 4)
1
2
3
4
5
6
7
8
9
10
11
12
13
# 带条件的模式匹配守卫
def classify_number(num):
match num:
case n if n > 0:
return "正数"
case n if n < 0:
return "负数"
case 0:
return "零"

print(classify_number(5)) # 正数
print(classify_number(-3)) # 负数
print(classify_number(0)) # 零

循环语句

while 循环

⭐⭐ while 循环会在条件为 True 时重复执行代码块

1
2
3
4
5
# while 循环示例
count = 0
while count < 5:
print(f"Count: {count}")
count += 1

for 循环

⭐⭐ for 循环用于遍历序列列表元组字符串等或其他可迭代对象

1
2
3
4
5
6
7
8
9
10
11
12
# 遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

# 使用 range()
for i in range(5): # 0, 1, 2, 3, 4
print(i)

# 带步长的 range
for i in range(0, 10, 2): # 0, 2, 4, 6, 8
print(i)

循环控制语句

  • break: 立即退出循环
  • continue: 跳过当前迭代继续下一次迭代
  • pass: 空语句用作占位符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# break 示例
for i in range(10):
if i == 5:
break
print(i) # 输出 0, 1, 2, 3, 4

# continue 示例
for i in range(10):
if i % 2 == 0:
continue
print(i) # 输出奇数 1, 3, 5, 7, 9

# pass 示例
for i in range(5):
pass # 占位符什么都不做

enumerate 和 zip

1
2
3
4
5
6
7
8
9
10
# enumerate同时获取索引和值
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")

# zip并行遍历多个序列
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")

数据结构

列表List

列表是 Python 中最常用的数据结构之一是有序的可变的集合

创建列表

1
2
3
4
5
6
7
8
# 创建列表
empty_list = []
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
nested = [[1, 2], [3, 4], [5, 6]]

# 使用 list() 函数
my_list = list(range(5)) # [0, 1, 2, 3, 4]

访问元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fruits = ["apple", "banana", "cherry", "date"]

# 正向索引
print(fruits[0]) # apple
print(fruits[2]) # cherry

# 负向索引
print(fruits[-1]) # date最后一个元素
print(fruits[-2]) # cherry

# 切片
print(fruits[1:3]) # ['banana', 'cherry']
print(fruits[:2]) # ['apple', 'banana']
print(fruits[2:]) # ['cherry', 'date']
print(fruits[::2]) # ['apple', 'cherry']步长为2

修改列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fruits = ["apple", "banana", "cherry"]

# 修改元素
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']

# 添加元素
fruits.append("date") # 末尾添加
fruits.insert(1, "elderberry") # 指定位置插入
fruits.extend(["fig", "grape"]) # 扩展列表

# 删除元素
fruits.remove("banana") # 删除指定值
del fruits[0] # 删除指定索引
popped = fruits.pop() # 删除并返回最后一个元素

列表方法

方法描述
append(x)在列表末尾添加元素
extend(iterable)用可迭代对象的元素扩展列表
insert(i, x)在位置 i 插入元素 x
remove(x)删除第一个值为 x 的元素
pop([i])删除并返回位置 i 的元素默认为最后一个
clear()清空列表
index(x)返回第一个值为 x 的元素的索引
count(x)返回值为 x 的元素个数
sort()原地排序
reverse()原地反转
copy()返回列表的浅拷贝

列表推导式

1
2
3
4
5
6
7
8
9
10
11
# 基本列表推导式
squares = [x**2 for x in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# 带条件的列表推导式
evens = [x for x in range(20) if x % 2 == 0]
print(evens) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# 嵌套列表推导式
matrix = [[i*j for j in range(3)] for i in range(3)]
print(matrix) # [[0, 0, 0], [0, 1, 2], [0, 2, 4]]

元组Tuple

元组是有序的不可变的集合

创建元组

1
2
3
4
5
6
7
8
# 创建元组
empty_tuple = ()
single_element = (1,) # 注意逗号
numbers = (1, 2, 3, 4, 5)
mixed = (1, "hello", 3.14)

# 不使用括号可选
coordinates = 10, 20

访问元组

1
2
3
4
5
6
7
8
colors = ("red", "green", "blue")

# 索引访问
print(colors[0]) # red
print(colors[-1]) # blue

# 切片
print(colors[1:]) # ('green', 'blue')

元组解包

⭐⭐ 解包Unpacking是将元组中的元素赋值给多个变量的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 基本解包
person = ("Alice", 25, "Beijing")
name, age, city = person
print(f"{name}, {age}岁, 来自{city}") # Alice, 25岁, 来自Beijing

# 交换变量值Python特色
a = 10
b = 20
a, b = b, a
print(a, b) # 20, 10

# 使用 * 号收集剩余元素
numbers = (1, 2, 3, 4, 5)
first, second, *rest = numbers
print(first) # 1
print(second) # 2
print(rest) # [3, 4, 5]

# 收集中间元素
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5

# 嵌套解包
nested = (1, (2, 3), 4)
a, (b, c), d = nested
print(a, b, c, d) # 1 2 3 4

# 函数返回值的解包
def get_user_info():
return "Bob", 30, "Shanghai"

username, user_age, user_city = get_user_info()
print(f"{username}今年{user_age}岁") # Bob今年30岁

元组方法

1
2
3
4
numbers = (1, 2, 3, 2, 4, 2)

print(numbers.count(2)) # 3统计出现次数
print(numbers.index(3)) # 2查找索引

集合Set

集合是无序的不重复的元素集合

创建集合

1
2
3
4
# 创建集合
empty_set = set() # 注意{} 创建的是空字典
numbers = {1, 2, 3, 4, 5}
from_list = set([1, 2, 2, 3, 3]) # {1, 2, 3}自动去重

集合运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# 并集
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8}
print(set1.union(set2))

# 交集
print(set1 & set2) # {4, 5}
print(set1.intersection(set2))

# 差集
print(set1 - set2) # {1, 2, 3}
print(set1.difference(set2))

# 对称差集
print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2))

集合方法

方法描述
add(x)添加元素
remove(x)删除元素不存在时抛出异常
discard(x)删除元素不存在时不报错
pop()随机删除并返回一个元素
clear()清空集合
union(other)并集
intersection(other)交集
difference(other)差集
issubset(other)判断是否为子集
issuperset(other)判断是否为超集

字典Dictionary

字典是无序的可变的键值对集合

创建字典

1
2
3
4
5
6
7
8
9
10
# 创建字典
empty_dict = {}
person = {
"name": "Alice",
"age": 25,
"city": "Beijing"
}

# 使用 dict() 函数
person2 = dict(name="Bob", age=30, city="Shanghai")

访问字典

1
2
3
4
5
6
7
8
9
10
11
12
13
person = {"name": "Alice", "age": 25, "city": "Beijing"}

# 访问值
print(person["name"]) # Alice
print(person.get("age")) # 25
print(person.get("phone", "N/A")) # N/A键不存在时返回默认值

# 遍历字典
for key in person:
print(f"{key}: {person[key]}")

for key, value in person.items():
print(f"{key}: {value}")

修改字典

1
2
3
4
5
6
7
8
9
10
11
12
person = {"name": "Alice", "age": 25}

# 添加/修改键值对
person["email"] = "alice@example.com" # 添加
person["age"] = 26 # 修改

# 删除键值对
del person["city"]
email = person.pop("email") # 删除并返回值

# 合并字典
person.update({"phone": "123456", "city": "Beijing"})

字典方法

方法描述
keys()返回所有键的视图
values()返回所有值的视图
items()返回所有键值对的视图
get(key, default)获取值键不存在时返回默认值
update(other)更新字典
pop(key)删除并返回指定键的值
popitem()删除并返回最后一个键值对
clear()清空字典
copy()返回字典的浅拷贝

字典推导式

1
2
3
4
5
6
7
# 字典推导式
squares = {x: x**2 for x in range(5)}
print(squares) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

# 带条件的字典推导式
even_squares = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares) # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

函数

定义函数

1
2
3
4
5
6
7
8
# 基本函数定义
def greet(name):
"""问候函数"""
return f"Hello, {name}!"

# 调用函数
message = greet("Alice")
print(message) # Hello, Alice!

参数类型

位置参数

1
2
3
4
def add(a, b):
return a + b

result = add(5, 3) # 8

默认参数

1
2
3
4
5
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"

print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Hi")) # Hi, Bob!

可变参数

1
2
3
4
5
6
7
8
9
10
11
12
# *args接收任意数量的位置参数
def sum_all(*args):
return sum(args)

print(sum_all(1, 2, 3, 4, 5)) # 15

# **kwargs接收任意数量的关键字参数
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

print_info(name="Alice", age=25, city="Beijing")

关键字参数

1
2
3
4
5
6
def create_person(name, age, city="Unknown"):
return {"name": name, "age": age, "city": city}

# 使用关键字参数
person = create_person(name="Alice", age=25, city="Beijing")
person = create_person(age=25, name="Alice") # 顺序可以不同

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 单个返回值
def square(x):
return x ** 2

# 多个返回值实际上是返回元组
def get_name_and_age():
return "Alice", 25

name, age = get_name_and_age()

# 没有返回值返回 None
def say_hello():
print("Hello!")

result = say_hello()
print(result) # None

作用域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 全局变量
global_var = "I'm global"

def test_scope():
# 局部变量
local_var = "I'm local"
print(local_var)
print(global_var) # 可以访问全局变量

test_scope()
# print(local_var) # 错误无法访问局部变量

# 修改全局变量
def modify_global():
global global_var
global_var = "Modified"

modify_global()
print(global_var) # Modified

回调函数

回调函数是作为参数传递给另一个函数的函数在特定事件发生或操作完成时被调用

1
2
3
4
5
6
7
8
9
10
11
12
# 基本回调函数示例
def greet(name):
return f"Hello, {name}!"

def process_name(name, callback):
"""处理名字并调用回调函数"""
result = callback(name)
return result

# 使用回调
message = process_name("Alice", greet)
print(message) # Hello, Alice!

应用场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 场景1数据处理后的回调
def fetch_data(callback):
"""模拟获取数据"""
data = [1, 2, 3, 4, 5]
print("数据获取完成")
callback(data) # 调用回调函数处理数据

def process_data(data):
"""处理数据的回调函数"""
squared = [x**2 for x in data]
print(f"处理结果{squared}")

fetch_data(process_data)
# 输出
# 数据获取完成
# 处理结果[1, 4, 9, 16, 25]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 场景2异步操作的回调
import time

def download_file(url, callback):
"""模拟文件下载"""
print(f"正在下载{url}")
time.sleep(1) # 模拟下载时间
file_content = f"Content of {url}"
print("下载完成")
callback(file_content) # 下载完成后调用回调

def save_to_disk(content):
"""保存文件的回调函数"""
print(f"保存到磁盘{content[:20]}...")

download_file("https://example.com/file.txt", save_to_disk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 场景3多个回调函数
def calculate(a, b, operation):
"""执行计算并返回结果"""
return operation(a, b)

# 定义不同的操作回调
def add(x, y):
return x + y

def multiply(x, y):
return x * y

def subtract(x, y):
return x - y

# 使用不同的回调
print(calculate(10, 5, add)) # 15
print(calculate(10, 5, multiply)) # 50
print(calculate(10, 5, subtract)) # 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 场景4带错误处理的回调
def divide(a, b, success_callback, error_callback):
"""除法运算支持成功和失败回调"""
try:
if b == 0:
raise ZeroDivisionError("除数不能为零")
result = a / b
success_callback(result)
except Exception as e:
error_callback(str(e))

def on_success(result):
print(f"计算成功结果{result}")

def on_error(error_message):
print(f"计算失败{error_message}")

divide(10, 2, on_success, on_error) # 计算成功结果5.0
divide(10, 0, on_success, on_error) # 计算失败除数不能为零

匿名函数

1
2
3
4
5
6
7
8
9
10
11
# Lambda 函数匿名函数
square = lambda x: x ** 2
print(square(5)) # 25

# 常用于高阶函数
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared) # [1, 4, 9, 16, 25]

evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # [2, 4]

高阶函数

函数功能参数返回值示例
map() 对 iterable 中的每个元素应用 function function, iterable map 对象可转换为列表 list(map(lambda x: x*2, [1,2,3]))[2,4,6]
filter() 过滤 iterable 中使 function 返回 True 的元素 function, iterable filter 对象可转换为列表 list(filter(lambda x: x>2, [1,2,3,4]))[3,4]
reduce() 对 iterable 累积应用 function需导入 functools function, iterable[, initializer] 单个值 reduce(lambda x,y: x+y, [1,2,3])6
sorted() 对 iterable 排序返回新列表 iterable[, key, reverse] 排序后的新列表 sorted([3,1,2], key=lambda x: -x)[3,2,1]
sum() 计算 iterable 中元素的总和 iterable[, start] 数值 sum([1,2,3])6
max() 返回 iterable 中的最大值 iterable[, key, default] 最大值 max([1,2,3], key=lambda x: -x)1
min() 返回 iterable 中的最小值 iterable[, key, default] 最小值 min([1,2,3])1
any() 如果 iterable 中任一元素为 True返回 True iterable 布尔值 any([False, True, False])True
all() 如果 iterable 中所有元素为 True返回 True iterable 布尔值 all([True, True, False])False
zip() 将多个 iterable 打包成元组的迭代器 *iterables zip 对象 list(zip([1,2], ['a','b']))[(1,'a'), (2,'b')]
enumerate() 为 iterable 添加索引 iterable[, start] enumerate 对象 list(enumerate(['a','b']))[(0,'a'), (1,'b')]

使用技巧

  • 链式调用可以组合使用多个高阶函数sum(map(lambda x: x**2, filter(lambda x: x%2==0, numbers)))
  • 性能优化这些函数返回的是迭代器惰性求值适合处理大数据
  • 可读性对于简单操作使用 lambda复杂逻辑建议定义命名函数
  • 替代方案列表推导式通常比 map/filter 更 Pythonic[x**2 for x in numbers if x%2==0]

应用场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 场景1数据排序 - 自定义排序规则
students = [
{"name": "Alice", "age": 25, "score": 88},
{"name": "Bob", "age": 20, "score": 95},
{"name": "Charlie", "age": 23, "score": 78}
]

# 按年龄升序排序
sorted_by_age = sorted(students, key=lambda s: s["age"])
print("按年龄排序")
for s in sorted_by_age:
print(f" {s['name']}: {s['age']}岁")

# 按分数降序排序
sorted_by_score = sorted(students, key=lambda s: s["score"], reverse=True)
print("\n按分数排序")
for s in sorted_by_score:
print(f" {s['name']}: {s['score']}分")

# 多条件排序先按分数降序再按年龄升序
sorted_multi = sorted(students, key=lambda s: (-s["score"], s["age"]))
print("\n多条件排序")
for s in sorted_multi:
print(f" {s['name']}: {s['score']}分, {s['age']}岁")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 场景2数据处理 - 快速转换和过滤
products = [
{"name": "Laptop", "price": 5999, "stock": 50},
{"name": "Phone", "price": 3999, "stock": 0},
{"name": "Tablet", "price": 2499, "stock": 30},
{"name": "Watch", "price": 1999, "stock": 100}
]

# 提取所有商品名称
names = list(map(lambda p: p["name"], products))
print("商品名称", names)

# 筛选有库存的商品
in_stock = list(filter(lambda p: p["stock"] > 0, products))
print(f"\n有库存的商品{len(in_stock)}个")
for p in in_stock:
print(f" {p['name']}: 库存{p['stock']}件")

# 计算打折后的价格8折
discounted = list(map(lambda p: {"name": p["name"], "original": p["price"],
"discounted": p["price"] * 0.8}, products))
print("\n打折后价格")
for p in discounted:
print(f" {p['name']}: ¥{p['original']} → ¥{p['discounted']:.2f}")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 场景3函数式编程 - 简化代码逻辑
from functools import reduce

# 计算列表中所有数字的乘积
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(f"乘积{product}") # 120

# 找出列表中的最大值
max_value = reduce(lambda x, y: x if x > y else y, numbers)
print(f"最大值{max_value}") # 5

# 字符串拼接
words = ["Hello", "World", "Python"]
sentence = reduce(lambda x, y: f"{x} {y}", words)
print(f"拼接结果{sentence}") # Hello World Python

# 嵌套使用先过滤偶数再求平方最后求和
result = sum(map(lambda x: x**2, filter(lambda x: x % 2 == 0, range(1, 11))))
print(f"1-10中偶数的平方和{result}") # 220 (2²+4²+6²+8²+10²)

模块与包

导入模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 导入整个模块
import math
print(math.sqrt(16)) # 4.0

# 导入整个模块并重命名
import numpy as np

# 导入特定函数
from math import sqrt, pi
print(sqrt(16)) # 4.0
print(pi) # 3.141592653589793

# 导入所有内容不推荐
from os import *

创建模块

1
2
3
4
5
6
7
8
# mymodule.py
def greet(name):
return f"Hello, {name}!"

def add(a, b):
return a + b

PI = 3.14159
1
2
3
4
5
6
# main.py
import mymodule

print(mymodule.greet("Alice")) # Hello, Alice!
print(mymodule.add(5, 3)) # 8
print(mymodule.PI) # 3.14159

特殊变量

Python 中有一些特殊的变量也称为魔术变量或双下划线变量它们在模块和包中具有特殊含义

__name__

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# __name__ 变量用于判断模块是作为主程序运行还是被导入

# 当直接运行脚本时__name__ 的值为 "__main__"
# 当被其他模块导入时__name__ 的值为模块名

def main():
print("这是主函数")

if __name__ == "__main__":
# 只有直接运行此脚本时才会执行
main()
print("脚本作为主程序运行")
else:
# 被导入时不会执行
print("模块被导入")

__file__

1
2
3
4
5
6
# __file__ 包含当前模块的文件路径
import os

print(__file__) # 当前文件的完整路径
print(os.path.basename(__file__)) # 文件名
print(os.path.dirname(__file__)) # 文件所在目录

__doc__

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# __doc__ 包含模块类或函数的文档字符串

"""这是一个模块的文档字符串"""

def greet(name):
"""问候函数

参数:
name (str): 要问候的人名

返回:
str: 问候语
"""
return f"Hello, {name}!"

print(__doc__) # 模块的文档字符串
print(greet.__doc__) # 函数的文档字符串

__package__

1
2
3
4
5
6
7
# __package__ 表示当前模块所属的包名

# 在包中的模块
print(__package__) # 例如'mypackage.subpackage'

# 在主脚本中
print(__package__) # None 或 ''

__all__

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# __all__ 定义使用 from module import * 时导出的内容

# mymodule.py
__all__ = ['public_func', 'PublicClass']

def public_func():
"""公开函数"""
pass

def _private_func():
"""私有函数不会被 import * 导入"""
pass

class PublicClass:
"""公开类"""
pass

class _PrivateClass:
"""私有类不会被 import * 导入"""
pass

# 使用时
from mymodule import *
# 只导入 public_func 和 PublicClass

其他特殊变量

变量描述
__name__模块名称主程序为 "__main__"
__file__模块的文件路径
__doc__模块的文档字符串
__package__模块所属的包名
__all__定义 import * 时导出的内容
__builtins__内置命名空间
__cached__编译后的缓存文件路径
__loader__加载器对象
__spec__模块规范对象

包的结构

1
2
3
4
5
6
7
mypackage/
├── __init__.py
├── module1.py
├── module2.py
└── subpackage/
├── __init__.py
└── module3.py
1
2
3
# 使用包
from mypackage import module1
from mypackage.subpackage import module3

类型注解

类型注解Type Hints是 Python 3.5+ 引入的特性用于为变量函数参数和返回值提供类型提示

基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 变量类型注解
name: str = "Python"
age: int = 30
height: float = 1.75
is_student: bool = True

# 函数参数和返回值注解
def greet(name: str) -> str:
"""问候函数"""
return f"Hello, {name}!"

def add(a: int, b: int) -> int:
"""加法函数"""
return a + b

# 使用注解后的函数
message = greet("Alice")
result = add(5, 3)
print(message) # Hello, Alice!
print(result) # 8

常见类型注解

类型示例描述
intx: int = 10整数
floatx: float = 3.14浮点数
strx: str = "hello"字符串
boolx: bool = True布尔值
bytesx: bytes = b"hello"字节串
Nonedef func() -> None:无返回值
Anyx: Any = ...任意类型

容器类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from typing import List, Dict, Tuple, Set

# 列表
numbers: List[int] = [1, 2, 3, 4, 5]
names: List[str] = ["Alice", "Bob", "Charlie"]

# 字典
person: Dict[str, int] = {"age": 25, "score": 90}
scores: Dict[str, List[int]] = {"math": [90, 85, 92], "english": [88, 92, 85]}

# 元组
coordinates: Tuple[float, float] = (10.5, 20.3)
person_info: Tuple[str, int, str] = ("Alice", 25, "Beijing")

# 集合
unique_numbers: Set[int] = {1, 2, 3, 4, 5}

可选类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from typing import Optional

# Optional可以是某种类型或 None
def find_user(user_id: int) -> Optional[str]:
"""查找用户可能返回 None"""
users = {1: "Alice", 2: "Bob"}
return users.get(user_id)

result = find_user(1) # "Alice"
result = find_user(99) # None


# 新语法使用 | 代替 OptionalPython 3.10+
def find_user(user_id: int) -> str | None:
"""查找用户可能返回 None"""
users = {1: "Alice", 2: "Bob"}
return users.get(user_id)

联合类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from typing import Union
# Union可以是多种类型之一
def process_value(value: Union[int, str]) -> str:
"""处理整数或字符串"""
if isinstance(value, int):
return f"数字{value}"
else:
return f"字符串{value}"

print(process_value(42)) # 数字42
print(process_value("hello")) # 字符串hello


# 新语法使用 | 代替 UnionPython 3.10+
def process_value(value: int | str) -> str:
"""处理整数或字符串"""
if isinstance(value, int):
return f"数字{value}"
else:
return f"字符串{value}"

复杂类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from typing import Callable, TypeVar, Generic

# Callable可调用对象函数
def apply_operation(x: int, y: int, operation: Callable[[int, int], int]) -> int:
"""应用操作函数"""
return operation(x, y)

def add(a: int, b: int) -> int:
return a + b

result = apply_operation(5, 3, add)
print(result) # 8

# TypeVar泛型类型变量
T = TypeVar('T')

def first_element(items: List[T]) -> T:
"""返回列表的第一个元素"""
return items[0]

first_int = first_element([1, 2, 3]) # int 类型
first_str = first_element(["a", "b", "c"]) # str 类型

类的类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Person:
def __init__(self, name: str, age: int) -> None:
self.name: str = name
self.age: int = age

def greet(self) -> str:
return f"Hi, I'm {self.name}"

@staticmethod
def create_anonymous() -> 'Person':
"""创建匿名人物"""
return Person("Anonymous", 0)

@classmethod
def from_dict(cls, data: Dict[str, any]) -> 'Person':
"""从字典创建 Person 对象"""
return cls(data['name'], data['age'])

# 使用类型注解的类
person = Person("Alice", 25)
print(person.greet()) # Hi, I'm Alice

类型检查工具

⭐⭐ Python 本身不强制类型检查但可以使用第三方工具进行静态类型检查

常用工具

  • mypy最流行的 Python 静态类型检查器
  • pyrightMicrosoft 开发的类型检查器
  • pytypeGoogle 开发的类型检查器
1
2
3
4
5
# 安装 mypy
pip install mypy

# 运行类型检查
mypy your_script.py
1
2
3
4
5
6
# example.py - 包含类型错误的示例
def add(a: int, b: int) -> int:
return a + b

# 这行代码会导致 mypy 报错
result = add("hello", "world") # Error: Argument 1 has incompatible type "str"; expected "int"

面向对象

类和对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 定义类
class Dog:
# 类属性
species = "Canine"

# 构造方法
def __init__(self, name, age):
# 实例属性
self.name = name
self.age = age

# 实例方法
def bark(self):
return f"{self.name} says Woof!"

def get_info(self):
return f"{self.name} is {self.age} years old"

# 创建对象
my_dog = Dog("Buddy", 3)
print(my_dog.bark()) # Buddy says Woof!
print(my_dog.get_info()) # Buddy is 3 years old
print(my_dog.species) # Canine

魔术方法

Python 中的魔法方法Magic Methods也称为双下划线方法或特殊方法它们以双下划线开头和结尾用于定义对象的特殊行为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return f"Vector({self.x}, {self.y})"

def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)

def __eq__(self, other):
return self.x == other.x and self.y == other.y

def __len__(self):
return 2

# 使用特殊方法
v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2

print(v1) # Vector(1, 2)
print(v3) # Vector(4, 6)
print(v1 == v2) # False
print(len(v1)) # 2

初始化相关

方法调用时机描述
__new__(cls[, ...])创建实例时控制实例的创建过程返回新实例
__init__(self[, ...])实例创建后初始化实例属性
__del__(self)对象销毁时清理资源析构函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Example:
def __new__(cls, value):
print(f"创建实例: {value}")
instance = super().__new__(cls)
return instance

def __init__(self, value):
print(f"初始化实例: {value}")
self.value = value

def __del__(self):
print(f"销毁实例: {self.value}")

obj = Example(42)
# 输出
# 创建实例: 42
# 初始化实例: 42

字符串相关

方法调用时机描述
__str__(self)str()print()返回用户友好的字符串表示
__repr__(self)repr() 或交互式环境返回开发者友好的字符串表示
__bytes__(self)bytes()返回字节表示
__format__(self, format_spec)format()自定义格式化输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __str__(self):
return f"{self.name} ({self.age}岁)"

def __repr__(self):
return f"Person('{self.name}', {self.age})"

p = Person("Alice", 25)
print(str(p)) # Alice (25岁)
print(repr(p)) # Person('Alice', 25)

比较运算符

方法运算符描述
__eq__(self, other)==等于
__ne__(self, other)!=不等于
__lt__(self, other)<小于
__le__(self, other)<=小于等于
__gt__(self, other)>大于
__ge__(self, other)>=大于等于
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Student:
def __init__(self, name, score):
self.name = name
self.score = score

def __eq__(self, other):
return self.score == other.score

def __lt__(self, other):
return self.score < other.score

def __str__(self):
return f"{self.name}: {self.score}分"

s1 = Student("Alice", 90)
s2 = Student("Bob", 85)
print(s1 > s2) # True
print(s1 == s2) # False

算术运算符

方法运算符描述
__add__(self, other)+加法
__sub__(self, other)-减法
__mul__(self, other)*乘法
__truediv__(self, other)/除法
__floordiv__(self, other)//整除
__mod__(self, other)%取模
__pow__(self, other)**幂运算
__matmul__(self, other)@矩阵乘法

反向运算符

方法运算符描述
__radd__(self, other)+反向加法
__rsub__(self, other)-反向减法
__rmul__(self, other)*反向乘法

赋值运算符

方法运算符描述
__iadd__(self, other)+=增量加法
__isub__(self, other)-=增量减法
__imul__(self, other)*=增量乘法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Number:
def __init__(self, value):
self.value = value

def __add__(self, other):
return Number(self.value + other.value)

def __mul__(self, other):
return Number(self.value * other.value)

def __str__(self):
return str(self.value)

n1 = Number(5)
n2 = Number(3)
print(n1 + n2) # 8
print(n1 * n2) # 15

容器类型相关

方法调用时机描述
__len__(self)len()返回容器长度
__getitem__(self, key)obj[key]获取元素
__setitem__(self, key, value)obj[key] = value设置元素
__delitem__(self, key)del obj[key]删除元素
__contains__(self, item)in 运算符判断是否包含元素
__iter__(self)iter()返回迭代器
__next__(self)next()返回下一个元素
__reversed__(self)reversed()返回反向迭代器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class MyList:
def __init__(self, items):
self.items = items

def __len__(self):
return len(self.items)

def __getitem__(self, index):
return self.items[index]

def __setitem__(self, index, value):
self.items[index] = value

def __contains__(self, item):
return item in self.items

def __iter__(self):
return iter(self.items)

my_list = MyList([1, 2, 3, 4, 5])
print(len(my_list)) # 5
print(my_list[2]) # 3
print(3 in my_list) # True
my_list[0] = 10
print(my_list[0]) # 10

属性访问相关

方法调用时机描述
__getattr__(self, name)访问不存在的属性属性未找到时调用
__getattribute__(self, name)访问任何属性所有属性访问都调用
__setattr__(self, name, value)obj.attr = value设置属性时调用
__delattr__(self, name)del obj.attr删除属性时调用
__dir__(self)dir()返回属性列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class SmartObject:
def __init__(self):
self._data = {}

def __getattr__(self, name):
if name in self._data:
return self._data[name]
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")

def __setattr__(self, name, value):
if name.startswith('_'):
super().__setattr__(name, value)
else:
self._data[name] = value

obj = SmartObject()
obj.name = "Alice"
obj.age = 25
print(obj.name) # Alice
print(obj.age) # 25

可调用对象相关

方法调用时机描述
__call__(self[, args...])obj()使实例可调用
1
2
3
4
5
6
7
8
9
10
11
12
class Multiplier:
def __init__(self, factor):
self.factor = factor

def __call__(self, value):
return value * self.factor

double = Multiplier(2)
triple = Multiplier(3)

print(double(5)) # 10
print(triple(5)) # 15

上下文管理器相关

方法调用时机描述
__enter__(self)进入 with返回上下文管理器对象
__exit__(self, exc_type, exc_val, exc_tb)退出 with清理资源处理异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class FileManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
self.file = None

def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file

def __exit__(self, exc_type, exc_val, exc_tb):
if self.file:
self.file.close()
return False # 不抑制异常

with FileManager('test.txt', 'w') as f:
f.write('Hello, World!')
# 文件自动关闭

其他魔法方法

方法调用时机描述
__hash__(self)hash()返回对象的哈希值
__bool__(self)bool()返回布尔值
__int__(self)int()转换为整数
__float__(self)float()转换为浮点数
__index__(self)切片操作转换为整数索引
__copy__(self)copy.copy()浅拷贝
__deepcopy__(self, memo)copy.deepcopy()深拷贝
__sizeof__(self)sys.getsizeof()返回对象大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class CustomNumber:
def __init__(self, value):
self.value = value

def __bool__(self):
return self.value != 0

def __int__(self):
return int(self.value)

def __float__(self):
return float(self.value)

def __hash__(self):
return hash(self.value)

num = CustomNumber(5)
print(bool(num)) # True
print(int(num)) # 5
print(float(num)) # 5.0

继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 父类
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
raise NotImplementedError("子类必须实现此方法")

# 子类
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"

class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"

# 使用继承
cat = Cat("Whiskers")
dog = Dog("Buddy")

print(cat.speak()) # Whiskers says Meow!
print(dog.speak()) # Buddy says Woof!

多态

1
2
3
4
5
6
7
# 多态示例
def animal_sound(animal):
print(animal.speak())

animals = [Cat("Whiskers"), Dog("Buddy"), Cat("Luna")]
for animal in animals:
animal_sound(animal)

封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.__balance = balance # 私有属性

def deposit(self, amount):
if amount > 0:
self.__balance += amount
return True
return False

def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
return True
return False

def get_balance(self):
return self.__balance

# 使用封装
account = BankAccount("Alice", 1000)
account.deposit(500)
account.withdraw(200)
print(account.get_balance()) # 1300
# print(account.__balance) # 错误无法直接访问私有属性

异常处理

try-except

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 基本异常处理
try:
result = 10 / 0
except ZeroDivisionError:
print("除数不能为零")

# 捕获多个异常
try:
num = int("abc")
except (ValueError, TypeError) as e:
print(f"发生错误{e}")

# 捕获所有异常
try:
# 可能出错的代码
pass
except Exception as e:
print(f"未知错误{e}")

try-except-else-finally

1
2
3
4
5
6
7
8
try:
result = 10 / 2
except ZeroDivisionError:
print("除数不能为零")
else:
print(f"结果是{result}") # 没有异常时执行
finally:
print("无论如何都会执行") # 总是执行

抛出异常

1
2
3
4
5
6
7
8
9
10
11
12
# 抛出异常
def validate_age(age):
if age < 0:
raise ValueError("年龄不能为负数")
if age > 150:
raise ValueError("年龄不合理")
return True

try:
validate_age(-5)
except ValueError as e:
print(e) # 年龄不能为负数

自定义异常

1
2
3
4
5
6
7
8
9
10
class MyException(Exception):
def __init__(self, message, code):
super().__init__(message)
self.code = code

try:
raise MyException("自定义错误", 404)
except MyException as e:
print(f"错误信息{e}")
print(f"错误代码{e.code}")

文件操作

读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 基本读取
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
print(content)

# 逐行读取
with open('example.txt', 'r', encoding='utf-8') as file:
for line in file:
print(line.strip())

# 读取所有行到列表
with open('example.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()

写入文件

1
2
3
4
5
6
7
8
# 写入文件覆盖
with open('output.txt', 'w', encoding='utf-8') as file:
file.write("Hello, World!\n")
file.write("第二行\n")

# 追加文件
with open('output.txt', 'a', encoding='utf-8') as file:
file.write("追加的内容\n")

文件模式

模式描述
'r'只读默认
'w'写入覆盖已有内容
'a'追加
'x'创建新文件已存在则失败
'b'二进制模式
't'文本模式默认
'+'读写模式

JSON文件

1
2
3
4
5
6
7
8
9
10
11
import json

# 写入 JSON
data = {"name": "Alice", "age": 25, "city": "Beijing"}
with open('data.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=2)

# 读取 JSON
with open('data.json', 'r', encoding='utf-8') as file:
loaded_data = json.load(file)
print(loaded_data)

常用标准库

Python 标准库是 Python 安装时自带的模块集合提供了丰富的功能涵盖文件系统网络通信数据处理日期时间正则表达式等多个领域

随机数

random 模块提供了生成随机数的功能如生成随机数随机选择元素生成随机密码等等

生成随机数

1
2
3
4
5
6
7
8
9
10
11
import random

# 随机浮点数0.0 到 1.0
print(random.random())

# 随机整数
print(random.randint(1, 10)) # 1-10 之间的整数
print(random.randrange(0, 10, 2)) # 0-10 之间的偶数

# 随机浮点数范围
print(random.uniform(1.5, 10.5))

序列操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random

# 从序列中随机选择
fruits = ['apple', 'banana', 'cherry', 'date']
print(random.choice(fruits))

# 随机选择多个元素
print(random.sample(fruits, 2))

# 打乱序列
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)

# 加权随机选择
choices = ['A', 'B', 'C']
weights = [0.1, 0.3, 0.6]
print(random.choices(choices, weights=weights, k=5))

随机种子

1
2
3
4
5
6
7
8
9
10
11
import random

# 设置种子保证可重复性
random.seed(42)
print(random.random()) # 每次运行结果相同

random.seed(42)
print(random.random()) # 与上面相同

# 使用系统时间作为种子默认
random.seed()

迭代器

itertools 模块提供了迭代器功能如生成迭代器迭代器操作等等

无限迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import itertools

# count无限计数
counter = itertools.count(10, 2) # 从10开始步长2
print(next(counter)) # 10
print(next(counter)) # 12
print(next(counter)) # 14

# cycle循环迭代
cycler = itertools.cycle(['A', 'B', 'C'])
print(next(cycler)) # A
print(next(cycler)) # B
print(next(cycler)) # C
print(next(cycler)) # A

# repeat重复元素
repeater = itertools.repeat('Hello', 3)
print(list(repeater)) # ['Hello', 'Hello', 'Hello']

组合迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import itertools

# product笛卡尔积
result = itertools.product('AB', '12')
print(list(result)) # [('A', '1'), ('A', '2'), ('B', '1'), ('B', '2')]

# permutations排列
result = itertools.permutations('ABC', 2)
print(list(result)) # [('A', 'B'), ('A', 'C'), ('B', 'A'), ...]

# combinations组合
result = itertools.combinations('ABC', 2)
print(list(result)) # [('A', 'B'), ('A', 'C'), ('B', 'C')]

# combinations_with_replacement可重复组合
result = itertools.combinations_with_replacement('ABC', 2)
print(list(result)) # [('A', 'A'), ('A', 'B'), ...]

其他工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import itertools

# chain连接多个迭代器
result = itertools.chain([1, 2], [3, 4], [5, 6])
print(list(result)) # [1, 2, 3, 4, 5, 6]

# groupby分组
data = [('A', 1), ('A', 2), ('B', 1), ('B', 2)]
for key, group in itertools.groupby(data, key=lambda x: x[0]):
print(f"{key}: {list(group)}")

# accumulate累积计算
result = itertools.accumulate([1, 2, 3, 4])
print(list(result)) # [1, 3, 6, 10]

# filterfalse过滤假值
result = itertools.filterfalse(lambda x: x % 2 == 0, range(10))
print(list(result)) # [1, 3, 5, 7, 9]

序列化

pickle 模块用于序列化对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pickle

# 序列化
data = {
'name': 'Alice',
'age': 25,
'hobbies': ['reading', 'coding']
}

# 保存到文件
with open('data.pkl', 'wb') as f:
pickle.dump(data, f)

# 从文件加载
with open('data.pkl', 'rb') as f:
loaded_data = pickle.load(f)
print(loaded_data)

# 序列化到字符串
serialized = pickle.dumps(data)
deserialized = pickle.loads(serialized)

工具类

collections 模块提供了一些数据结构用于处理数据

Counter

💗💗 Counter 用于计数可哈希对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from collections import Counter

# 创建计数器
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
counter = Counter(words)
print(counter) # Counter({'apple': 3, 'banana': 2, 'orange': 1})

# 统计字符串
text = "hello world"
char_counter = Counter(text)
print(char_counter)

# 常用方法
print(counter.most_common(2)) # 最常见的2个[('apple', 3), ('banana', 2)]
print(counter['apple']) # 3

# 更新计数器
counter.update(["apple", "grape"])
print(counter)

# 算术运算
c1 = Counter(a=3, b=1)
c2 = Counter(a=1, b=2)
print(c1 + c2) # Counter({'a': 4, 'b': 3})
print(c1 - c2) # Counter({'a': 2})

defaultdict

💗💗 defaultdict 提供默认值的字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from collections import defaultdict

# 默认值为列表
dd_list = defaultdict(list)
dd_list['fruits'].append('apple')
dd_list['fruits'].append('banana')
print(dd_list) # defaultdict(<class 'list'>, {'fruits': ['apple', 'banana']})

# 默认值为整数
dd_int = defaultdict(int)
dd_int['count'] += 1
dd_int['count'] += 1
print(dd_int) # defaultdict(<class 'int'>, {'count': 2})

# 默认值为字典
dd_dict = defaultdict(dict)
dd_dict['user1']['name'] = 'Alice'
dd_dict['user1']['age'] = 25
print(dd_dict)

namedtuple

💗💗 namedtuple 创建具名元组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from collections import namedtuple

# 定义具名元组
Person = namedtuple('Person', ['name', 'age', 'city'])

# 创建实例
person = Person("Alice", 25, "Beijing")
print(person.name) # Alice
print(person.age) # 25
print(person.city) # Beijing

# 转换为字典
print(person._asdict()) # OrderedDict([('name', 'Alice'), ...])

# 替换字段
new_person = person._replace(age=26)
print(new_person)

deque

💗💗 deque 是双端队列支持高效的首尾操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from collections import deque

# 创建双端队列
dq = deque([1, 2, 3, 4, 5])

# 尾部添加
dq.append(6)
print(dq) # deque([1, 2, 3, 4, 5, 6])

# 头部添加
dq.appendleft(0)
print(dq) # deque([0, 1, 2, 3, 4, 5, 6])

# 尾部弹出
last = dq.pop()
print(last) # 6

# 头部弹出
first = dq.popleft()
print(first) # 0

# 限制长度
dq = deque(maxlen=3)
dq.extend([1, 2, 3, 4, 5])
print(dq) # deque([3, 4, 5], maxlen=3)

文件目录

os 模块pathlib 模块提供了与操作系统交互的功能主要用于文件和目录操作

基本操作

💗💗 os 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os

# 获取当前工作目录
current_dir = os.getcwd()
print(f"当前目录{current_dir}")

# 切换目录
os.chdir('/path/to/directory')

# 列出目录内容
files = os.listdir('.')
print(f"目录内容{files}")

# 创建目录
os.mkdir('new_folder') # 创建单级目录
os.makedirs('parent/child') # 创建多级目录

# 删除目录
os.rmdir('empty_folder') # 删除空目录
os.removedirs('parent/child') # 删除多级空目录

# 删除文件
os.remove('file.txt')

# 重命名文件或目录
os.rename('old_name.txt', 'new_name.txt')

💗💗 pathlib 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from pathlib import Path

# 创建路径对象
path = Path('folder/file.txt')
absolute_path = Path('/home/user/file.txt')

# 当前目录
current = Path.cwd()
print(current)

# 家目录
home = Path.home()
print(home)


path = Path('test.txt')

# 创建文件
path.touch()

# 写入文件
path.write_text('Hello, World!', encoding='utf-8')

# 读取文件
content = path.read_text(encoding='utf-8')
print(content)

# 删除文件
path.unlink()

# 创建目录
dir_path = Path('new_folder')
dir_path.mkdir(exist_ok=True)

# 删除目录
dir_path.rmdir()

# 遍历目录
for file in Path('.').glob('*.txt'):
print(file)

# 递归遍历
for file in Path('.').rglob('*.py'):
print(file)

路径操作

💗💗 os 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os

# 路径拼接跨平台兼容
path = os.path.join('folder', 'subfolder', 'file.txt')
print(path) # folder/subfolder/file.txt (Linux/Mac)
# folder\subfolder\file.txt (Windows)

# 路径判断
print(os.path.exists('file.txt')) # 是否存在
print(os.path.isfile('file.txt')) # 是否是文件
print(os.path.isdir('folder')) # 是否是目录
print(os.path.isabs('/absolute/path')) # 是否是绝对路径

# 路径信息
print(os.path.abspath('file.txt')) # 绝对路径
print(os.path.dirname('/path/file')) # 目录部分/path
print(os.path.basename('/path/file')) # 文件名部分file
print(os.path.splitext('file.txt')) # 分割扩展名('file', '.txt')

# 文件大小
size = os.path.getsize('file.txt')
print(f"文件大小{size} bytes")

💗💗 os.path 模块

1
2
3
4
5
6
7
8
9
10
11
12
import os.path

# 常用函数
os.path.join('a', 'b', 'c') # 路径拼接
os.path.exists('file.txt') # 检查存在
os.path.isfile('file.txt') # 是否文件
os.path.isdir('folder') # 是否目录
os.path.getsize('file.txt') # 文件大小
os.path.abspath('file.txt') # 绝对路径
os.path.dirname('/path/file') # 目录名
os.path.basename('/path/file') # 文件名
os.path.splitext('file.txt') # 分割扩展名

💗💗 pathlib 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from pathlib import Path

path = Path('folder/subfolder/file.txt')

# 路径属性
print(path.name) # file.txt
print(path.stem) # file
print(path.suffix) # .txt
print(path.parent) # folder/subfolder
print(path.parts) # ('folder', 'subfolder', 'file.txt')

# 路径判断
print(path.exists()) # 是否存在
print(path.is_file()) # 是否文件
print(path.is_dir()) # 是否目录
print(path.is_absolute()) # 是否绝对路径

# 路径拼接
new_path = path.parent / 'new_file.txt'
print(new_path) # folder/subfolder/new_file.txt

环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os

# 获取环境变量
home = os.environ.get('HOME') # Linux/Mac
home = os.environ.get('USERPROFILE') # Windows
path = os.environ.get('PATH')

# 设置环境变量
os.environ['MY_VAR'] = 'my_value'

# 删除环境变量
del os.environ['MY_VAR']

# 获取系统信息
print(os.name) # 操作系统名称'posix', 'nt', 'java'
print(os.sep) # 路径分隔符'/' 或 '\'
print(os.linesep) # 行分隔符

压缩文件

ZIP 文件操作

zipfile 模块提供了与 ZIP 文件交互的功能如创建读取写入 ZIP 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import zipfile

# 创建 ZIP 文件
with zipfile.ZipFile('archive.zip', 'w') as zipf:
zipf.write('file1.txt')
zipf.write('file2.txt')

# 解压 ZIP 文件
with zipfile.ZipFile('archive.zip', 'r') as zipf:
zipf.extractall('extracted/')

# 查看 ZIP 内容
with zipfile.ZipFile('archive.zip', 'r') as zipf:
for info in zipf.infolist():
print(f"{info.filename}: {info.file_size} bytes")

TAR 文件操作

tarfile 模块用于操作 TAR 文件

1
2
3
4
5
6
7
8
9
import tarfile

# 创建 TAR.GZ 文件
with tarfile.open('archive.tar.gz', 'w:gz') as tar:
tar.add('folder/')

# 解压 TAR.GZ 文件
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
tar.extractall('extracted/')

临时文件

tempfile 模块提供了创建临时文件的功能如创建临时文件临时目录等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tempfile

# 创建临时文件
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
f.write('临时内容')
temp_path = f.name
print(f"临时文件{temp_path}")

# 创建临时目录
with tempfile.TemporaryDirectory() as tmpdir:
print(f"临时目录{tmpdir}")
# 在目录中创建文件
temp_file = f"{tmpdir}/test.txt"
with open(temp_file, 'w') as f:
f.write('测试内容')
# 退出 with 块后自动清理

配置文件

configparser 模块用于处理 INI 格式的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import configparser

# 创建配置
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45'}
config['database'] = {
'host': 'localhost',
'port': '3306',
'user': 'root'
}
config['server'] = {
'bind_address': '0.0.0.0',
'port': '8080'
}

# 写入文件
with open('config.ini', 'w') as f:
config.write(f)

# 读取配置
config = configparser.ConfigParser()
config.read('config.ini')

print(config['database']['host']) # localhost
print(config['server']['port']) # 8080

系统相关

sys 模块提供了与 Python 解释器的交互功能如获取 Python 版本命令行参数环境变量等

系统信息

1
2
3
4
5
6
7
8
9
10
11
import sys

# Python 版本信息
print(sys.version) # 完整版本信息
print(sys.version_info) # 版本元组sys.version_info(major=3, minor=11, ...)
print(sys.platform) # 平台标识'win32', 'linux', 'darwin'

# 解释器信息
print(sys.executable) # Python 解释器路径
print(sys.prefix) # 安装前缀
print(sys.byteorder) # 字节序'little' 或 'big'

命令行参数

1
2
3
4
5
6
7
8
9
10
11
12
import sys

# 获取命令行参数
print(f"脚本名称{sys.argv[0]}")
print(f"参数列表{sys.argv}")

# 示例python script.py arg1 arg2 arg3
# sys.argv = ['script.py', 'arg1', 'arg2', 'arg3']

if len(sys.argv) > 1:
for i, arg in enumerate(sys.argv[1:], 1):
print(f"参数 {i}: {arg}")

标准输入输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sys

# 标准输出
sys.stdout.write("Hello, World!\n")
print("Hello, World!", file=sys.stdout)

# 标准错误
sys.stderr.write("Error message\n")
print("Error message", file=sys.stderr)

# 标准输入
user_input = sys.stdin.readline()
print(f"你输入了{user_input.strip()}")

# 刷新输出缓冲区
sys.stdout.flush()

退出程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import sys

# 正常退出
sys.exit(0)

# 异常退出
sys.exit(1)

# 带消息退出
sys.exit("程序出错")

# 在 try-except 中使用
try:
# 可能出错的代码
result = 1 / 0
except Exception as e:
print(f"错误{e}")
sys.exit(1)

模块搜索路径

1
2
3
4
5
6
7
8
9
10
11
import sys

# 查看模块搜索路径
print(sys.path)

# 添加自定义路径
sys.path.append('/path/to/my/modules')
sys.path.insert(0, '/priority/path')

# 移除路径
sys.path.remove('/unwanted/path')

日期时间

datetime 模块 time 模块提供了与日期和时间相关的功能如获取当前时间日期时间间隔等

基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from datetime import datetime, date, time, timedelta

# 当前日期和时间
now = datetime.now()
print(f"当前时间{now}")
print(f"年份{now.year}")
print(f"月份{now.month}")
print(f"日期{now.day}")
print(f"小时{now.hour}")
print(f"分钟{now.minute}")
print(f"秒数{now.second}")

# 当前日期
today = date.today()
print(f"今天{today}")

# 创建特定日期
birthday = date(1990, 5, 15)
print(f"生日{birthday}")

# 创建特定时间
meeting = time(14, 30, 0)
print(f"会议时间{meeting}")

格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from datetime import datetime

now = datetime.now()

# 格式化为字符串
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted) # 2024-01-15 14:30:00

formatted = now.strftime("%Y年%m月%d日 %H时%M分")
print(formatted) # 2024年01月15日 14时30分

# 常用格式代码
# %Y - 四位年份
# %m - 月份01-12
# %d - 日期01-31
# %H - 小时00-23
# %M - 分钟00-59
# %S - 秒00-59
# %A - 星期全称
# %B - 月份全称

# 解析字符串为日期
date_string = "2024-01-15 14:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print(parsed_date)

日期运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from datetime import datetime, timedelta

now = datetime.now()

# 日期加减
tomorrow = now + timedelta(days=1)
yesterday = now - timedelta(days=1)
next_week = now + timedelta(weeks=1)
next_hour = now + timedelta(hours=1)

print(f"明天{tomorrow}")
print(f"昨天{yesterday}")

# 计算日期差
date1 = datetime(2024, 1, 1)
date2 = datetime(2024, 12, 31)
diff = date2 - date1
print(f"相差 {diff.days} 天")

# 比较日期
if tomorrow > now:
print("明天在未来")

时区处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from datetime import datetime, timezone, timedelta

# UTC 时间
utc_now = datetime.now(timezone.utc)
print(f"UTC 时间{utc_now}")

# 创建时区UTC+8
beijing_tz = timezone(timedelta(hours=8))
beijing_time = datetime.now(beijing_tz)
print(f"北京时间{beijing_time}")

# 时区转换
utc_time = datetime.now(timezone.utc)
beijing_time = utc_time.astimezone(timezone(timedelta(hours=8)))
print(f"转换为北京时间{beijing_time}")

时间操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import time

# 当前时间戳
timestamp = time.time()
print(f"时间戳{timestamp}")

# 格式化时间
local_time = time.localtime()
formatted = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print(f"本地时间{formatted}")

# 休眠
print("等待 2 秒...")
time.sleep(2)
print("完成")

# 性能计时
start = time.perf_counter()
# 执行的代码
end = time.perf_counter()
print(f"耗时{end - start:.4f} 秒")

数学运算

math 模块提供了基本的数学运算功能如求平方根对数幂等等

数学常数

1
2
3
4
5
6
7
8
import math

# 数学常数
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.tau) # 6.283185307179586 (2π)
print(math.inf) # 无穷大
print(math.nan) # NaN

基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import math

# 取整函数
print(math.ceil(3.14)) # 4向上取整
print(math.floor(3.14)) # 3向下取整
print(math.trunc(3.14)) # 3截断小数
print(round(3.14)) # 3四舍五入

# 绝对值
print(math.fabs(-5)) # 5.0

# 最大值和最小值
print(math.fmax(3, 5)) # 5.0
print(math.fmin(3, 5)) # 3.0

幂和对数

1
2
3
4
5
6
7
8
9
10
11
import math

# 幂运算
print(math.pow(2, 10)) # 1024.0
print(math.sqrt(16)) # 4.0平方根
print(math.cbrt(27)) # 3.0立方根Python 3.11+

# 对数
print(math.log(math.e)) # 1.0自然对数
print(math.log10(100)) # 2.0以10为底
print(math.log2(8)) # 3.0以2为底

三角函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import math

# 角度转弧度
angle_deg = 180
angle_rad = math.radians(angle_deg)
print(f"{angle_deg}度 = {angle_rad}弧度")

# 三角函数
print(math.sin(math.pi / 2)) # 1.0
print(math.cos(math.pi)) # -1.0
print(math.tan(math.pi / 4)) # 1.0

# 反三角函数
print(math.asin(1)) # 1.5707963267948966 (π/2)
print(math.acos(0)) # 1.5707963267948966
print(math.atan(1)) # 0.7853981633974483 (π/4)

其他函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import math

# 阶乘
print(math.factorial(5)) # 120

# 最大公约数
print(math.gcd(12, 8)) # 4

# 最小公倍数Python 3.9+
print(math.lcm(4, 6)) # 12

# 判断特殊值
print(math.isinf(float('inf'))) # True
print(math.isnan(float('nan'))) # True
print(math.isfinite(1.0)) # True

哈希计算

hashlib 模块提供了哈希计算功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import hashlib

# MD5
text = "Hello, World!"
md5_hash = hashlib.md5(text.encode()).hexdigest()
print(f"MD5: {md5_hash}")

# SHA256
sha256_hash = hashlib.sha256(text.encode()).hexdigest()
print(f"SHA256: {sha256_hash}")

# SHA512
sha512_hash = hashlib.sha512(text.encode()).hexdigest()
print(f"SHA512: {sha512_hash}")

# 文件哈希
def file_hash(filepath, algorithm='sha256'):
hash_func = hashlib.new(algorithm)
with open(filepath, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_func.update(chunk)
return hash_func.hexdigest()

# print(file_hash('example.txt'))

日志处理

logging 模块提供了日志处理功能

基本日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import logging

# 基本配置
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='app.log',
filemode='w'
)

# 创建 logger
logger = logging.getLogger(__name__)

# 记录日志
logger.debug('调试信息')
logger.info('普通信息')
logger.warning('警告信息')
logger.error('错误信息')
logger.critical('严重错误')

高级配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import logging

# 创建 logger
logger = logging.getLogger('my_app')
logger.setLevel(logging.DEBUG)

# 创建处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)

file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.DEBUG)

# 创建格式化器
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)

# 添加处理器
logger.addHandler(console_handler)
logger.addHandler(file_handler)

# 使用
logger.info('应用启动')
logger.error('发生错误')

高阶函数

functools 模块提供了高阶函数reducepartial

reduce

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from functools import reduce

# reduce累积应用函数
numbers = [1, 2, 3, 4, 5]

# 求和
total = reduce(lambda x, y: x + y, numbers)
print(total) # 15

# 求积
product = reduce(lambda x, y: x * y, numbers)
print(product) # 120

# 带初始值
total = reduce(lambda x, y: x + y, numbers, 10)
print(total) # 25

lru_cache

1
2
3
4
5
6
7
8
9
10
11
from functools import lru_cache

# 缓存装饰器
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(10)) # 55
print(fibonacci.cache_info()) # 缓存信息

partial

1
2
3
4
5
6
7
8
9
10
11
12
13
from functools import partial

# 偏函数
def power(base, exponent):
return base ** exponent

# 创建平方函数
square = partial(power, exponent=2)
print(square(5)) # 25

# 创建立方函数
cube = partial(power, exponent=3)
print(cube(5)) # 125

正则表达式

re 模块提供了正则表达式功能如匹配替换查找等等

基本匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re

# 基本匹配
text = "Hello, World!"
match = re.search(r'World', text)
if match:
print(f"找到匹配{match.group()}")
print(f"起始位置{match.start()}")
print(f"结束位置{match.end()}")

# 匹配开头
match = re.match(r'Hello', text)
if match:
print("匹配开头成功")

# 完全匹配
match = re.fullmatch(r'Hello, World!', text)
if match:
print("完全匹配成功")

查找字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import re

text = "我有3个苹果和5个橙子还有10个香蕉"

# 查找所有数字
numbers = re.findall(r'\d+', text)
print(numbers) # ['3', '5', '10']

# 查找所有单词
words = re.findall(r'\w+', text)
print(words)

# 迭代查找
for match in re.finditer(r'\d+', text):
print(f"找到 {match.group()} 在位置 {match.start()}-{match.end()}")

替换字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re

text = "我有3个苹果和5个橙子"

# 替换所有数字
new_text = re.sub(r'\d+', 'X', text)
print(new_text) # 我有X个苹果和X个橙子

# 限制替换次数
new_text = re.sub(r'\d+', 'X', text, count=1)
print(new_text) # 我有X个苹果和5个橙子

# 使用函数进行替换
def double_num(match):
num = int(match.group())
return str(num * 2)

new_text = re.sub(r'\d+', double_num, text)
print(new_text) # 我有6个苹果和10个橙子

分割字符串

1
2
3
4
5
6
7
import re

text = "apple,banana;cherry date"

# 按多种分隔符分割
fruits = re.split(r'[,\s;]+', text)
print(fruits) # ['apple', 'banana', 'cherry', 'date']

编译正则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
import re

# 编译正则提高重复使用性能
pattern = re.compile(r'\d+')

# 使用编译后的模式
text = "我有3个苹果和5个橙子"
matches = pattern.findall(text)
print(matches) # ['3', '5']

# 带标志的编译
pattern = re.compile(r'hello', re.IGNORECASE)
print(pattern.search("HELLO WORLD")) # 找到匹配

常用正则语法

语法描述示例
.匹配任意字符除换行a.c 匹配 "abc"
\d匹配数字\d+ 匹配 "123"
\w匹配字母数字下划线\w+ 匹配 "hello"
\s匹配空白字符\s+ 匹配空格
*匹配 0 次或多次ab* 匹配 "a", "ab", "abb"
+匹配 1 次或多次ab+ 匹配 "ab", "abb"
?匹配 0 次或 1 次ab? 匹配 "a", "ab"
{n}匹配 n 次a{3} 匹配 "aaa"
[abc]匹配 ab 或 c[aeiou] 匹配元音
^匹配字符串开头^Hello
$匹配字符串结尾World$

进程与线程

进程管理

subprocess 模块提供了子进程管理功能

💗💗 基本用法

1
2
3
4
5
6
7
8
9
10
11
import subprocess

# 执行简单命令
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
print(result.returncode)

# 执行 shell 命令
result = subprocess.run('echo Hello', shell=True, capture_output=True, text=True)
print(result.stdout)

💗💗 高级用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import subprocess

# 带输入的命令
result = subprocess.run(
['python', '-c', 'print(input())'],
input='Hello\n',
capture_output=True,
text=True
)
print(result.stdout)

# 超时控制
try:
result = subprocess.run(
['sleep', '10'],
timeout=2,
capture_output=True
)
except subprocess.TimeoutExpired:
print("命令超时")

# 实时输出
process = subprocess.Popen(
['ping', 'www.baidu.com'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)

for line in process.stdout:
print(line, end='')

线程管理

threading 模块提供了线程管理功能

💗💗 基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import threading
import time

# 定义线程函数
def worker(name, duration):
print(f"线程 {name} 开始")
time.sleep(duration)
print(f"线程 {name} 结束")

# 创建线程
thread1 = threading.Thread(target=worker, args=("Thread-1", 2))
thread2 = threading.Thread(target=worker, args=("Thread-2", 3))

# 启动线程
thread1.start()
thread2.start()

# 等待线程完成
thread1.join()
thread2.join()

print("所有线程完成")

💗💗 线程锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import threading

# 共享资源
counter = 0
lock = threading.Lock()

def increment():
global counter
for _ in range(100000):
with lock: # 自动获取和释放锁
counter += 1

# 创建多个线程
threads = []
for _ in range(10):
t = threading.Thread(target=increment)
threads.append(t)
t.start()

# 等待所有线程
for t in threads:
t.join()

print(f"计数器值{counter}") # 1000000

并发处理

concurrent.futures 模块提供了进程池和线程池用于并发处理任务

💗💗 ProcessPoolExecutor 进程池

1
2
3
4
5
6
7
8
9
10
11
from concurrent.futures import ProcessPoolExecutor
import os

def square(n):
return n * n

# 进程池
with ProcessPoolExecutor(max_workers=4) as executor:
numbers = [1, 2, 3, 4, 5]
results = executor.map(square, numbers)
print(list(results)) # [1, 4, 9, 16, 25]

💗💗 ThreadPoolExecutor 线程池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from concurrent.futures import ThreadPoolExecutor, as_completed
import time

def task(name, duration):
time.sleep(duration)
return f"任务 {name} 完成耗时 {duration}秒"

# 线程池
with ThreadPoolExecutor(max_workers=3) as executor:
# 提交任务
futures = {executor.submit(task, f"Task-{i}", i): i for i in range(1, 6)}

# 获取结果
for future in as_completed(futures):
result = future.result()
print(result)

命令行参数

argparse 模块用于处理命令行参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import argparse

# 创建解析器
parser = argparse.ArgumentParser(description='示例程序')

# 添加参数
parser.add_argument('name', help='用户名')
parser.add_argument('-a', '--age', type=int, help='年龄')
parser.add_argument('-c', '--city', default='Beijing', help='城市')
parser.add_argument('-v', '--verbose', action='store_true', help='详细模式')

# 解析参数
args = parser.parse_args()

# 使用参数
print(f"姓名{args.name}")
print(f"年龄{args.age}")
print(f"城市{args.city}")
print(f"详细模式{args.verbose}")

# 使用python script.py Alice -a 25 -c Shanghai -v

JSON 模块

json 模块提供了 JSON 数据处理功能

序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import json

# Python 对象转 JSON 字符串
data = {
"name": "Alice",
"age": 25,
"city": "Beijing",
"hobbies": ["reading", "coding"]
}

json_string = json.dumps(data)
print(json_string)

# 格式化输出美化
json_pretty = json.dumps(data, indent=2, ensure_ascii=False)
print(json_pretty)

# 写入 JSON 文件
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)

反序列化

1
2
3
4
5
6
7
8
9
10
11
12
import json

# JSON 字符串转 Python 对象
json_string = '{"name": "Alice", "age": 25}'
data = json.loads(json_string)
print(data["name"]) # Alice
print(data["age"]) # 25

# 从文件读取 JSON
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
print(data)

类型映射

Python 类型JSON 类型
dictobject
list, tuplearray
strstring
int, floatnumber
Truetrue
Falsefalse
Nonenull

自定义编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import json
from datetime import datetime

# 自定义编码器
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime("%Y-%m-%d %H:%M:%S")
return super().default(obj)

data = {
"name": "Alice",
"created_at": datetime.now()
}

json_string = json.dumps(data, cls=CustomEncoder, ensure_ascii=False)
print(json_string)

UUID 模块

uuid 模块用于生成和操作 UUIDUniversally Unique Identifier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import uuid

# UUID1基于时间和MAC地址
uuid1 = uuid.uuid1()
print(f"UUID1: {uuid1}")

# UUID4随机生成最常用
uuid4 = uuid.uuid4()
print(f"UUID4: {uuid4}")

# 转换为字符串
uuid_str = str(uuid4)
print(f"字符串{uuid_str}")

# 从字符串解析
parsed_uuid = uuid.UUID(uuid_str)
print(f"解析{parsed_uuid}")

COPY 模块

copy 模块提供了对象复制功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import copy

# 原始列表
original = [[1, 2], [3, 4]]

# 浅拷贝
shallow = copy.copy(original)
shallow[0][0] = 99
print(original) # [[99, 2], [3, 4]]受影响

# 深拷贝
original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original)
deep[0][0] = 99
print(original) # [[1, 2], [3, 4]]不受影响

CSV 模块

csv 模块用于读写 CSV 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import csv

# 写入 CSV
data = [
['姓名', '年龄', '城市'],
['Alice', 25, 'Beijing'],
['Bob', 30, 'Shanghai'],
['Charlie', 35, 'Guangzhou']
]

with open('data.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)

# 读取 CSV
with open('data.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)

# 使用 DictWriter
with open('data.csv', 'w', newline='', encoding='utf-8') as f:
fieldnames = ['姓名', '年龄', '城市']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'姓名': 'Alice', '年龄': 25, '城市': 'Beijing'})

第三方库

Python 拥有丰富的第三方库生态系统PyPIPython Package Index上收录了数十万个高质量的第三方库涵盖了 Web 开发数据科学人工智能网络爬虫自动化测试等各个领域

包管理工具

安装与配置

pip 是 Python 的包管理工具用于安装和管理第三方库

1
2
3
4
5
6
# 检查 pip 版本
pip --version
pip3 --version

# 升级 pip
pip install --upgrade pip

国内镜像源

使用国内镜像源可以加速包的下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 列出配置
pip config list

# 临时使用
pip install package -i https://mirrors.aliyun.com/pypi/simple

# 永久配置
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

# 查看当前数据源
pip config get global.index-url

# 常用镜像源
# 清华大学https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里云https://mirrors.aliyun.com/pypi/simple
# 豆瓣https://pypi.douban.com/simple
# 中国科技大学https://pypi.mirrors.ustc.edu.cn/simple

批量安装依赖

1
2
3
4
5
6
7
8
# 从 requirements.txt 安装
pip install -r requirements.txt

# 生成 requirements.txt
pip freeze > requirements.txt

# 更新所有包
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U

基本命令

命令描述
pip install package安装包
pip uninstall package卸载包
pip list列出已安装的包
pip show package显示包的详细信息
pip search keyword搜索包已废弃
pip freeze以 requirements 格式列出已安装的包
pip install -r requirements.txt从文件安装依赖
1
2
3
4
5
6
7
# 常用操作示例
pip install requests # 安装 requests 库
pip install numpy==1.21.0 # 安装指定版本
pip install "django>=3.0,<4.0" # 安装版本范围
pip uninstall requests # 卸载包
pip list # 查看所有已安装的包
pip freeze > requirements.txt # 导出依赖列表

虚拟环境

虚拟环境可以为每个项目创建独立的 Python 环境避免依赖冲突

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建虚拟环境
python -m venv myenv

# 激活虚拟环境
# Windows
myenv\Scripts\activate
# macOS/Linux
source myenv/bin/activate

# 退出虚拟环境
deactivate

# 在虚拟环境中安装包
pip install requests
pip freeze > requirements.txt

Web 开发框架

Django

Django 是一个功能强大的全栈 Web 框架遵循”电池included”理念

1
2
3
4
5
pip install django
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
python manage.py runserver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# views.py
from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello, Django!")

# urls.py
from django.urls import path
from . import views

urlpatterns = [
path('hello/', views.hello, name='hello'),
]

# models.py
from django.db import models

class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.title

Flask

Flask 是一个轻量级的微框架灵活且易于扩展

1
pip install flask
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
return 'Hello, Flask!'

@app.route('/user/<name>')
def user(name):
return f'Hello, {name}!'

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
return f'Welcome, {username}!'
return render_template('login.html')

if __name__ == '__main__':
app.run(debug=True)

FastAPI

FastAPI 是一个现代高性能的 API 框架基于 Python 类型提示

1
pip install fastapi uvicorn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
name: str
price: float
description: str = None

@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

@app.post("/items/")
def create_item(item: Item):
return {"item": item.dict()}

# 运行uvicorn main:app --reload

Streamlit

Streamlit 是一个快速构建数据 Web 应用的框架无需前端开发经验

💗💗 安装与使用

1
pip install streamlit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# app.py
import streamlit as st
import pandas as pd
import numpy as np

# 页面标题
st.title('📊 数据分析应用')
st.write('这是一个使用 Streamlit 构建的数据分析示例')

# 侧边栏
st.sidebar.header('配置选项')
option = st.sidebar.selectbox(
'选择图表类型',
['折线图', '柱状图', '散点图']
)

# 生成示例数据
data = pd.DataFrame({
'日期': pd.date_range('2024-01-01', periods=100),
'数值': np.random.randn(100).cumsum()
})

# 显示数据
st.subheader('原始数据')
st.dataframe(data.head(10))

# 绘制图表
st.subheader('数据可视化')
if option == '折线图':
st.line_chart(data.set_index('日期'))
elif option == '柱状图':
st.bar_chart(data.set_index('日期'))
else:
st.scatter_chart(data, x='日期', y='数值')

# 滑块控制
threshold = st.slider('选择阈值', 0.0, 100.0, 50.0)
st.write(f'当前阈值{threshold}')

# 按钮交互
if st.button('刷新数据'):
st.rerun()

💗💗 运行应用

1
streamlit run app.py

💗💗 常用组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st

# 文本组件
st.title('标题')
st.header('二级标题')
st.subheader('三级标题')
st.text('普通文本')
st.markdown('**加粗** *斜体*')

# 输入组件
name = st.text_input('姓名')
age = st.number_input('年龄', min_value=0, max_value=150)
date = st.date_input('选择日期')
color = st.color_picker('选择颜色')

# 选择组件
option = st.selectbox('单选', ['选项1', '选项2', '选项3'])
options = st.multiselect('多选', ['A', 'B', 'C', 'D'])
agree = st.checkbox('同意条款')

# 文件上传
uploaded_file = st.file_uploader('上传文件', type=['csv', 'xlsx'])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.dataframe(df)

# 进度条和状态
progress_bar = st.progress(0)
for i in range(100):
progress_bar.progress(i + 1)

st.success('操作成功')
st.error('发生错误')
st.warning('警告信息')
st.info('提示信息')

# 布局
col1, col2, col3 = st.columns(3)
with col1:
st.metric('销售额', '¥100,000', '+10%')
with col2:
st.metric('订单数', '1,234', '-5%')
with col3:
st.metric('用户数', '567', '+15%')

# 缓存优化
@st.cache_data
def load_data():
# 耗时操作
return pd.read_csv('large_file.csv')

df = load_data() # 首次调用后会被缓存

数据科学库

NumPy

NumPy 是 Python 科学计算的基础库提供高性能的多维数组对象

1
pip install numpy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np

# 创建数组
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# 特殊数组
zeros = np.zeros((3, 3)) # 全零数组
ones = np.ones((2, 4)) # 全一数组
random = np.random.rand(3, 3) # 随机数组

# 数组运算
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [4 10 18]
print(np.dot(a, b)) # 32点积

# 统计函数
data = np.array([1, 2, 3, 4, 5])
print(np.mean(data)) # 3.0
print(np.std(data)) # 标准差
print(np.max(data)) # 5
print(np.min(data)) # 1

Pandas

Pandas 是数据分析的核心库提供 DataFrame 数据结构

1
pip install pandas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd

# 创建 DataFrame
data = {
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['Beijing', 'Shanghai', 'Guangzhou']
}
df = pd.DataFrame(data)

# 读取 CSV 文件
df = pd.read_csv('data.csv')

# 数据查看
print(df.head()) # 前5行
print(df.info()) # 基本信息
print(df.describe()) # 统计信息

# 数据筛选
young_people = df[df['age'] < 30]
beijing_people = df[df['city'] == 'Beijing']

# 数据统计
avg_age = df['age'].mean()
age_count = df['age'].value_counts()

# 数据分组
grouped = df.groupby('city')['age'].mean()

# 数据合并
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'C': [7, 8]})
merged = pd.merge(df1, df2, on='A', how='outer')

Matplotlib

Matplotlib 是 Python 最流行的绘图库

1
pip install matplotlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import matplotlib.pyplot as plt
import numpy as np

# 折线图
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

# 散点图
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y)
plt.title('Scatter Plot')
plt.show()

# 柱状图
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 25]
plt.bar(categories, values)
plt.title('Bar Chart')
plt.show()

# 子图
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x, np.sin(x))
ax2.plot(x, np.cos(x))
plt.show()

Seaborn

Seaborn 基于 Matplotlib提供更美观的统计图形

1
pip install seaborn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import seaborn as sns
import matplotlib.pyplot as plt

# 加载示例数据集
tips = sns.load_dataset('tips')

# 分布图
sns.histplot(data=tips, x='total_bill')
plt.show()

# 关系图
sns.scatterplot(data=tips, x='total_bill', y='tip', hue='sex')
plt.show()

# 箱线图
sns.boxplot(data=tips, x='day', y='total_bill')
plt.show()

人工智能库

TensorFlow

TensorFlow 是 Google 开发的深度学习框架

1
pip install tensorflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf
from tensorflow import keras

# 创建模型
model = keras.Sequential([
keras.layers.Dense(128, activation='relu', input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# 训练模型
# model.fit(x_train, y_train, epochs=5)

# 预测
# predictions = model.predict(x_test)

PyTorch

PyTorch 是 Facebook 开发的深度学习框架以动态计算图著称

1
pip install torch torchvision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
import torch.nn as nn
import torch.optim as optim

# 定义模型
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10)
)

def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits

# 创建模型实例
model = NeuralNetwork()

# 损失函数和优化器
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

Scikit-learn

Scikit-learn 是机器学习算法库提供分类回归聚类等算法

1
pip install scikit-learn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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, y = iris.data, iris.target

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# 创建模型
knn = KNeighborsClassifier(n_neighbors=3)

# 训练模型
knn.fit(X_train, y_train)

# 预测
y_pred = knn.predict(X_test)

# 评估
accuracy = accuracy_score(y_test, y_pred)
print(f'准确率{accuracy:.2f}')

网络爬虫库

Requests

Requests 是最流行的 HTTP 库简化 HTTP 请求

1
pip install requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import requests

# GET 请求
response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())

# POST 请求
data = {'key': 'value'}
response = requests.post('https://httpbin.org/post', data=data)

# 带参数的请求
params = {'q': 'python', 'page': 1}
response = requests.get('https://api.example.com/search', params=params)

# 设置请求头
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('https://example.com', headers=headers)

# 会话保持
session = requests.Session()
session.get('https://example.com/login')
response = session.get('https://example.com/profile')

BeautifulSoup

BeautifulSoup 是 HTML 解析库用于提取网页数据

1
pip install beautifulsoup4 lxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from bs4 import BeautifulSoup
import requests

# 获取网页
response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'lxml')

# 查找元素
title = soup.find('title')
print(title.text)

# 查找所有链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))

# CSS 选择器
articles = soup.select('div.article')
for article in articles:
heading = article.select_one('h2')
print(heading.text)

# 提取属性
img = soup.find('img')
print(img.get('src'))

Scrapy

Scrapy 是强大的爬虫框架适合大规模数据抓取

1
pip install scrapy
1
2
3
4
scrapy startproject myproject
cd myproject
scrapy genspider example example.com
scrapy crawl example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# spiders/example.py
import scrapy

class ExampleSpider(scrapy.Spider):
name = 'example'
allowed_domains = ['example.com']
start_urls = ['https://example.com']

def parse(self, response):
# 提取数据
for article in response.css('div.article'):
yield {
'title': article.css('h2::text').get(),
'link': article.css('a::attr(href)').get(),
}

# 跟进下一页
next_page = response.css('a.next::attr(href)').get()
if next_page:
yield response.follow(next_page, self.parse)

其他常用库

Pillow

Pillow 是图像处理库

1
pip install Pillow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from PIL import Image

# 打开图片
img = Image.open('photo.jpg')

# 调整大小
img_resized = img.resize((800, 600))

# 旋转
img_rotated = img.rotate(45)

# 转换格式
img.save('photo.png', 'PNG')

# 添加滤镜
from PIL import ImageFilter
img_blur = img.filter(ImageFilter.BLUR)

OpenCV

OpenCV 是计算机视觉库

1
pip install opencv-python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import cv2

# 读取图片
img = cv2.imread('photo.jpg')

# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 边缘检测
edges = cv2.Canny(gray, 100, 200)

# 保存结果
cv2.imwrite('edges.jpg', edges)

# 视频处理
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

SQLAlchemy

SQLAlchemy 是 ORM 数据库工具包

1
pip install sqlalchemy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# 创建引擎
engine = create_engine('sqlite:///example.db')

# 定义基类
Base = declarative_base()

# 定义模型
class User(Base):
__tablename__ = 'users'

id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)

# 创建表
Base.metadata.create_all(engine)

# 创建会话
Session = sessionmaker(bind=engine)
session = Session()

# 添加数据
new_user = User(name='Alice', email='alice@example.com')
session.add(new_user)
session.commit()

# 查询数据
users = session.query(User).all()
for user in users:
print(user.name, user.email)

Pytest

Pytest 是测试框架

1
pip install pytest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# test_example.py
def add(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_string():
assert 'hello'.upper() == 'HELLO'
assert 'hello'.islower() == True

# 运行测试
# pytest test_example.py -v

高级特性

装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 简单装饰器
def my_decorator(func):
def wrapper(*args, **kwargs):
print("函数执行前")
result = func(*args, **kwargs)
print("函数执行后")
return result
return wrapper

@my_decorator
def say_hello(name):
print(f"Hello, {name}!")

say_hello("Alice")

生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
# 生成器函数
def countdown(n):
while n > 0:
yield n
n -= 1

for num in countdown(5):
print(num) # 5, 4, 3, 2, 1

# 生成器表达式
squares = (x**2 for x in range(10))
for square in squares:
print(square)

上下文管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 使用 with 语句
with open('file.txt', 'r') as f:
content = f.read()

# 自定义上下文管理器
class MyContextManager:
def __enter__(self):
print("进入上下文")
return self

def __exit__(self, exc_type, exc_val, exc_tb):
print("退出上下文")
return False

with MyContextManager():
print("在上下文中")

迭代器和生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 自定义迭代器
class CountDown:
def __init__(self, start):
self.current = start

def __iter__(self):
return self

def __next__(self):
if self.current <= 0:
raise StopIteration
else:
self.current -= 1
return self.current + 1

for num in CountDown(5):
print(num) # 5, 4, 3, 2, 1

实用技巧

列表操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 列表去重
my_list = [1, 2, 2, 3, 3, 4]
unique = list(set(my_list))

# 列表反转
reversed_list = my_list[::-1]

# 列表展平
nested = [[1, 2], [3, 4], [5, 6]]
flat = [item for sublist in nested for item in sublist]

# 两个列表合并为字典
keys = ['a', 'b', 'c']
values = [1, 2, 3]
my_dict = dict(zip(keys, values))

字符串操作

1
2
3
4
5
6
7
8
9
10
11
12
# 字符串反转
text = "Hello"
reversed_text = text[::-1]

# 判断回文
def is_palindrome(s):
return s == s[::-1]

# 统计字符频率
from collections import Counter
text = "hello world"
freq = Counter(text)

性能优化

1
2
3
4
5
6
7
8
9
10
11
12
# 使用列表推导式代替循环
# 慢
squares = []
for x in range(1000):
squares.append(x**2)

# 快
squares = [x**2 for x in range(1000)]

# 使用生成器处理大数据
# 节省内存
large_data = (x**2 for x in range(1000000))

常见问题

权限错误

1
2
3
4
5
6
7
8
# Windows以管理员身份运行
# macOS/Linux使用 --user 参数
pip install package --user

# 或使用虚拟环境推荐
python -m venv myenv
source myenv/bin/activate
pip install package

依赖冲突

1
2
3
4
5
6
# 查看依赖树
pip install pipdeptree
pipdeptree

# 解决冲突
pip install package1==1.0 package2==2.0

安装失败

1
2
3
4
5
6
7
8
9
# 清除缓存后重新安装
pip cache purge
pip install package

# 使用 --no-cache-dir
pip install package --no-cache-dir

# 查看详细错误信息
pip install package -v

学习资源

  • 视频
    • Python 零基础入门到大神https://www.bilibili.com/video/BV1qW4y1a7fU
  • 官方资源
    • Python 官方网站: https://www.python.org
    • Python 官方文档: https://docs.python.org/3
    • PyPI 官方网站: https://pypi.org
    • pip 官方文档: https://pip.pypa.io
  • 在线学习
    • 菜鸟教程: https://www.runoob.com/python3
    • 廖雪峰 Python 教程: https://www.liaoxuefeng.com/wiki/1016959663602400
    • Codecademy: https://www.codecademy.com/learn/learn-python-3
  • 第三方库文档
    • Django: https://docs.djangoproject.com
    • Flask: https://flask.palletsprojects.com
    • FastAPI: https://fastapi.tiangolo.com
    • NumPy: https://numpy.org/doc
    • Pandas: https://pandas.pydata.org/docs
    • TensorFlow: https://www.tensorflow.org
    • PyTorch: https://pytorch.org/docs
  • 书籍
    • Python Crash CoursePython 编程从入门到实践
    • Fluent Python流畅的 Python
    • Python CookbookPython 食谱
    • Automate the Boring Stuff with PythonPython 自动化办公
  • 社区
    • Stack Overflow: https://stackoverflow.com/questions/tagged/python
    • Reddit r/Python: https://www.reddit.com/r/Python
    • GitHub: https://github.com/topics/python