关于 Python Fundamentals
面向 Python 初学者的基础编程学习技能,涵盖语法、数据结构、面向对象编程和标准库,并通过实践项目巩固知识。
主要功能
- 01Python 语法与数据类型
- 02条件、循环与函数
- 03面向对象编程基础
- 04标准库与文件操作
- 05虚拟环境和 pip 管理
适用场景
- 01练习编写符合 PEP 8 的代码
- 02用 pathlib 构建文件整理工具
- 03通过 JSON 实现联系人管理
- 04分析文本并导出 CSV 统计
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-python --skill python-fundamentals安装后在 Claude Code 中输入/python-fundamentals确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/config.yamlreferences/GUIDE.mdscripts/helper.py详细介绍
Python 基础
概述
本技能涵盖 Python 编程的基础要素,包括语法、数据类型、控制结构、函数、面向对象编程和标准库。
学习目标
- 遵循 PEP 8 指南,编写整洁且符合 Python 风格的代码
- 掌握所有 Python 数据结构(列表、元组、字典、集合)
- 理解并实现面向对象编程概念
- 有效地使用 Python 标准库
- 使用 pip/venv 管理虚拟环境和包
核心主题
1. Python 语法与数据类型
- 变量和命名约定
- 数值类型(int、float、complex)
- 字符串和字符串方法
- 布尔逻辑和 None
- 类型提示(PEP 484)
- F 字符串和格式化
代码示例:
# Type hints and f-strings
def greet_user(name: str, age: int) -> str:
return f"Hello {name}, you are {age} years old!"
# Using the function
message = greet_user("Alice", 30)
print(message) # Hello Alice, you are 30 years old!
2. 控制流与函数
- 条件语句(if/elif/else)
- 循环(for、while)
- 列表/字典/集合推导式
- 函数参数和实参
- Lambda 函数
- 装饰器基础
代码示例:
# List comprehension with conditional
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_squares = [x**2 for x in numbers if x % 2 == 0]
print(even_squares) # [4, 16, 36, 64, 100]
# Decorator example
def timing_decorator(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} took {end - start:.4f} seconds")
return result
return wrapper
@timing_decorator
def slow_function():
import time
time.sleep(1)
return "Done!"
3. 面向对象编程
- 类和实例
- 继承和组合
- 多态和鸭子类型
- 封装(公有/私有)
- 魔术方法(init、str、repr 等)
- 抽象基类
代码示例:
from abc import ABC, abstractmethod
class Vehicle(ABC):
def __init__(self, brand: str, model: str):
self.brand = brand
self.model = model
@abstractmethod
def start_engine(self) -> str:
pass
def __str__(self) -> str:
return f"{self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand: str, model: str, doors: int):
super().__init__(brand, model)
self.doors = doors
def start_engine(self) -> str:
return f"{self} engine started with {self.doors} doors"
# Usage
car = Car("Toyota", "Camry", 4)
print(car.start_engine()) # Toyota Camry engine started with 4 doors
4. 标准库
- 文件操作(open、read、write)
- 使用 pathlib 处理路径
- 使用 datetime 进行日期/时间操作
- collections(Counter、defaultdict、namedtuple)
- itertools 和 functools
- json 和 csv 模块
代码示例:
from pathlib import Path
from datetime import datetime, timedelta
from collections import Counter
import json
# Path operations
data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
config_file = data_dir / "config.json"
config = {"app_name": "MyApp", "version": "1.0.0"}
# Write JSON
with open(config_file, "w") as f:
json.dump(config, f, indent=2)
# Read JSON
with open(config_file, "r") as f:
loaded_config = json.load(f)
# Date operations
today = datetime.now()
next_week = today + timedelta(days=7)
formatted = today.strftime("%Y-%m-%d %H:%M:%S")
# Counter for frequency analysis
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
word_count = Counter(words)
print(word_count.most_common(2)) # [('apple', 3), ('banana', 2)]
动手实践
项目 1:文件管理工具
构建一个按扩展名整理文件的命令行工具。
要求:
- 接受目录路径作为输入
- 递归扫描文件
- 按扩展名对文件分组
- 将文件移至整理后的文件夹
- 生成汇总报告
关键技能: pathlib、文件 I/O、os 模块
项目 2:联系人管理器(面向对象编程)
使用面向对象原则创建联系人管理系统。
要求:
- 包含姓名、电子邮箱和电话的 Contact 类
- 用于管理联系人的 ContactBook 类
- CRUD 操作(创建、读取、更新、删除)
- 搜索和筛选功能
- 将数据持久化到 JSON 文件
关键技能: 面向对象编程、JSON 序列化、数据结构
项目 3:文本分析器
分析文本文件中的统计信息和模式。
要求:
- 词频分析
- 字符计数(含空格/不含空格)
- 平均词长
- 最常见的词(排除停用词)
- 将结果导出为 CSV
关键技能: 字符串处理、collections.Counter、CSV
评估标准
- 编写符合 PEP 8 规范的 Python 代码
- 针对不同场景使用合适的数据结构
- 遵循正确的面向对象编程原则实现类
- 有效利用标准库模块
- 管理虚拟环境和依赖项
- 正确处理异常
- 编写清晰的文档字符串和注释
资源
官方文档
- Python.org - Python 官方文档
- PEP 8 - Python 风格指南
- Python 教程 - 官方教程
学习平台
- Real Python - 全面的教程
- Python for Everybody - 免费课程
- Automate the Boring Stuff - 实用 Python
工具
后续步骤
掌握 Python 基础后,继续学习:
- Django 框架 - 构建 Web 应用
- Pandas 数据分析 - 处理数据
- Pytest 测试 - 编写全面的测试
- Asyncio 编程 - 异步 Python
评论
支持 Markdown