Python 基础速览

本页面涵盖 Python 开发必备的核心知识,适合有编程基础的开发者快速上手。

🗺️ Python 知识体系

flowchart LR A[基础语法] --> B[数据结构] B --> C[函数&面向对象] C --> D[模块&包] D --> E[实战技能] E --> F[Web/数据/AI] style A fill:#11998e,color:#fff style B fill:#667eea,color:#fff style C fill:#764ba2,color:#fff style D fill:#f093fb,color:#fff style E fill:#4facfe,color:#fff style F fill:#00f2fe,color:#333
💡 建议:边学边练,在 Python REPL (python) 或 Jupyter Notebook 中尝试代码示例。

安装 Python

💡 2026 建议:使用 pyenv 管理多版本,避免系统 Python 冲突。

快速安装(推荐)

# macOS
brew install python@3.14

# Linux (Ubuntu/Debian)
sudo apt update && sudo apt install python3.14 python3.14-venv python3.14-dev

# Windows - 从 python.org 下载或使用 winget
winget install Python.Python.3.14

pyenv 多版本管理

# 安装 pyenv
curl https://pyenv.run | bash

# 安装多个版本
pyenv install 3.14.0
pyenv install 3.13.0

# 项目切换版本
cd myproject
pyenv local 3.13.0  # 自动创建 .python-version

# 验证
python --version

基础语法

Hello World 与注释

print("Hello, World!")  # 输出

# 多行注释用三引号
"""
这是文档字符串
也是多行注释
"""

缩进 - Python 的灵魂

if True:
    print("缩进表示代码块")
    if False:
        print("嵌套缩进")
print("这是外层")  # 缩进错误会导致 IndentationError

# PEP 8: 使用 4 空格,禁止 Tab 混用
# 用编辑器配置:tab-width = 4, insert spaces
⚠️ 注意:缩进是 Python 语法的一部分,缩进不一致会报错。推荐使用空格而非 Tab。

变量

Python 是动态类型语言,变量无需声明类型:

# 基础类型
name = "张三"      # str
age = 25           # int
height = 1.75      # float
is_active = True   # bool

# 变量命名规范
user_name = "valid"    # 小写+下划线
UserName = "Pascal"   # 类名用 PascalCase
CONSTANT = 3.14       # 常量用全大写

# 链式赋值
a = b = c = 0

# 多元解包
x, y, z = 1, 2, 3
first, *rest = [1, 2, 3, 4]  # first=1, rest=[2,3,4]

数据类型

核心数据类型一览

# 数值
x = 10        # int
y = 3.14      # float
z = 1+2j      # complex 复数

# 字符串 - 不可变
s = "hello"
s = 'world'
s = """多行
字符串"""

# 列表 - 可变,有序
nums = [1, 2, 3]
nums.append(4)
nums[0] = 0

# 元组 - 不可变,有序
point = (10, 20)
x, y = point  # 解包

# 字典 - 可变,键值对
user = {"name": "张三", "age": 25}
user["city"] = "北京"

# 集合 - 无序,不重复
unique = {1, 2, 3}
unique.add(4)
unique.add(1)  # 重复无效

# 布尔值
is_valid = True
is_empty = False
bool(0)  # False
bool([])  # False
bool("")  # False

类型转换

int("42")     # "42" -> 42
float("3.14") # "3.14" -> 3.14
str(42)       # 42 -> "42"
bool(1)       # True
list("abc")   # ['a', 'b', 'c']
set([1,2,2])  # {1, 2}

可变 vs 不可变:

  • 不可变:int, float, str, tuple, frozenset
  • 可变:list, dict, set

⚠️ 函数参数传递时注意:可变对象在函数内修改会影响外部

控制流

🔀 条件语句执行流程

flowchart TD A[开始] --> B{age >= 18?} B -->|Yes| C[输出: 成年人] B -->|No| D{age >= 13?} D -->|Yes| E[输出: 青少年] D -->|No| F[输出: 儿童] C --> G[结束] E --> G F --> G style B fill:#f093fb,color:#fff style D fill:#f093fb,color:#fff style A fill:#11998e,color:#fff style G fill:#eb3349,color:#fff

