Python 概述 Python 是一种解释型、 高级、 通用的编程语言, 由 Guido van Rossum 在 1989 年底开始创作, 并于 1991 年首次发布。 Python 的设计哲学强调代码的可读性 , 其语法允许程序员用更少的代码行表达概念, 相比 C++ 或 Java 等语言更加简洁。
⭐⭐特点:
简单易学: Python 的语法清晰简洁, 接近自然语言, 非常适合初学者入门。
解释型语言: Python 是解释型语言, 代码逐行执行, 便于调试和测试。
跨平台: Python 可以在 Windows、 Linux、 macOS 等多种操作系统上运行。
面向对象: Python 支持面向对象编程, 包括类、 继承、 多态等特性。
动态类型: Python 是动态类型语言, 变量不需要声明类型, 运行时自动推断。
自动内存管理: Python 具有垃圾回收机制, 自动管理内存分配和释放。
丰富的标准库: Python 提供了"电池included"的标准库, 涵盖文件处理、 网络通信、 数据库操作等多个领域。
强大的第三方库: PyPI( Python Package Index) 拥有数十万个第三方库, 几乎可以满足任何开发需求。
可扩展性: Python 可以调用 C/C++ 编写的库, 也可以被嵌入到其他应用程序中。
社区活跃: Python 拥有庞大且活跃的开发者社区, 提供丰富的学习资源和技术支持。
⭐⭐应用领域:
Web 开发: 使用 Django、 Flask、 FastAPI 等框架构建 Web 应用和 API。
数据科学与分析: 使用 NumPy、 Pandas、 Matplotlib 等进行数据处理和可视化。
人工智能与机器学习: 使用 TensorFlow、 PyTorch、 Scikit-learn 等开发 AI 模型。
自动化脚本: 编写系统管理、 文件处理、 网络爬虫等自动化脚本。
桌面应用: 使用 Tkinter、 PyQt、 Kivy 等开发图形界面应用。
游戏开发: 使用 Pygame 等库进行简单的游戏开发。
网络编程: 开发网络服务器、 客户端应用、 网络工具等。
科学计算: 用于物理学、 生物学、 天文学等领域的数值计算和模拟。
环境安装 下载安装 💗💗 Python 官方网站提供了各种操作系统的安装包。 Windows 系统安装
访问 Python 官网下载最新版本的安装包
运行安装程序, 务必勾选”Add Python to PATH”选项
选择”Install Now”进行默认安装, 或选择”Customize installation”自定义安装
安装完成后, 打开命令提示符( CMD) , 输入 python --version 验证安装
macOS 系统安装 1 2 3 4 5 brew install python3
Linux 系统安装 1 2 3 4 5 6 7 8 9 sudo apt update sudo apt install python3 python3-pip sudo yum install python3 python3-pip sudo dnf install python3 python3-pip
验证安装 1 2 3 4 5 6 7 8 9 10 python3 --version pip3 --version python3 >>> print ("Hello, Python!" ) >>> exit ()
开发工具 💗💗 Python 有多种优秀的开发工具可供选择。
Visual Studio Code : 轻量级、 功能强大的代码编辑器, 通过扩展支持 Python 开发
PyCharm : JetBrains 出品的专业 Python IDE, 分为社区版( 免费) 和专业版
Jupyter Notebook : 适合数据科学和机器学习的交互式笔记本环境
Sublime Text : 轻量级文本编辑器, 通过插件支持 Python
Atom : GitHub 开发的开源文本编辑器
VS Code 配置 Python 开发环境 🔗🔗 Visual Studio Code 安装文档
安装 VS Code
在扩展市场搜索并安装 “Python” 扩展( 由 Microsoft 提供)
打开 Python 文件或文件夹
VS Code 会自动检测系统中安装的 Python 解释器
可以通过左下角选择 Python 解释器版本
编程基础 基本结构 Python 程序的基本结构 1 2 3 4 5 6 7 8 9 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 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
注意事项
Python 使用缩进 来表示代码块, 通常使用 4 个空格
注释使用 # 符号
字符串可以使用单引号 ' 或双引号 "
Python 严格区分大小写
语句末尾不需要分号( 但可以添加)
代码注释 单行注释
多行注释 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)) print (type (age)) print (type (height)) print (type (is_student)) age = "thirty" print (type (age))
常量 ⭐⭐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) : 每个单词首字母大写, 用于类名
全大写命名法 : 所有字母大写, 单词用下划线分隔, 用于常量
单下划线前缀 : 表示受保护的成员( 约定)
双下划线前缀 : 表示私有成员( 名称修饰)
数据类型 数值类型 💗💗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 price = 99.99 scientific = 1.23e-4 complex_num = 3 + 4j print (complex_num.real) print (complex_num.imag) int_num = int (3.14 ) float_num = float (5 ) complex_num = complex (2 , 3 )
布尔类型 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 result = True or False result = not True print (bool (0 )) print (bool (1 )) print (bool ("" )) print (bool ("hello" )) print (bool ([])) print (bool ([1 , 2 ])) print (bool (None ))
字符串类型 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" name = "Python" version = 3.11 message = f"Welcome to {name} {version} " text = " Hello, World! " print (text.strip()) print (text.lower()) print (text.upper()) print (text.replace("World" , "Python" )) print (text.split("," ))
None 类型 1 2 3 4 5 6 value = None if value is None : print ("值为空" )
类型转换 隐式转换 Python 在某些情况下会自动进行类型转换。
1 2 3 4 5 result = 5 + 3.14 result = True + 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" ) int_value = int (3.14 ) float_value = float ("3.14" ) float_value = float (5 ) str_value = str (123 ) str_value = str (3.14 ) list_value = list ("hello" ) tuple_value = tuple ([1 , 2 , 3 ]) set_value = set ([1 , 2 , 2 , 3 ]) try : num = int ("abc" ) except ValueError: print ("无法转换为整数" )
运算符 算术运算符
运算符 示例 结果 描述
+ x + y 两数之和 加法
- x - y 两数之差 减法
* x * y 两数之积 乘法
/ x / y 两数之商( 浮点数) 除法
// x // y 商的整数部分 整除
% x % y 余数 取模
** x ** y x 的 y 次幂 幂运算
1 2 3 4 5 6 7 8 9 10 11 a = 10 b = 3 print (a + b) print (a - b) print (a * b) print (a / b) print (a // b) print (a % b) print (a ** b)
比较运算符
运算符 示例 结果 描述
== x == y True/False 等于
!= x != y True/False 不等于
> x > y True/False 大于
< x < y True/False 小于
>= x >= y True/False 大于等于
<= x <= y True/False 小于等于
1 2 3 4 5 6 7 8 9 10 a = 10 b = 5 print (a == b) print (a != b) print (a > b) print (a < b) print (a >= b) print (a <= b)
逻辑运算符
运算符 示例 结果 描述
and x and y True/False 逻辑与
or x or y True/False 逻辑或
not not x True/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。
a b a and b a or b
True True True True
False True False True
True False False True
False False False False
1 2 3 4 5 6 7 a = True b = False print (a and b) print (a or b) print (not a)
赋值运算符
运算符 示例 等价于
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
//= x //= y x = x // y
%= x %= y x = x % y
**= x **= y x = x ** y
成员运算符
运算符 示例 结果 描述
in x in y True/False x 在 y 序列中
not in x not in y True/False x 不在 y 序列中
1 2 3 4 5 my_list = [1 , 2 , 3 , 4 , 5 ] print (3 in my_list) print (6 not in my_list)
身份运算符
运算符 示例 结果 描述
is x is y True/False x 和 y 是同一个对象
is not x is not y True/False x 和 y 不是同一个对象
1 2 3 4 5 6 7 8 a = [1 , 2 , 3 ] b = a c = [1 , 2 , 3 ] print (a is b) print (a is c) print (a == c)
运算符优先级 💗💗优先级由高到低
优先级 运算符 描述
1 () 括号
2 ** 幂运算
3 +x, -x, ~x 正号、 负号、 按位取反
4 *, /, //, % 乘、 除、 整除、 取模
5 +, - 加、 减
6 <<, >> 位移
7 & 按位与
8 ^ 按位异或
9 | 按位或
10 in, not in, is, is not, <, <=, >, >=, !=, == 比较、 成员、 身份运算符
11 not 逻辑非
12 and 逻辑与
13 or 逻辑或
输入输出 输出( 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" )
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 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 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 )) print (http_status(404 )) print (http_status(999 ))
💗💗模式匹配支持多种模式 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 ))) print (describe_point((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 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) for i in range (5 ): print (i) for i in range (0 , 10 , 2 ): print (i)
循环控制语句
break: 立即退出循环。
continue: 跳过当前迭代, 继续下一次迭代。
pass: 空语句, 用作占位符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 for i in range (10 ): if i == 5 : break print (i) for i in range (10 ): if i % 2 == 0 : continue print (i) for i in range (5 ): pass
enumerate 和 zip 1 2 3 4 5 6 7 8 9 10 fruits = ["apple" , "banana" , "cherry" ] for index, fruit in enumerate (fruits): print (f"{index} : {fruit} " ) 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 ]] my_list = list (range (5 ))
访问元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 fruits = ["apple" , "banana" , "cherry" , "date" ] print (fruits[0 ]) print (fruits[2 ]) print (fruits[-1 ]) print (fruits[-2 ]) print (fruits[1 :3 ]) print (fruits[:2 ]) print (fruits[2 :]) print (fruits[::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) 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) evens = [x for x in range (20 ) if x % 2 == 0 ] print (evens) matrix = [[i*j for j in range (3 )] for i in range (3 )] print (matrix)
元组( 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 ]) print (colors[-1 ]) print (colors[1 :])
元组解包 ⭐⭐ 解包( 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} " ) a = 10 b = 20 a, b = b, a print (a, b) numbers = (1 , 2 , 3 , 4 , 5 ) first, second, *rest = numbers print (first) print (second) print (rest) first, *middle, last = numbers print (first) print (middle) print (last) nested = (1 , (2 , 3 ), 4 ) a, (b, c), d = nested print (a, b, c, d) def get_user_info (): return "Bob" , 30 , "Shanghai" username, user_age, user_city = get_user_info() print (f"{username} 今年{user_age} 岁" )
元组方法 1 2 3 4 numbers = (1 , 2 , 3 , 2 , 4 , 2 ) print (numbers.count(2 )) print (numbers.index(3 ))
集合( 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 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) print (set1.union(set2))print (set1 & set2) print (set1.intersection(set2))print (set1 - set2) print (set1.difference(set2))print (set1 ^ set2) 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" } 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" ]) print (person.get("age" )) print (person.get("phone" , "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) even_squares = {x: x**2 for x in range (10 ) if x % 2 == 0 } print (even_squares)
函数 定义函数 1 2 3 4 5 6 7 8 def greet (name ): """问候函数""" return f"Hello, {name} !" message = greet("Alice" ) print (message)
参数类型 位置参数 1 2 3 4 def add (a, b ): return a + b result = add(5 , 3 )
默认参数 1 2 3 4 5 def greet (name, greeting="Hello" ): return f"{greeting} , {name} !" print (greet("Alice" )) print (greet("Bob" , "Hi" ))
可变参数 1 2 3 4 5 6 7 8 9 10 11 12 def sum_all (*args ): return sum (args) print (sum_all(1 , 2 , 3 , 4 , 5 )) 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() def say_hello (): print ("Hello!" ) result = say_hello() print (result)
作用域 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() def modify_global (): global global_var global_var = "Modified" modify_global() print (global_var)
回调函数 回调函数是作为参数传递给另一个函数的函数, 在特定事件发生或操作完成时被调用。
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)
应用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import timedef 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 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)) print (calculate(10 , 5 , multiply)) print (calculate(10 , 5 , subtract))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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) divide(10 , 0 , on_success, on_error)
匿名函数 1 2 3 4 5 6 7 8 9 10 11 square = lambda x: x ** 2 print (square(5 )) numbers = [1 , 2 , 3 , 4 , 5 ] squared = list (map (lambda x: x**2 , numbers)) print (squared) evens = list (filter (lambda x: x % 2 == 0 , numbers)) print (evens)
高阶函数
函数 功能 参数 返回值 示例
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 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 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' ]} 件" ) 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' ]:.2 f} " )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 from functools import reducenumbers = [1 , 2 , 3 , 4 , 5 ] product = reduce(lambda x, y: x * y, numbers) print (f"乘积: {product} " ) max_value = reduce(lambda x, y: x if x > y else y, numbers) print (f"最大值: {max_value} " ) words = ["Hello" , "World" , "Python" ] sentence = reduce(lambda x, y: f"{x} {y} " , words) print (f"拼接结果: {sentence} " ) result = sum (map (lambda x: x**2 , filter (lambda x: x % 2 == 0 , range (1 , 11 )))) print (f"1-10中偶数的平方和: {result} " )
模块与包 导入模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import mathprint (math.sqrt(16 )) import numpy as npfrom math import sqrt, piprint (sqrt(16 )) print (pi) from os import *
创建模块 1 2 3 4 5 6 7 8 def greet (name ): return f"Hello, {name} !" def add (a, b ): return a + b PI = 3.14159
1 2 3 4 5 6 import mymoduleprint (mymodule.greet("Alice" )) print (mymodule.add(5 , 3 )) print (mymodule.PI)
特殊变量 Python 中有一些特殊的变量( 也称为魔术变量或双下划线变量) , 它们在模块和包中具有特殊含义。
__name__1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 def main (): print ("这是主函数" ) if __name__ == "__main__" : main() print ("脚本作为主程序运行" ) else : print ("模块被导入" )
__file__1 2 3 4 5 6 import osprint (__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 """这是一个模块的文档字符串""" def greet (name ): """问候函数 参数: name (str): 要问候的人名 返回: str: 问候语 """ return f"Hello, {name} !" print (__doc__) print (greet.__doc__)
__package__1 2 3 4 5 6 7 print (__package__) print (__package__)
__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__ = ['public_func' , 'PublicClass' ] def public_func (): """公开函数""" pass def _private_func (): """私有函数( 不会被 import * 导入) """ pass class PublicClass : """公开类""" pass class _PrivateClass : """私有类( 不会被 import * 导入) """ pass from mymodule import *
其他特殊变量
变量 描述
__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 module1from 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) print (result)
常见类型注解
类型 示例 描述
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 def find_user (user_id: int ) -> Optional [str ]: """查找用户, 可能返回 None""" users = {1 : "Alice" , 2 : "Bob" } return users.get(user_id) result = find_user(1 ) result = find_user(99 ) 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 def process_value (value: Union [int , str ] ) -> str : """处理整数或字符串""" if isinstance (value, int ): return f"数字: {value} " else : return f"字符串: {value} " print (process_value(42 )) print (process_value("hello" )) 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 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) T = TypeVar('T' ) def first_element (items: List [T] ) -> T: """返回列表的第一个元素""" return items[0 ] first_int = first_element([1 , 2 , 3 ]) first_str = first_element(["a" , "b" , "c" ])
类的类型注解 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())
类型检查工具 ⭐⭐ Python 本身不强制类型检查, 但可以使用第三方工具进行静态类型检查。 常用工具:
mypy : 最流行的 Python 静态类型检查器
pyright : Microsoft 开发的类型检查器
pytype : Google 开发的类型检查器
1 2 3 4 5 pip install mypy mypy your_script.py
1 2 3 4 5 6 def add (a: int , b: int ) -> int : return a + b result = add("hello" , "world" )
面向对象 类和对象 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()) print (my_dog.get_info()) print (my_dog.species)
魔术方法 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) print (v3) print (v1 == v2) print (len (v1))
初始化相关
方法 调用时机 描述
__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 )
字符串相关
方法 调用时机 描述
__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)) print (repr (p))
比较运算符
方法 运算符 描述
__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) print (s1 == s2)
算术运算符
方法 运算符 描述
__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) print (n1 * n2)
容器类型相关
方法 调用时机 描述
__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)) print (my_list[2 ]) print (3 in my_list) my_list[0 ] = 10 print (my_list[0 ])
属性访问相关
方法 调用时机 描述
__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) print (obj.age)
可调用对象相关
方法 调用时机 描述
__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 )) print (triple(5 ))
上下文管理器相关
方法 调用时机 描述
__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)) print (int (num)) print (float (num))
继承 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()) print (dog.speak())
多态 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())
异常处理 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 jsondata = {"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 ) 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 randomprint (random.random())print (random.randint(1 , 10 )) print (random.randrange(0 , 10 , 2 )) 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 randomfruits = ['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 randomrandom.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 itertoolscounter = itertools.count(10 , 2 ) print (next (counter)) print (next (counter)) print (next (counter)) cycler = itertools.cycle(['A' , 'B' , 'C' ]) print (next (cycler)) print (next (cycler)) print (next (cycler)) print (next (cycler)) repeater = itertools.repeat('Hello' , 3 ) print (list (repeater))
组合迭代器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import itertoolsresult = itertools.product('AB' , '12' ) print (list (result)) result = itertools.permutations('ABC' , 2 ) print (list (result)) result = itertools.combinations('ABC' , 2 ) print (list (result)) result = itertools.combinations_with_replacement('ABC' , 2 ) print (list (result))
其他工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import itertoolsresult = itertools.chain([1 , 2 ], [3 , 4 ], [5 , 6 ]) print (list (result)) 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)} " ) result = itertools.accumulate([1 , 2 , 3 , 4 ]) print (list (result)) result = itertools.filterfalse(lambda x: x % 2 == 0 , range (10 )) print (list (result))
序列化 pickle 模块, 用于序列化对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import pickledata = { '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 Counterwords = ["apple" , "banana" , "apple" , "orange" , "banana" , "apple" ] counter = Counter(words) print (counter) text = "hello world" char_counter = Counter(text) print (char_counter)print (counter.most_common(2 )) print (counter['apple' ]) counter.update(["apple" , "grape" ]) print (counter)c1 = Counter(a=3 , b=1 ) c2 = Counter(a=1 , b=2 ) print (c1 + c2) print (c1 - c2)
defaultdict 💗💗 defaultdict 提供默认值的字典。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from collections import defaultdictdd_list = defaultdict(list ) dd_list['fruits' ].append('apple' ) dd_list['fruits' ].append('banana' ) print (dd_list) dd_int = defaultdict(int ) dd_int['count' ] += 1 dd_int['count' ] += 1 print (dd_int) 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 namedtuplePerson = namedtuple('Person' , ['name' , 'age' , 'city' ]) person = Person("Alice" , 25 , "Beijing" ) print (person.name) print (person.age) print (person.city) print (person._asdict()) 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 dequedq = deque([1 , 2 , 3 , 4 , 5 ]) dq.append(6 ) print (dq) dq.appendleft(0 ) print (dq) last = dq.pop() print (last) first = dq.popleft() print (first) dq = deque(maxlen=3 ) dq.extend([1 , 2 , 3 , 4 , 5 ]) print (dq)
文件目录 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 oscurrent_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 Pathpath = 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 ospath = os.path.join('folder' , 'subfolder' , 'file.txt' ) print (path) 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' )) print (os.path.basename('/path/file' )) print (os.path.splitext('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.pathos.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 Pathpath = Path('folder/subfolder/file.txt' ) print (path.name) print (path.stem) print (path.suffix) print (path.parent) print (path.parts) 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)
环境变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import oshome = os.environ.get('HOME' ) home = os.environ.get('USERPROFILE' ) path = os.environ.get('PATH' ) os.environ['MY_VAR' ] = 'my_value' del os.environ['MY_VAR' ]print (os.name) 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 zipfilewith zipfile.ZipFile('archive.zip' , 'w' ) as zipf: zipf.write('file1.txt' ) zipf.write('file2.txt' ) with zipfile.ZipFile('archive.zip' , 'r' ) as zipf: zipf.extractall('extracted/' ) 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 tarfilewith tarfile.open ('archive.tar.gz' , 'w:gz' ) as tar: tar.add('folder/' ) 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 tempfilewith 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('测试内容' )
配置文件 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 configparserconfig = 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' ]) print (config['server' ]['port' ])
系统相关 sys 模块提供了与 Python 解释器的交互功能, 如获取 Python 版本、 命令行参数、 环境变量等。
系统信息 1 2 3 4 5 6 7 8 9 10 11 import sysprint (sys.version) print (sys.version_info) print (sys.platform) print (sys.executable) print (sys.prefix) print (sys.byteorder)
命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 import sysprint (f"脚本名称: {sys.argv[0 ]} " )print (f"参数列表: {sys.argv} " )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 syssys.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 syssys.exit(0 ) sys.exit(1 ) sys.exit("程序出错" ) 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 sysprint (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, timedeltanow = 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 datetimenow = datetime.now() formatted = now.strftime("%Y-%m-%d %H:%M:%S" ) print (formatted) formatted = now.strftime("%Y年%m月%d日 %H时%M分" ) print (formatted) 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, timedeltanow = 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, timedeltautc_now = datetime.now(timezone.utc) print (f"UTC 时间: {utc_now} " )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 timetimestamp = 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:.4 f} 秒" )
数学运算 math 模块提供了基本的数学运算功能, 如求平方根、 对数、 幂等等。
数学常数 1 2 3 4 5 6 7 8 import mathprint (math.pi) print (math.e) print (math.tau) print (math.inf) print (math.nan)
基本函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import mathprint (math.ceil(3.14 )) print (math.floor(3.14 )) print (math.trunc(3.14 )) print (round (3.14 )) print (math.fabs(-5 )) print (math.fmax(3 , 5 )) print (math.fmin(3 , 5 ))
幂和对数 1 2 3 4 5 6 7 8 9 10 11 import mathprint (math.pow (2 , 10 )) print (math.sqrt(16 )) print (math.cbrt(27 )) print (math.log(math.e)) print (math.log10(100 )) print (math.log2(8 ))
三角函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import mathangle_deg = 180 angle_rad = math.radians(angle_deg) print (f"{angle_deg} 度 = {angle_rad} 弧度" )print (math.sin(math.pi / 2 )) print (math.cos(math.pi)) print (math.tan(math.pi / 4 )) print (math.asin(1 )) print (math.acos(0 )) print (math.atan(1 ))
其他函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import mathprint (math.factorial(5 )) print (math.gcd(12 , 8 )) print (math.lcm(4 , 6 )) print (math.isinf(float ('inf' ))) print (math.isnan(float ('nan' ))) print (math.isfinite(1.0 ))
哈希计算 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 hashlibtext = "Hello, World!" md5_hash = hashlib.md5(text.encode()).hexdigest() print (f"MD5: {md5_hash} " )sha256_hash = hashlib.sha256(text.encode()).hexdigest() print (f"SHA256: {sha256_hash} " )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()
日志处理 logging 模块提供了日志处理功能。
基本日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import logginglogging.basicConfig( level=logging.DEBUG, format ='%(asctime)s - %(name)s - %(levelname)s - %(message)s' , filename='app.log' , filemode='w' ) 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 logginglogger = 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 模块提供了高阶函数, 如 reduce、 partial 等。
reduce 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from functools import reducenumbers = [1 , 2 , 3 , 4 , 5 ] total = reduce(lambda x, y: x + y, numbers) print (total) product = reduce(lambda x, y: x * y, numbers) print (product) total = reduce(lambda x, y: x + y, numbers, 10 ) print (total)
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 )) print (fibonacci.cache_info())
partial 1 2 3 4 5 6 7 8 9 10 11 12 13 from functools import partialdef power (base, exponent ): return base ** exponent square = partial(power, exponent=2 ) print (square(5 )) cube = partial(power, exponent=3 ) print (cube(5 ))
正则表达式 re 模块提供了正则表达式功能, 如匹配、 替换、 查找等等。
基本匹配 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import retext = "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 retext = "我有3个苹果和5个橙子, 还有10个香蕉" numbers = re.findall(r'\d+' , text) print (numbers) 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 retext = "我有3个苹果和5个橙子" new_text = re.sub(r'\d+' , 'X' , text) print (new_text) new_text = re.sub(r'\d+' , 'X' , text, count=1 ) print (new_text) def double_num (match ): num = int (match .group()) return str (num * 2 ) new_text = re.sub(r'\d+' , double_num, text) print (new_text)
分割字符串 1 2 3 4 5 6 7 import retext = "apple,banana;cherry date" fruits = re.split(r'[,\s;]+' , text) print (fruits)
编译正则表达式 1 2 3 4 5 6 7 8 9 10 11 12 13 import repattern = re.compile (r'\d+' ) text = "我有3个苹果和5个橙子" matches = pattern.findall(text) print (matches) 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]匹配 a、 b 或 c [aeiou] 匹配元音
^匹配字符串开头 ^Hello
$匹配字符串结尾 World$
进程与线程 进程管理 subprocess 模块提供了子进程管理功能。
💗💗 基本用法 1 2 3 4 5 6 7 8 9 10 11 import subprocessresult = subprocess.run(['ls' , '-l' ], capture_output=True , text=True ) print (result.stdout)print (result.stderr)print (result.returncode)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 subprocessresult = 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 threadingimport timedef 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 threadingcounter = 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} " )
并发处理 concurrent.futures 模块提供了进程池和线程池, 用于并发处理任务。
💗💗 ProcessPoolExecutor 进程池 1 2 3 4 5 6 7 8 9 10 11 from concurrent.futures import ProcessPoolExecutorimport osdef 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))
💗💗 ThreadPoolExecutor 线程池 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from concurrent.futures import ThreadPoolExecutor, as_completedimport timedef 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 argparseparser = 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} " )
JSON 模块 json 模块提供了 JSON 数据处理功能。
序列化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import jsondata = { "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)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 jsonjson_string = '{"name": "Alice", "age": 25}' data = json.loads(json_string) print (data["name" ]) print (data["age" ]) 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 jsonfrom datetime import datetimeclass 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 模块用于生成和操作 UUID( Universally Unique Identifier) 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import uuiduuid1 = uuid.uuid1() print (f"UUID1: {uuid1} " )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 copyoriginal = [[1 , 2 ], [3 , 4 ]] shallow = copy.copy(original) shallow[0 ][0 ] = 99 print (original) original = [[1 , 2 ], [3 , 4 ]] deep = copy.deepcopy(original) deep[0 ][0 ] = 99 print (original)
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 csvdata = [ ['姓名' , '年龄' , '城市' ], ['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) with open ('data.csv' , 'r' , encoding='utf-8' ) as f: reader = csv.reader(f) for row in reader: print (row) 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 拥有丰富的第三方库生态系统, PyPI( Python Package Index) 上收录了数十万个高质量的第三方库, 涵盖了 Web 开发、 数据科学、 人工智能、 网络爬虫、 自动化测试等各个领域。
包管理工具 安装与配置 pip 是 Python 的包管理工具, 用于安装和管理第三方库。
1 2 3 4 5 6 pip --version pip3 --version 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
批量安装依赖 1 2 3 4 5 6 7 8 pip install -r 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 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 myenv\Scripts\activate source myenv/bin/activatedeactivate 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 myprojectpython 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 from django.http import HttpResponsedef hello (request ): return HttpResponse("Hello, Django!" ) from django.urls import pathfrom . import viewsurlpatterns = [ path('hello/' , views.hello, name='hello' ), ] from django.db import modelsclass 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 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, requestapp = 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 FastAPIfrom pydantic import BaseModelapp = 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 ()}
Streamlit Streamlit 是一个快速构建数据 Web 应用的框架, 无需前端开发经验。
💗💗 安装与使用
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 import streamlit as stimport pandas as pdimport numpy as npst.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 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 stst.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 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 nparr = 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) print (a * b) print (np.dot(a, b)) data = np.array([1 , 2 , 3 , 4 , 5 ]) print (np.mean(data)) print (np.std(data)) print (np.max (data)) print (np.min (data))
Pandas Pandas 是数据分析的核心库, 提供 DataFrame 数据结构。
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 pddata = { 'name' : ['Alice' , 'Bob' , 'Charlie' ], 'age' : [25 , 30 , 35 ], 'city' : ['Beijing' , 'Shanghai' , 'Guangzhou' ] } df = pd.DataFrame(data) df = pd.read_csv('data.csv' ) print (df.head()) 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 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 pltimport numpy as npx = 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import seaborn as snsimport matplotlib.pyplot as plttips = 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import tensorflow as tffrom tensorflow import kerasmodel = 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' ])
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 torchimport torch.nn as nnimport torch.optim as optimclass 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_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.metrics import accuracy_scoreiris = 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:.2 f} ' )
网络爬虫库 Requests Requests 是最流行的 HTTP 库, 简化 HTTP 请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import requestsresponse = requests.get('https://api.github.com' ) print (response.status_code)print (response.json())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 BeautifulSoupimport requestsresponse = 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' )) 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 2 3 4 scrapy startproject myproject cd myprojectscrapy 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 import scrapyclass 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from PIL import Imageimg = Image.open ('photo.jpg' ) img_resized = img.resize((800 , 600 )) img_rotated = img.rotate(45 ) img.save('photo.png' , 'PNG' ) from PIL import ImageFilterimg_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 cv2img = 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 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, Stringfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmakerengine = 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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
高级特性 装饰器 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) 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 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)
实用技巧 列表操作 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 Countertext = "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 pip install package --user python -m venv myenv source myenv/bin/activatepip 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 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 Course》 ( Python 编程: 从入门到实践)
《 Fluent Python》 ( 流畅的 Python)
《 Python Cookbook》 ( Python 食谱)
《 Automate the Boring Stuff with Python》 ( Python 自动化办公)
社区
Stack Overflow: https://stackoverflow.com/questions/tagged/python
Reddit r/Python: https://www.reddit.com/r/Python
GitHub: https://github.com/topics/python