A comprehensive, beginner-friendly collection of Python notes and runnable scripts. Topics include core syntax, OOP, exceptions, files & OS, modules/packages, web scraping, and mini Pygame projects. Each example is small, focused, and easy to run from the command line.
- Practical, hands-on learning with minimal boilerplate
- Organized by topic for quick lookup and reference
- Examples are self-contained and demonstrate one concept at a time
- Suitable for self-study, coursework, and interview warm‑ups
基本编程语法学习笔记/
├─ 单例设计/ # design patterns: singleton via __new__
├─ 封装系统项目集/ # simple encapsulation exercises & projects
├─ 异常/ # try/except, raise, propagation
├─ 文件的基本操作/ # file I/O, copy, read/write, os utilities
├─ 模块/ # imports, packages, aliases, setup
│ └─ hm_message/ # tiny example package (send/receive)
├─ 爬虫/ # requests, BeautifulSoup, regex, CSV outputs
│ ├─ 学习案例/ # small learning demos
│ └─ 实战正式项目/ # full scraping scripts with data
├─ pygame项目实战/ # step-by-step Pygame demos & mini games
├─ test/ test1/ # small sandboxes for experiments
└─ resources/screenshots/ # put screenshots used in README here
__new__方法重写.py: Demonstrates overriding__new__to ensure only one instance is created. Explains lifecycle differences between__new__and__init__, object identity (id()), and typical pitfalls such as multiple inheritance.- Use cases: configuration managers, connection pools, and loggers.
士兵开枪封装案例.py: Encapsulates Soldier and Gun interactions; methods likefire(), ammo management, and responsibility boundaries between objects.- 名片管理系统:
cards_main.py,cards_tools.py: CLI CRUD demo with functions for add/search/update/delete, file persistence patterns to extend, and simple input validation. - 房子家具增添系统: 家具类与房屋类的组合关系、面积占用、可用空间检测与异常处理建议。
完整的异常语法.py: Full try/except/else/finally flow; where to place cleanup.异常的传递.py: Shows stack propagation and where exceptions surface.异常的简单捕获.py,捕获错误类型.py: Catching broad vs specific exceptions; trade-offs.抛出异常.py: Raising custom exceptions with helpful messages.
- Read/Write:
读取文件.py,写入文件.py, line-by-line分行读取文件.pyfor memory efficiency. - Copying:
复制小文件.pyvs复制大文件.py(buffered reads and chunk sizes). - Utilities:
os模块.pyfor path ops, listing, and existence checks. eval计算器.py: Parsing simple arithmetic safely; caveats and safer alternatives (ast.literal_eval).- Notes:
ReadmeandReadme[复件]contain quick tips.
- Import styles:
导入模块.py,from_import 导入.py, aliases导入模块指定别名.py. - Package example
hm_message/:send_message.py,receive_message.py,__init__.pylayout and relative imports. setup.py: Minimal packaging primer; how Python finds modules; PYTHONPATH basics.- Name guard demos:
_name_测试导入.py,_name_测试模块.pyillustrateif __name__ == "__main__":usage.
- 学习案例:
requests爬虫.py: Basic GET requests, headers, response handling.Beautiful soup 爬虫.py: Parsing HTML withbs4, element selection.正则表达式*.py: Extracting structured data via regex; pros/cons vs DOM parsers.- JSON conversions:
json转换为python.py,把python转变为json.py, sampletext.json.
- 实战正式项目:
豆瓣高分电影排行榜*.py: Fetch and writedouban_movies.csvwith columns like title, rating, url.- 番茄小说采集:
番茄小说数据爬取*.py,fanqie_*.csvoutputs; pagination and category selection. 当当网爬取数据.py: Example of multi-page scraping and deduplication strategies.爬取网页所有图片.py: Download images; file naming, collision handling, and retry policies.疫情采集信息项目.py: Data extraction into CSV/JSON; rate-limit and error handling notes.- Ethics & legality: obey
robots.txt, add delays, and do not overload servers.
单继承.py,多继承.py: Linear vs diamond inheritance; method resolution order (MRO).覆盖父类方法.py,拓展父类方法.py: Overriding and callingsuper()correctly.多态案例.py: Interface-based programming; behavior substitution.- Attributes/methods:
类属性.py,类方法.py,静态方法.pydifferences and use cases. - Privacy:
父类的私有属性及方法.py,私有属性与方法.pyshow name mangling and access patterns.
1.pygame入门.py→9.事件监听.py: Progressive steps from window creation, drawing backgrounds and sprites,Rectusage, to game loop and event handling.大鱼吃小鱼.py: A small playable demo; movement, collision detection, scoring, and simple state management.测试模块.py: Quick checks and sandboxes for Pygame utilities.- Images under
pygame项目实战/images/used by demos.
test/,test1/subfolders provide scratch space for experimenting with code snippets and quick validations.
- Requirements:
Python 3.8+ - Optional dependencies for specific folders are listed in
requirements.txt:requestsandbeautifulsoup4for web scrapingpygamefor Pygame demos
Install all optional dependencies at once:
pip install -r requirements.txt- Navigate to the folder of the example.
- Run the script in
cmd:
python 路径\到\示例.py- For scraping scripts in
爬虫/实战正式项目/, make sure you have network access and note that outputs like CSV files will be created next to the script. - For
pygame项目实战/, ensurepygameis installed and a windowed environment is available. - For package demos under
模块/hm_message/, run a module or import functions:
python 模块\_name_测试模块.py
python -c "from 模块.hm_message.send_message import send; send()"- Prefer virtual environments for isolating dependencies.
- On Windows, enable UTF‑8 in
cmdif you see encoding issues:
chcp 65001- Use
python -m pip install ...ifpipis not on PATH.
- OOP & Patterns: inheritance, polymorphism, overriding, singleton via
__new__ - Exceptions: error types, simple/advanced catching, raising, propagation
- Files & OS: safe read/write, copying large files,
osutilities,evaldemo - Modules & Packages: absolute/relative imports, aliases, simple package layout
- Web Scraping:
requests,BeautifulSoup, regex parsing, data export to CSV - Pygame: window setup, drawing, events, game loop, small playable demos
- Scraping: CSV files like
douban_movies.csv,fanqie_*.csvwith headers; logs printed on progress and failures. - Files: Copies created in destination folders; large file copies use chunked reads.
- Pygame: Window renders sprites and detects collisions; close with
Escor window button.
- Q: Why are there many small scripts?
- A: Each script focuses on one concept to reduce cognitive load.
- Q: Do I need all dependencies?
- A: No. Install only what's needed per folder, or use
requirements.txtto install all.
- A: No. Install only what's needed per folder, or use
- Q: Can I use this for teaching?
- A: Yes. The examples are designed for classroom demos and assignments.
- This repository is actively curated for teaching and self‑study.
- Examples are intentionally small and focused; feel free to extend.
- Fork the repository and create a feature branch.
- Keep examples small and focused; match folder/topic organization.
- Add a short docstring and minimal comments to explain intent.
- Provide sample inputs/outputs when relevant (CSV, JSON, screenshots).
MIT