🔄 循环执行流程

flowchart TD A[开始] --> B[初始化计数器] B --> C{条件满足?} C -->|Yes| D[执行循环体] D --> E[更新计数器] E --> C C -->|No| F[结束循环] style C fill:#f093fb,color:#fff style A fill:#11998e,color:#fff style F fill:#eb3349,color:#fff

条件语句

age = 18

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

循环

for 循环

# 遍历列表
fruits = ["苹果", "香蕉", "橙子"]
for fruit in fruits:
    print(fruit)

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

while 循环

count = 0
while count < 5:
    print(count)
    count += 1

break 和 continue

# break - 跳出循环
for i in range(10):
    if i == 5:
        break
    print(i)  # 输出: 0, 1, 2, 3, 4

# continue - 跳过当前迭代
for i in range(5):
    if i == 2:
        continue
    print(i)  # 输出: 0, 1, 3, 4

函数

函数是可重复使用的代码块,使用 def 关键字定义:

📞 函数调用时序图

sequenceDiagram participant 主程序 participant greet函数 participant 返回值 主程序->>greet函数: 调用 greet("Python") greet函数->>greet函数: 接收参数 name="Python" greet函数->>greet函数: 执行函数体 greet函数->>返回值: return f"你好, {name}!" 返回值-->>主程序: 返回 "你好, Python!" 主程序->>主程序: print(message) Note over 主程序: 输出: 你好, Python!

🔧 函数类型分类

flowchart TB A[函数类型] --> B[内置函数] A --> C[用户自定义函数] A --> D[匿名函数] B --> B1[print, len, range...] C --> C1[普通函数] C --> C2[装饰器函数] C --> C3[生成器函数] C --> C4[递归函数] D --> D1[lambda x: x*2] style A fill:#667eea,color:#fff
# 定义函数
def greet(name):
    """向用户打招呼"""
    return f"你好, {name}!"

# 调用函数
message = greet("Python")
print(message)  # 输出: 你好, Python!

# 带默认参数的函数
def introduce(name, age=18):
    print(f"我是 {name},今年 {age} 岁")

introduce("小明")           # 我是小明,今年 18 岁
introduce("小红", 20)      # 我是小红,今年 20 岁

# 返回多个值
def calculate(a, b):
    return a + b, a - b

sum_result, diff_result = calculate(10, 5)
print(sum_result)   # 15
print(diff_result)  # 5

内置函数

# 常用内置函数
print(len("Hello"))      # 5 - 字符串长度
print(max([1, 5, 3]))    # 5 - 最大值
print(min([1, 5, 3]))    # 1 - 最小值
print(sum([1, 2, 3]))    # 6 - 求和
print(abs(-5))           # 5 - 绝对值
print(round(3.14159, 2)) # 3.14 - 四舍五入

面向对象编程

Python 是一门面向对象的编程语言。类(Class)是对象的蓝图,对象(Object)是类的实例。

🏗️ 类与对象关系图

classDiagram class Person { +str name +int age +__init__(name, age) +say_hello() str +introduce() str } class Student { +str school +__init__(name, age, school) +study() str +introduce() str } class Teacher { +str subject +__init__(name, age, subject) +teach() str } Person <|-- Student : 继承 Person <|-- Teacher : 继承 Person o-- Person1 : 创建 Student o-- Student1 : 创建

🔄 面向对象核心概念

flowchart LR A[封装] --> A1[把数据和方法绑在一起] A --> A2[保护内部数据] B[继承] --> B1[子类继承父类] B --> B2[代码复用] C[多态] --> C1[不同对象同一接口] C --> C2[不同实现] D[抽象] --> D1[隐藏复杂性] D --> D2[只暴露接口] style A fill:#11998e,color:#fff style B fill:#667eea,color:#fff style C fill:#764ba2,color:#fff style D fill:#f093fb,color:#fff

定义类和创建对象

class Person:
    """人类"""
    
    def __init__(self, name, age):
        """构造函数"""
        self.name = name
        self.age = age
    
    def say_hello(self):
        """实例方法"""
        return f"你好,我是 {self.name}"
    
    def introduce(self):
        return f"我叫 {self.name},今年 {self.age} 岁"

