Python Cheat Sheet
title: Python date: 2020-12-23 18:41:20 background: bg-[#436b97] tags: - script - interpret - 编程语言 - 后端开发 categories: - Programming intro: | Python 速查表 - 包含 Python 3 编程语言最重要的概念、语法和常用方法。适合初学者和开发者快速参考。 plugins: - copyCode - runCode
入门指南 {.cols-3}
简介
Python 是一种高级、解释型、动态类型的编程语言,以简洁易读著称。
- Python 官网 (python.org)
- Python 官方文档 (docs.python.org)
- Python 中文文档 (docs.python.org/zh-cn)
- Python 正则表达式 (cheatsheets)
Hello World
# 最简单的 Python 程序
print("Hello, World!")
# 使用 f-string 格式化输出
name = "Python"
print(f"Hello, {name}!")
Python 程序从上到下执行,不需要 main 函数
变量声明
# Python 是动态类型语言,无需声明类型
age = 18 # 整数 int
name = "张三" # 字符串 str
price = 99.99 # 浮点数 float
is_valid = True # 布尔值 bool
# 多变量赋值
x, y, z = 1, 2, 3
a = b = c = 0 # 相同值赋给多个变量
# 变量命名规则:字母/下划线开头,区分大小写
user_name = "admin" # 推荐使用下划线命名法
数据类型 {.row-span-2}
| 类型 | 说明 | 示例 |
|---|---|---|
str | 字符串 | "hello", 'world' |
int | 整数 | 42, -10, 0 |
float | 浮点数 | 3.14, -0.5 |
complex | 复数 | 3+4j |
bool | 布尔值 | True, False |
list | 列表 (可变) | [1, 2, 3] |
tuple | 元组 (不可变) | (1, 2, 3) |
dict | 字典 | {"a": 1} |
set | 集合 | {1, 2, 3} |
None | 空值 | None |
# 类型检查
type(42) # <class 'int'>
type("hello") # <class 'str'>
# 类型判断
isinstance(42, int) # True
isinstance("hi", str) # True
字符串切片
text = "Hello, World!"
# 基本切片 [start:end:step]
text[0:5] # 'Hello' (索引0-4)
text[7:] # 'World!' (从索引7到末尾)
text[:5] # 'Hello' (从开头到索引4)
text[-6:-1] # 'World' (负索引)
text[::2] # 'Hlo ol!' (每隔一个字符)
text[::-1] # '!dlroW ,olleH' (反转)
列表操作
# 创建列表
fruits = ["apple", "banana", "cherry"]
numbers = list(range(1, 6)) # [1, 2, 3, 4, 5]
# 常用操作
fruits.append("orange") # 末尾添加
fruits.insert(0, "grape") # 指定位置插入
fruits.remove("banana") # 删除元素
fruits.pop() # 删除并返回最后一个
len(fruits) # 获取长度
# 列表推导式 (List Comprehension)
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]
条件语句
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "D"
# 三元运算符
result = "及格" if score >= 60 else "不及格"
# match-case (Python 3.10+)
match score // 10:
case 10 | 9:
grade = "A"
case 8:
grade = "B"
case _:
grade = "C"
循环语句
# for 循环
for i in range(5):
print(i) # 0, 1, 2, 3, 4
# 遍历列表
fruits = ["apple", "banana"]
for fruit in fruits:
print(fruit)
# 带索引遍历
for i, fruit in enumerate(fruits):
print(f"{i}: {fruit}")
# while 循环
count = 0
while count < 5:
print(count)
count += 1
# break 和 continue
for i in range(10):
if i == 3:
continue # 跳过本次循环
if i == 7:
break # 退出循环
print(i)
函数定义
# 基本函数
def greet(name):
"""问候函数 - 这是文档字符串"""
return f"Hello, {name}!"
# 默认参数
def power(base, exp=2):
return base ** exp
# 可变参数
def sum_all(*args):
return sum(args)
# 关键字参数
def create_user(**kwargs):
return kwargs
# Lambda 表达式
square = lambda x: x ** 2
add = lambda x, y: x + y
文件操作 {.col-span-2}
# 读取文件
with open("file.txt", "r", encoding="utf-8") as f:
content = f.read() # 读取全部内容
# 或逐行读取
# for line in f:
# print(line.strip())
# 写入文件
with open("file.txt", "w", encoding="utf-8") as f:
f.write("Hello, World!")
# 追加内容
with open("file.txt", "a", encoding="utf-8") as f:
f.write("\n新的一行")
# JSON 文件操作
import json
with open("data.json", "w") as f:
json.dump({"name": "张三", "age": 25}, f, ensure_ascii=False)
with open("data.json", "r") as f:
data = json.load(f)
使用 with 语句可以自动关闭文件,避免资源泄露
算术运算符
a, b = 10, 3
a + b # 13 加法
a - b # 7 减法
a * b # 30 乘法
a / b # 3.333... 除法(返回浮点数)
a // b # 3 整除(向下取整)
a % b # 1 取余
a ** b # 1000 幂运算
# 复合赋值运算符
x = 10
x += 5 # x = x + 5 -> 15
x -= 3 # x = x - 3 -> 12
x *= 2 # x = x * 2 -> 24
字符串操作 {.cols-2}
字符串方法
s = " Hello, World! "
# 大小写转换
s.upper() # " HELLO, WORLD! "
s.lower() # " hello, world! "
s.title() # " Hello, World! "
s.capitalize() # " hello, world! "
# 去除空白
s.strip() # "Hello, World!"
s.lstrip() # "Hello, World! "
s.rstrip() # " Hello, World!"
# 查找替换
s.find("World") # 9 (返回索引,未找到返回-1)
s.index("World") # 9 (未找到抛出异常)
s.replace("World", "Python") # " Hello, Python! "
s.count("l") # 3
# 判断方法
s.startswith(" H") # True
s.endswith("! ") # True
"hello".isalpha() # True (全是字母)
"123".isdigit() # True (全是数字)
"hello123".isalnum() # True (字母或数字)
字符串格式化
name = "张三"
age = 25
price = 99.5
# f-string (推荐,Python 3.6+)
f"姓名: {name}, 年龄: {age}"
f"价格: {price:.2f}" # 保留2位小数
f"数字: {42:08d}" # 补零: 00000042
f"百分比: {0.85:.1%}" # 百分比: 85.0%
# format 方法
"姓名: {}, 年龄: {}".format(name, age)
"姓名: {n}, 年龄: {a}".format(n=name, a=age)
# % 格式化 (旧式)
"姓名: %s, 年龄: %d" % (name, age)
"价格: %.2f" % price
# 对齐
f"{'左对齐':<10}" # '左对齐 '
f"{'右对齐':>10}" # ' 右对齐'
f"{'居中':^10}" # ' 居中 '
字符串分割与连接
# 分割
text = "apple,banana,cherry"
fruits = text.split(",") # ['apple', 'banana', 'cherry']
text2 = "hello world python"
words = text2.split() # ['hello', 'world', 'python']
# 按行分割
lines = "line1\nline2\nline3".splitlines()
# 连接
"-".join(["a", "b", "c"]) # "a-b-c"
" ".join(words) # "hello world python"
"".join(["H", "i"]) # "Hi"
多行字符串
# 三引号字符串
text = """这是一个
多行字符串,
可以保留换行符。"""
# 原始字符串 (忽略转义)
path = r"C:\Users\name\Documents"
# 字节字符串
data = b"Hello"
type(data) # <class 'bytes'>
列表与元组 {.cols-2}
列表方法 {.row-span-2}
lst = [3, 1, 4, 1, 5, 9, 2, 6]
# 添加元素
lst.append(7) # 末尾添加 [3,1,4,1,5,9,2,6,7]
lst.insert(0, 0) # 指定位置插入
lst.extend([8, 9]) # 扩展列表
# 删除元素
lst.remove(1) # 删除第一个匹配的元素
lst.pop() # 删除并返回最后一个元素
lst.pop(0) # 删除并返回指定索引的元素
lst.clear() # 清空列表
# 查找
lst = [3, 1, 4, 1, 5]
lst.index(4) # 2 (返回第一个匹配的索引)
lst.count(1) # 2 (统计出现次数)
4 in lst # True (是否存在)
# 排序
lst.sort() # 原地排序 [1, 1, 3, 4, 5]
lst.sort(reverse=True) # 降序排序
sorted(lst) # 返回新列表,不修改原列表
lst.reverse() # 原地反转
# 复制
lst2 = lst.copy() # 浅拷贝
lst3 = lst[:] # 切片复制
import copy
lst4 = copy.deepcopy(lst) # 深拷贝
列表推导式
# 基本语法: [表达式 for 变量 in 可迭代对象 if 条件]
# 平方数列表
squares = [x**2 for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# 带条件过滤
evens = [x for x in range(20) if x % 2 == 0]
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 嵌套循环
matrix = [[i*j for j in range(1,4)] for i in range(1,4)]
# [[1,2,3], [2,4,6], [3,6,9]]
# 字典推导式
squares_dict = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
# 集合推导式
unique_lengths = {len(word) for word in ["hello", "world", "hi"]}
# {2, 5}
元组操作
# 创建元组 (不可变)
t1 = (1, 2, 3)
t2 = 1, 2, 3 # 括号可省略
t3 = tuple([1, 2, 3])
t4 = (1,) # 单元素元组需要逗号
# 元组解包
a, b, c = (1, 2, 3)
first, *rest = (1, 2, 3, 4) # first=1, rest=[2,3,4]
a, *middle, z = (1, 2, 3, 4, 5) # middle=[2,3,4]
# 元组方法
t = (1, 2, 3, 2, 1)
t.count(2) # 2
t.index(3) # 2
# 元组与列表转换
list((1, 2, 3)) # [1, 2, 3]
tuple([1, 2, 3]) # (1, 2, 3)
元组是不可变的,适合存储不应被修改的数据
切片操作
lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 基本切片 [start:end:step]
lst[2:5] # [2, 3, 4]
lst[:3] # [0, 1, 2]
lst[7:] # [7, 8, 9]
lst[::2] # [0, 2, 4, 6, 8] 每隔一个
lst[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 反转
# 负索引
lst[-3:] # [7, 8, 9]
lst[:-3] # [0, 1, 2, 3, 4, 5, 6]
lst[-5:-2] # [5, 6, 7]
# 切片赋值
lst[2:5] = [20, 30, 40]
lst[::2] = [0, 0, 0, 0, 0] # 必须长度匹配
字典与集合 {.cols-2}
字典操作 {.row-span-2}
# 创建字典
d1 = {"name": "张三", "age": 25}
d2 = dict(name="李四", age=30)
d3 = dict([("a", 1), ("b", 2)])
# 访问元素
d1["name"] # "张三"
d1.get("name") # "张三"
d1.get("city", "未知") # "未知" (默认值)
# 添加/修改
d1["city"] = "北京"
d1.update({"age": 26, "job": "工程师"})
# 删除
del d1["age"]
d1.pop("city") # 删除并返回
d1.popitem() # 删除并返回最后一项 (Python 3.7+)
d1.clear() # 清空
# 常用方法
d = {"a": 1, "b": 2, "c": 3}
d.keys() # dict_keys(['a', 'b', 'c'])
d.values() # dict_values([1, 2, 3])
d.items() # dict_items([('a',1), ('b',2), ('c',3)])
# 遍历字典
for key in d:
print(key, d[key])
for key, value in d.items():
print(f"{key}: {value}")
# 字典推导式
squares = {x: x**2 for x in range(5)}
集合操作
# 创建集合 (无序、不重复)
s1 = {1, 2, 3, 4, 5}
s2 = set([3, 4, 5, 6, 7])
s3 = set() # 空集合 (不能用 {})
# 添加/删除
s1.add(6)
s1.remove(1) # 不存在会报错
s1.discard(10) # 不存在不会报错
s1.pop() # 随机删除一个
s1.clear() # 清空
# 集合运算
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a | b # 并集 {1, 2, 3, 4, 5, 6}
a & b # 交集 {3, 4}
a - b # 差集 {1, 2}
a ^ b # 对称差集 {1, 2, 5, 6}
# 集合方法
a.union(b)
a.intersection(b)
a.difference(b)
a.symmetric_difference(b)
# 子集判断
{1, 2}.issubset({1, 2, 3}) # True
{1, 2, 3}.issuperset({1, 2}) # True
默认字典与计数器
from collections import defaultdict, Counter
# defaultdict - 带默认值的字典
dd = defaultdict(list)
dd["fruits"].append("apple")
dd["fruits"].append("banana")
# {'fruits': ['apple', 'banana']}
dd2 = defaultdict(int)
dd2["count"] += 1 # 自动初始化为 0
# Counter - 计数器
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
counter = Counter(words)
# Counter({'apple': 3, 'banana': 2, 'cherry': 1})
counter.most_common(2) # [('apple', 3), ('banana', 2)]
counter["apple"] # 3
嵌套数据结构
# 嵌套字典
users = {
"user1": {"name": "张三", "age": 25, "skills": ["Python", "Java"]},
"user2": {"name": "李四", "age": 30, "skills": ["JavaScript", "React"]}
}
# 访问嵌套数据
users["user1"]["name"] # "张三"
users["user1"]["skills"][0] # "Python"
# 嵌套列表 (矩阵)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix[1][2] # 6
# 安全访问嵌套数据
def safe_get(d, *keys, default=None):
for key in keys:
if isinstance(d, dict):
d = d.get(key, default)
else:
return default
return d
safe_get(users, "user1", "name") # "张三"
safe_get(users, "user3", "name", default="未知") # "未知"
函数进阶 {.cols-2}
参数类型 {.row-span-2}
# 位置参数
def greet(name, greeting):
return f"{greeting}, {name}!"
# 默认参数
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
# 关键字参数
greet(name="张三", greeting="你好")
# *args - 可变位置参数
def sum_all(*args):
return sum(args)
sum_all(1, 2, 3, 4) # 10
# **kwargs - 可变关键字参数
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="张三", age=25)
# 混合使用
def func(a, b, *args, c=10, **kwargs):
print(f"a={a}, b={b}, args={args}, c={c}, kwargs={kwargs}")
# 仅限关键字参数 (Python 3)
def func(a, b, *, c, d): # c 和 d 必须用关键字传递
pass
# 仅限位置参数 (Python 3.8+)
def func(a, b, /, c, d): # a 和 b 必须用位置传递
pass
Lambda 表达式
# 基本语法: lambda 参数: 表达式
square = lambda x: x ** 2
add = lambda x, y: x + y
# 常见用法
numbers = [1, 2, 3, 4, 5]
# 配合 map
list(map(lambda x: x**2, numbers)) # [1, 4, 9, 16, 25]
# 配合 filter
list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]
# 配合 sorted
students = [("Alice", 85), ("Bob", 90), ("Charlie", 78)]
sorted(students, key=lambda x: x[1], reverse=True)
# [('Bob', 90), ('Alice', 85), ('Charlie', 78)]
# 配合 reduce
from functools import reduce
reduce(lambda x, y: x + y, numbers) # 15
装饰器
# 基本装饰器
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}!")
# 带参数的装饰器
def repeat(times):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(times):
result = func(*args, **kwargs)
return result
return wrapper
return decorator
@repeat(3)
def greet(name):
print(f"Hello, {name}!")
# 保留函数元数据
from functools import wraps
def my_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
生成器
# 生成器函数 (使用 yield)
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 s in squares:
print(s)
# 无限生成器
def infinite_sequence():
num = 0
while True:
yield num
num += 1
# 生成器方法
gen = (x for x in range(3))
next(gen) # 0
next(gen) # 1
生成器是惰性求值的,适合处理大数据
类与面向对象 {.cols-2}
类定义 {.row-span-2}
class Person:
# 类变量
species = "Human"
# 构造方法
def __init__(self, name, age):
# 实例变量
self.name = name
self.age = age
# 实例方法
def greet(self):
return f"Hello, I'm {self.name}"
# 类方法
@classmethod
def create_anonymous(cls):
return cls("Anonymous", 0)
# 静态方法
@staticmethod
def is_adult(age):
return age >= 18
# 属性装饰器
@property
def info(self):
return f"{self.name}, {self.age}岁"
# 字符串表示
def __str__(self):
return f"Person({self.name}, {self.age})"
def __repr__(self):
return f"Person('{self.name}', {self.age})"
# 使用
p = Person("张三", 25)
p.greet() # "Hello, I'm 张三"
p.info # "张三, 25岁"
Person.is_adult(20) # True
Person.create_anonymous() # Person('Anonymous', 0)
继承
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("子类必须实现此方法")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
def fetch(self):
return f"{self.name} is fetching!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# 多重继承
class FlyingMixin:
def fly(self):
return f"{self.name} is flying!"
class Bird(Animal, FlyingMixin):
def speak(self):
return f"{self.name} says Tweet!"
# 使用
dog = Dog("Buddy")
dog.speak() # "Buddy says Woof!"
dog.fetch() # "Buddy is fetching!"
bird = Bird("Tweety")
bird.fly() # "Tweety is flying!"
# 调用父类方法
class Child(Parent):
def __init__(self, name, extra):
super().__init__(name)
self.extra = extra
魔术方法
class Vector:
def __init__(self, x, y):
self.x = x
self.y = 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
# 索引访问
def __getitem__(self, index):
return [self.x, self.y][index]
# 字符串表示
def __str__(self):
return f"Vector({self.x}, {self.y})"
# 可调用
def __call__(self):
return (self.x, self.y)
v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2 # Vector(4, 6)
v1 == v2 # False
len(v1) # 2
v1[0] # 1
v1() # (1, 2)
数据类 (Python 3.7+)
from dataclasses import dataclass, field
from typing import List
@dataclass
class Student:
name: str
age: int
grades: List[int] = field(default_factory=list)
# 自动生成 __init__, __repr__, __eq__
def average_grade(self):
return sum(self.grades) / len(self.grades) if self.grades else 0
# 使用
s1 = Student("张三", 20, [85, 90, 78])
s2 = Student("李四", 21)
print(s1) # Student(name='张三', age=20, grades=[85, 90, 78])
s1.average_grade() # 84.33...
# 不可变数据类
@dataclass(frozen=True)
class Point:
x: float
y: float
p = Point(1.0, 2.0)
# p.x = 3.0 # 会报错
异常处理 {.cols-2}
try-except 语句
try:
result = 10 / 0
except ZeroDivisionError:
print("不能除以零")
except (TypeError, ValueError) as e:
print(f"类型或值错误: {e}")
except Exception as e:
print(f"其他错误: {e}")
else:
print("没有异常时执行")
finally:
print("无论如何都会执行")
# 简洁写法
try:
risky_operation()
except SomeError:
pass # 忽略错误
自定义异常
# 定义自定义异常
class ValidationError(Exception):
"""验证错误"""
pass
class InsufficientFundsError(Exception):
"""余额不足错误"""
def __init__(self, balance, amount):
self.balance = balance
self.amount = amount
super().__init__(
f"余额不足: 当前余额 {balance}, 需要 {amount}"
)
# 使用
def withdraw(balance, amount):
if amount > balance:
raise InsufficientFundsError(balance, amount)
return balance - amount
try:
withdraw(100, 150)
except InsufficientFundsError as e:
print(e.balance, e.amount)
上下文管理器
# 使用 with 语句
with open("file.txt") as f:
content = f.read()
# 文件自动关闭
# 自定义上下文管理器 (类方式)
class Timer:
def __enter__(self):
import time
self.start = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
import time
self.end = time.time()
print(f"耗时: {self.end - self.start:.2f}秒")
return False # 不抑制异常
with Timer():
# 计时代码
sum(range(1000000))
# 使用 contextlib (函数方式)
from contextlib import contextmanager
@contextmanager
def timer():
import time
start = time.time()
yield
print(f"耗时: {time.time() - start:.2f}秒")
断言
# 断言 (调试用)
def divide(a, b):
assert b != 0, "除数不能为零"
return a / b
# 在生产环境可以用 python -O 禁用断言
# assert 只用于调试,不要用于输入验证
# 正确的输入验证
def divide_safe(a, b):
if b == 0:
raise ValueError("除数不能为零")
return a / b
常用模块 {.cols-2}
datetime 日期时间
from datetime import datetime, date, time, timedelta
# 当前时间
now = datetime.now()
today = date.today()
# 创建日期时间
dt = datetime(2024, 1, 15, 10, 30, 0)
d = date(2024, 1, 15)
t = time(10, 30, 0)
# 格式化
now.strftime("%Y-%m-%d %H:%M:%S") # "2024-01-15 10:30:00"
now.strftime("%Y年%m月%d日") # "2024年01月15日"
# 解析字符串
datetime.strptime("2024-01-15", "%Y-%m-%d")
# 时间计算
tomorrow = today + timedelta(days=1)
next_week = today + timedelta(weeks=1)
diff = datetime(2024, 12, 31) - datetime(2024, 1, 1)
diff.days # 365
# 时间戳
timestamp = datetime.now().timestamp()
datetime.fromtimestamp(timestamp)
os 和 pathlib
import os
from pathlib import Path
# os 模块
os.getcwd() # 当前目录
os.listdir(".") # 列出目录内容
os.path.exists("file.txt")
os.path.isfile("file.txt")
os.path.isdir("folder")
os.makedirs("a/b/c", exist_ok=True)
os.remove("file.txt")
os.rename("old.txt", "new.txt")
# pathlib (推荐,Python 3.4+)
p = Path(".")
p.exists()
p.is_file()
p.is_dir()
# 路径操作
path = Path("/home/user/documents")
path / "file.txt" # /home/user/documents/file.txt
path.parent # /home/user
path.name # documents
path.suffix # 扩展名
# 遍历文件
for f in Path(".").glob("*.py"):
print(f)
for f in Path(".").rglob("*.txt"): # 递归
print(f)
json 处理
import json
# Python -> JSON
data = {"name": "张三", "age": 25, "skills": ["Python", "Java"]}
json_str = json.dumps(data, ensure_ascii=False, indent=2)
# JSON -> Python
obj = json.loads(json_str)
# 文件操作
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
# 自定义序列化
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def person_encoder(obj):
if isinstance(obj, Person):
return {"name": obj.name, "age": obj.age}
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
json.dumps(Person("张三", 25), default=person_encoder)
re 正则表达式
import re
text = "我的邮箱是 test@example.com,电话是 13800138000"
# 匹配
pattern = r"\d+"
match = re.search(pattern, text)
if match:
print(match.group()) # 13800138000
# 查找所有
re.findall(r"\d+", text) # ['13800138000']
# 替换
re.sub(r"\d+", "***", text) # 数字替换为 ***
# 分割
re.split(r"[,,\s]+", "a, b,c d") # ['a', 'b', 'c', 'd']
# 编译正则 (提高效率)
email_pattern = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
emails = email_pattern.findall(text)
# 命名组
pattern = r"(?P<name>\w+)@(?P<domain>[\w.]+)"
match = re.search(pattern, "test@example.com")
match.group("name") # "test"
match.group("domain") # "example.com"
类型提示 (Type Hints) {.cols-2}
基本类型提示
# 变量类型提示
name: str = "张三"
age: int = 25
price: float = 99.99
is_valid: bool = True
# 函数类型提示
def greet(name: str) -> str:
return f"Hello, {name}!"
def add(a: int, b: int) -> int:
return a + b
# 可选类型
from typing import Optional
def get_user(user_id: int) -> Optional[str]:
# 可能返回 str 或 None
return None
# Union 类型
from typing import Union
def process(value: Union[int, str]) -> str:
return str(value)
# Python 3.10+ 可以用 | 代替 Union
def process(value: int | str) -> str:
return str(value)
复杂类型提示
from typing import List, Dict, Tuple, Set, Callable, Any
# 容器类型
names: List[str] = ["张三", "李四"]
scores: Dict[str, int] = {"math": 90, "english": 85}
point: Tuple[int, int] = (10, 20)
ids: Set[int] = {1, 2, 3}
# Python 3.9+ 可以直接使用内置类型
names: list[str] = ["张三", "李四"]
scores: dict[str, int] = {"math": 90}
# 可调用类型
def apply(func: Callable[[int, int], int], a: int, b: int) -> int:
return func(a, b)
# Any 类型 (任意类型)
def log(message: Any) -> None:
print(message)
# 泛型
from typing import TypeVar, Generic
T = TypeVar("T")
class Stack(Generic[T]):
def __init__(self) -> None:
self.items: List[T] = []
def push(self, item: T) -> None:
self.items.append(item)
def pop(self) -> T:
return self.items.pop()
类型别名
from typing import List, Dict, Tuple
# 类型别名
Vector = List[float]
Matrix = List[List[float]]
UserInfo = Dict[str, str | int]
def scale(vector: Vector, factor: float) -> Vector:
return [x * factor for x in vector]
# TypedDict (Python 3.8+)
from typing import TypedDict
class User(TypedDict):
name: str
age: int
email: str
def create_user(data: User) -> None:
print(f"Creating user: {data['name']}")
# Literal 类型
from typing import Literal
def set_mode(mode: Literal["read", "write", "append"]) -> None:
print(f"Mode: {mode}")
set_mode("read") # OK
# set_mode("edit") # 类型检查器会报错
实用技巧 {.cols-2}
解包技巧
# 列表解包
a, b, c = [1, 2, 3]
first, *rest = [1, 2, 3, 4, 5] # first=1, rest=[2,3,4,5]
*head, last = [1, 2, 3, 4, 5] # head=[1,2,3,4], last=5
a, *middle, z = [1, 2, 3, 4, 5] # middle=[2,3,4]
# 字典解包
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
merged = {**d1, **d2} # {"a": 1, "b": 2, "c": 3, "d": 4}
# 函数参数解包
def func(a, b, c):
return a + b + c
args = [1, 2, 3]
func(*args) # 6
kwargs = {"a": 1, "b": 2, "c": 3}
func(**kwargs) # 6
# 交换变量
x, y = y, x
常用内置函数
# 数学相关
abs(-5) # 5
max(1, 2, 3) # 3
min(1, 2, 3) # 1
sum([1, 2, 3]) # 6
pow(2, 3) # 8
round(3.14159, 2) # 3.14
# 序列相关
len([1, 2, 3]) # 3
sorted([3, 1, 2]) # [1, 2, 3]
reversed([1, 2, 3]) # 迭代器
enumerate(["a", "b"]) # [(0, "a"), (1, "b")]
zip([1, 2], ["a", "b"]) # [(1, "a"), (2, "b")]
# 函数式编程
list(map(str, [1, 2, 3])) # ["1", "2", "3"]
list(filter(lambda x: x > 0, [-1, 0, 1, 2])) # [1, 2]
all([True, True, False]) # False
any([True, False, False]) # True
# 类型转换
int("42") # 42
float("3.14") # 3.14
str(42) # "42"
list("abc") # ["a", "b", "c"]
tuple([1, 2]) # (1, 2)
set([1, 1, 2]) # {1, 2}
dict([("a", 1)]) # {"a": 1}
性能优化技巧
# 使用生成器代替列表 (节省内存)
# 不好
squares = [x**2 for x in range(1000000)]
# 好
squares = (x**2 for x in range(1000000))
# 字符串拼接
# 不好
s = ""
for item in items:
s += str(item)
# 好
s = "".join(str(item) for item in items)
# 成员检查用 set
# 不好
if item in large_list: # O(n)
pass
# 好
if item in large_set: # O(1)
pass
# 使用 collections.deque 做队列
from collections import deque
queue = deque()
queue.append(1) # O(1)
queue.appendleft(0) # O(1)
queue.pop() # O(1)
queue.popleft() # O(1)
# 使用 lru_cache 缓存
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 调试
print(f"DEBUG: {variable=}") # Python 3.8+
# 使用 pdb 调试器
import pdb
pdb.set_trace() # 设置断点
# breakpoint() (Python 3.7+)
breakpoint()
# 查看对象信息
dir(object) # 列出所有属性和方法
type(object) # 获取类型
id(object) # 获取对象 ID
vars(object) # 获取 __dict__
help(function) # 查看帮助
# 性能分析
import timeit
timeit.timeit('sum(range(100))', number=10000)
# 使用 logging 代替 print
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("调试信息")
logging.info("一般信息")
logging.warning("警告信息")
logging.error("错误信息")