# 创建对象
person1 = Person("张三", 25)
person2 = Person("李四", 30)

print(person1.say_hello())  # 输出: 你好,我是 张三
print(person2.introduce())  # 输出: 我叫 李四,今年 30 岁

继承

class Student(Person):
    """学生类,继承自 Person"""
    
    def __init__(self, name, age, school):
        super().__init__(name, age)  # 调用父类构造函数
        self.school = school
    
    def study(self):
        return f"{self.name} 正在 {self.school} 学习"
    
    def introduce(self):
        """重写父类方法"""
        return f"我是学生 {self.name},在 {self.school} 上学"

student = Student("王五", 20, "清华大学")
print(student.say_hello())  # 继承的方法
print(student.study())      # 自己的方法
print(student.introduce())  # 重写的方法

类方法和静态方法

class Math:
    pi = 3.14159
    
    @classmethod
    def circle_area(cls, radius):
        """类方法"""
        return cls.pi * radius ** 2
    
    @staticmethod
    def add(a, b):
        """静态方法"""
        return a + b

print(Math.circle_area(5))  # 使用类方法
print(Math.add(3, 4))       # 使用静态方法

字符串格式化

Python 提供了多种字符串格式化方法,让字符串拼接更加灵活和易读。

f-string(推荐,Python 3.6+,3.12+ 增强)

# 基本使用
name = "张三"
age = 25
print(f"姓名: {name}, 年龄: {age}")

# 表达式
print(f"明年年龄: {age + 1}")

# 格式化数字
price = 1234.5678
print(f"价格: {price:.2f}")  # 保留两位小数
print(f"百分比: {0.1234:.2%}")  # 百分比

# 对齐和填充
print(f"{'左对齐':<20}")  # 左对齐,宽度20
print(f"{'右对齐':>20}")  # 右对齐,宽度20
print(f"{'居中':^20}")    # 居中,宽度20

# 数字格式化
number = 1234567
print(f"千分位: {number:,}")  # 1,234,567
print(f"科学计数: {number:e}")  # 1.234567e+06

# 填充0
print(f"编号: {5:03d}")  # 005

# 调用方法
text = "hello world"
print(f"大写: {text.upper()}")

# 字典访问
person = {"name": "李四", "age": 30}
print(f"姓名: {person['name']}, 年龄: {person['age']}")

# 列表访问
items = ["apple", "banana", "orange"]
print(f"第一个: {items[0]}")

# 格式化日期
from datetime import datetime
now = datetime.now()
print(f"当前时间: {now:%Y-%m-%d %H:%M:%S}")

format() 方法

# 基本使用
print("姓名: {}, 年龄: {}".format("张三", 25))

# 位置参数
print("{0} 和 {1}".format("苹果", "香蕉"))
print("{1} 和 {0}".format("苹果", "香蕉"))

# 关键字参数
print("姓名: {name}, 年龄: {age}".format(name="李四", age=30))

# 混合使用
print("{0} 和 {name}".format("王五", name="赵六"))

# 格式化数字
print("价格: {:.2f}".format(1234.5678))
print("百分比: {:.2%}".format(0.1234))

# 对齐
print("{:<20}".format("左对齐"))
print("{:>20}".format("右对齐"))
print("{:^20}".format("居中"))

# 填充
print("{:*>20}".format("填充*"))
print("{:0>5}".format(42))  # 00042

# 千分位
print("{:,}".format(1234567))

% 格式化(旧式)

# 基本使用
name = "张三"
age = 25
print("姓名: %s, 年龄: %d" % (name, age))

# 格式化数字
print("价格: %.2f" % 1234.5678)
print("百分比: %.2f%%" % 0.1234)

# 字典
person = {"name": "李四", "age": 30}
print("姓名: %(name)s, 年龄: %(age)d" % person)

字符串模板

from string import Template

# 创建模板
template = Template("姓名: $name, 年龄: $age")

# 替换
result = template.substitute(name="张三", age=25)
print(result)

# 使用字典
person = {"name": "李四", "age": 30}
result = template.substitute(person)
print(result)

常用格式化示例

# 进制转换
num = 255
print(f"二进制: {num:b}")   # 11111111
print(f"八进制: {num:o}")   # 377
print(f"十六进制: {num:x}")  # ff
print(f"十六进制(大写): {num:X}")  # FF

# 科学计数
print(f"科学计数: {123456789:.2e}")  # 1.23e+08

# 货币格式
amount = 1234.567
print(f"金额: ¥{amount:,.2f}")  # ¥1,234.57

# 时间格式化
seconds = 3661
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
print(f"时间: {hours:02d}:{minutes:02d}:{secs:02d}")  # 01:01:01

# 表格格式化
data = [
    ("张三", 25, "北京"),
    ("李四", 30, "上海"),
    ("王五", 28, "广州")
]
print(f"{'姓名':<10}{'年龄':<10}{'城市':<10}")
print("-" * 30)
for name, age, city in data:
    print(f"{name:<10}{age:<10}{city:<10}")
💡 提示:f-string 是 Python 3.6+ 引入的,3.12+ 版本性能进一步优化,代码最简洁,推荐优先使用。

Python 常用技巧

掌握这些 Python 技巧可以让你的代码更简洁、更高效、更 Pythonic。

列表技巧

# 列表解包
a, b, c = [1, 2, 3]
print(a, b, c)  # 1 2 3

# 交换变量
a, b = b, a

# 使用 * 解包
first, *rest = [1, 2, 3, 4, 5]
print(first, rest)  # 1 [2, 3, 4, 5]

# 列表扁平化
nested = [[1, 2], [3, 4], [5, 6]]
flattened = [item for sublist in nested for item in sublist]
print(flattened)  # [1, 2, 3, 4, 5, 6]

# 列表去重(保持顺序)
items = [1, 2, 2, 3, 4, 4, 5]
unique = list(dict.fromkeys(items))
print(unique)  # [1, 2, 3, 4, 5]

# 列表分块
def chunk(lst, size):
    return [lst[i:i + size] for i in range(0, len(lst), size)]

print(chunk([1, 2, 3, 4, 5, 6, 7], 3))  # [[1, 2, 3], [4, 5, 6], [7]]

字典技巧

# 字典合并
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged = {**dict1, **dict2}
print(merged)  # {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# 字典排序
d = {'a': 3, 'b': 1, 'c': 2}
sorted_by_key = dict(sorted(d.items()))
sorted_by_value = dict(sorted(d.items(), key=lambda x: x[1]))
print(sorted_by_key)   # {'a': 3, 'b': 1, 'c': 2}
print(sorted_by_value) # {'b': 1, 'c': 2, 'a': 3}

# 字典默认值
d = {'a': 1, 'b': 2}
print(d.get('c', 0))  # 0

# 字典计数
from collections import Counter
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
count = Counter(words)
print(count)  # Counter({'apple': 3, 'banana': 2, 'orange': 1})

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

字符串技巧

# 字符串反转
text = "Hello"
reversed_text = text[::-1]
print(reversed_text)  # olleH

# 检查回文
def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("racecar"))  # True

# 字符串连接
words = ['Hello', 'World', 'Python']
sentence = ' '.join(words)
print(sentence)  # Hello World Python

# 去除字符串首尾字符
text = "  Hello World  "
print(text.strip())  # "Hello World"

# 统计字符频率
from collections import Counter
text = "hello world"
count = Counter(text)
print(count)  # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

# 首字母大写
text = "hello world"
print(text.title())  # Hello World

# 检查字符串类型
text = "12345"
print(text.isdigit())  # True
print(text.isalpha())  # False
print(text.isalnum())  # True

数值技巧

# 判断奇偶
num = 5
is_even = num % 2 == 0
is_odd = num % 2 != 0

# 绝对值
abs_num = abs(-5)  # 5

# 四舍五入
rounded = round(3.14159, 2)  # 3.14

# 幂运算
power = 2 ** 10  # 1024

# 整除
quotient = 10 // 3  # 3

# 最大最小值
numbers = [1, 5, 3, 9, 2]
print(max(numbers))  # 9
print(min(numbers))  # 1

# 求和
total = sum(numbers)  # 20

# 平均值
average = sum(numbers) / len(numbers)  # 4.0

# 生成随机数
import random
print(random.randint(1, 10))  # 1-10 之间的随机整数
print(random.choice(['a', 'b', 'c']))  # 随机选择一个

集合技巧

# 集合去重
items = [1, 2, 2, 3, 4, 4, 5]
unique = set(items)
print(unique)  # {1, 2, 3, 4, 5}

# 集合运算
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(set1 & set2)  # 交集 {3, 4}
print(set1 | set2)  # 并集 {1, 2, 3, 4, 5, 6}
print(set1 - set2)  # 差集 {1, 2}
print(set1 ^ set2)  # 对称差集 {1, 2, 5, 6}

# 检查子集
set1 = {1, 2}
set2 = {1, 2, 3, 4}
print(set1.issubset(set2))  # True

函数技巧

# 函数默认参数陷阱
def append_to_list(item, lst=None):
    if lst is None:
        lst = []
    lst.append(item)
    return lst

# 可变参数
def sum_all(*args):
    return sum(args)

print(sum_all(1, 2, 3, 4))  # 10

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

print_info(name="张三", age=25)

# 函数作为参数
def apply_operation(x, operation):
    return operation(x)

print(apply_operation(5, lambda x: x ** 2))  # 25

# 装饰器
def timer(func):
    import time
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} 执行时间: {end - start:.4f} 秒")
        return result
    return wrapper

@timer
def slow_function():
    import time
    time.sleep(1)
    return "完成"

类技巧

# __slots__ 节省内存
class Person:
    __slots__ = ['name', 'age']

    def __init__(self, name, age):
        self.name = name
        self.age = age

# @property 装饰器
class Circle:
    def __init__(self, radius):
        self._radius = radius

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, value):
        if value < 0:
            raise ValueError("半径不能为负数")
        self._radius = value

    @property
    def area(self):
        return 3.14159 * self._radius ** 2

# __str__ 和 __repr__
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

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

    def __repr__(self):
        return f"Point(x={self.x}, y={self.y})"

# 上下文管理器
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()

with FileManager('example.txt', 'w') as f:
    f.write('Hello')

文件技巧

# 读取文件所有行
with open('file.txt', 'r') as f:
    lines = f.readlines()

# 读取文件到字符串
with open('file.txt', 'r') as f:
    content = f.read()

# 逐行读取(内存高效)
with open('file.txt', 'r') as f:
    for line in f:
        print(line.strip())

# 写入文件
with open('file.txt', 'w') as f:
    f.write('Hello, World!')

# 追加到文件
with open('file.txt', 'a') as f:
    f.write('\nNew line')

# 检查文件是否存在
import os
if os.path.exists('file.txt'):
    print("文件存在")

# 获取文件大小
size = os.path.getsize('file.txt')
print(f"文件大小: {size} 字节")

日期时间技巧

from datetime import datetime, timedelta

# 获取当前时间
now = datetime.now()
print(now)

# 格式化时间
formatted = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted)

# 解析时间字符串
parsed = datetime.strptime('2026-01-25', '%Y-%m-%d')
print(parsed)

# 时间计算
tomorrow = now + timedelta(days=1)
yesterday = now - timedelta(days=1)
print(f"明天: {tomorrow}")
print(f"昨天: {yesterday}")

# 时间差
date1 = datetime(2026, 1, 1)
date2 = datetime(2026, 1, 25)
diff = date2 - date1
print(f"相差天数: {diff.days}")

枚举技巧

from enum import Enum, auto

class Color(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

print(Color.RED)        # Color.RED
print(Color.RED.value)  # 1
print(Color.RED.name)   # 'RED'

# 字符串枚举
class Status(str, Enum):
    PENDING = "pending"
    APPROVED = "approved"
    REJECTED = "rejected"

print(Status.PENDING)  # Status.PENDING
print(Status.PENDING.value)  # 'pending'

数据类

from dataclasses import dataclass
from typing import List

@dataclass
class Person:
    name: str
    age: int
    hobbies: List[str] = None

    def __post_init__(self):
        if self.hobbies is None:
            self.hobbies = []

# 创建实例
person = Person("张三", 25, ["阅读", "编程"])
print(person)  # Person(name='张三', age=25, hobbies=['阅读', '编程'])

# 访问属性
print(person.name)  # 张三
print(person.age)   # 25

# 转换为字典
print(person.__dict__)  # {'name': '张三', 'age': 25, 'hobbies': ['阅读', '编程']}

类型提示

from typing import List, Dict, Optional, Union

# 基本类型提示
def greet(name: str) -> str:
    return f"Hello, {name}!"

# 列表类型提示
def process_numbers(numbers: List[int]) -> List[int]:
    return [x * 2 for x in numbers]

# 字典类型提示
def get_user_info(user_id: int) -> Dict[str, Union[str, int]]:
    return {"name": "张三", "age": 25}

# 可选类型提示
def find_user(user_id: int) -> Optional[str]:
    if user_id == 1:
        return "张三"
    return None

# 联合类型提示
def process_value(value: Union[int, str]) -> str:
    return str(value)

性能优化技巧

# 使用生成器节省内存
def generate_numbers(n):
    for i in range(n):
        yield i

# 使用集合进行快速查找
items = [1, 2, 3, 4, 5]
item_set = set(items)
print(3 in item_set)  # O(1) 时间复杂度

# 使用 map 代替循环
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))

# 使用 filter 过滤
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

# 使用 any/all
numbers = [1, 3, 5, 7, 9]
has_even = any(x % 2 == 0 for x in numbers)  # False
all_odd = all(x % 2 != 0 for x in numbers)  # True

# 使用 zip 组合列表
names = ['张三', '李四', '王五']
ages = [25, 30, 28]
for name, age in zip(names, ages):
    print(f"{name}: {age}")

调试技巧

# 使用 print 调试
x = 10
print(f"x = {x}")  # x = 10

# 使用 assert
assert x > 0, "x 必须大于 0"

# 使用 pdb 调试
import pdb; pdb.set_trace()

# 使用 logging
import logging
logging.basicConfig(level=logging.INFO)
logging.info("这是一条信息")
logging.warning("这是一条警告")
logging.error("这是一条错误")

# 使用 type 检查类型
print(type(x))  # 
print(isinstance(x, int))  # True

# 使用 dir 查看对象属性
print(dir(x))

代码简洁技巧

# 三元运算符
age = 25
status = "成年" if age >= 18 else "未成年"

# 多条件判断
if x > 0 and y > 0 and z > 0:
    print("都大于0")

# 简化为
if all([x > 0, y > 0, z > 0]):
    print("都大于0")

# 使用 enumerate 获取索引
for i, item in enumerate(['a', 'b', 'c']):
    print(f"{i}: {item}")

# 使用 zip 同时遍历多个列表
names = ['张三', '李四', '王五']
ages = [25, 30, 28]
for name, age in zip(names, ages):
    print(f"{name}: {age}")

Python 编码最佳实践:

  • ✅ 使用有意义的变量名
  • ✅ 遵循 PEP 8 编码规范
  • ✅ 添加适当的注释和文档字符串
  • ✅ 使用类型提示提高代码可读性
  • ✅ 优先使用列表推导式而非循环
  • ✅ 使用上下文管理器管理资源
  • ✅ 异常处理要具体
  • ✅ 编写单元测试
💡 提示:Python 的哲学是"优雅胜于丑陋,明了胜于晦涩",编写 Pythonic 的代码会让代码更易读、更易维护。

Python 3.14 新特性:

  • 🚀 性能优化:CPython 解释器进一步优化,启动速度提升 10-15%
  • 📝 错误消息改进:更详细的错误提示,帮助快速定位问题
  • 🔧 标准库增强:pathlib、asyncio、typing 等模块功能增强
  • 🎯 类型提示改进:支持更复杂的类型注解和泛型
  • f-string 优化:更高效的字符串格式化
  • 🛠️ 调试支持:改进的调试器和 REPL 体验