一、Python 基础1. Python 简介- Python 特点:解释型、动态类型、面向对象、跨平台。
- 应用领域:Web 开发、数据分析、人工智能、自动化运维、科学计算等。
- 安装与环境配置:
- 安装 Python(官网下载或使用 Anaconda)。
- 配置环境变量。
- 使用 IDE(如 PyCharm、VS Code)或 Jupyter Notebook。
2. 基础语法- 变量命名规则(字母、数字、下划线,不能以数字开头)。
- 数据类型:整数(int)、浮点数(float)、字符串(str)、布尔值(bool)。
- 输入:input()。
- 输出:print(),格式化输出(f-string、format())。
3. 运算符- 算术运算符:+, -, *, /, //(整除), %(取余), **(幂运算)。
- 比较运算符:==, !=, >, <, >=, <=。
- 逻辑运算符:and, or, not。
- 赋值运算符:=, +=, -=, *=, /= 等。
4. 控制结构条件语句: if condition: # 代码块elif another_condition: # 代码块else: # 代码块
for 循环: for i in range(10): print(i)
while 循环: while condition: # 代码块
- break:跳出循环。
- continue:跳过当前迭代。
- pass:占位符,什么都不做。
5. 数据结构- 创建:my_list = [1, 2, 3]。
- 访问:my_list[0]。
- 修改:my_list[0] = 10。
- 常用方法:append(), remove(), pop(), sort(), reverse()。
- 创建:my_tuple = (1, 2, 3)。
- 不可变性:元组内容不可修改。
- 创建:my_dict = {'name': 'Alice', 'age': 25}。
- 访问:my_dict['name']。
- 常用方法:keys(), values(), items(), get()。
- 创建:my_set = {1, 2, 3}。
- 特性:去重、无序。
- 集合运算:union(), intersection(), difference()。
6. 函数定义与调用: def my_function(param1, param2): return param1 + param2
- 默认参数:def func(a, b=10)。
- 关键字参数:func(a=1, b=2)。
- 可变参数:*args(元组),**kwargs(字典)。
- 局部变量:函数内部定义的变量。
- 全局变量:函数外部定义的变量(使用 global 关键字修改)。
匿名函数: add = lambda x, y: x + y
二、Python 进阶1. 文件操作打开与关闭文件: file = open('file.txt', 'r')content = file.read()file.close()
上下文管理器: with open('file.txt', 'r') as file: content = file.read()
- r:只读。
- w:写入(覆盖)。
- a:追加。
- b:二进制模式。
2. 异常处理捕获异常: try: # 可能出错的代码except Exception as e: print(f'Error: {e}')finally: # 无论是否出错都会执行
自定义异常: class MyError(Exception): passraise MyError('Something went wrong')
3. 模块与包模块导入: import mathfrom math import sqrt
- os:操作系统交互。
- sys:系统相关功能。
- math:数学函数。
- random:随机数生成。
- datetime:日期和时间处理。
- 创建一个文件夹,包含 __init__.py 文件。
4. 面向对象编程(OOP)类与对象: class MyClass: def __init__(self, name): self.name = name def greet(self): print(f'Hello, {self.name}')obj = MyClass('Alice')obj.greet()
继承与多态: class Parent: def greet(self): print('Hello from Parent')class Child(Parent): def greet(self): print('Hello from Child')
- __str__:定义对象的字符串表示。
- __len__:定义对象的长度。
5. 高级特性列表推导式: squares = [x**2 for x in range(10)]
生成器: def my_generator(): yield 1 yield 2
装饰器: def my_decorator(func): def wrapper(): print('Before function call') func() print('After function call') return wrapper@my_decoratordef say_hello(): print('Hello')
三、Python 高级1. 正则表达式- .:匹配任意字符。
- *:匹配 0 次或多次。
- +:匹配 1 次或多次。
- \d:匹配数字。
使用 re 模块: import reresult = re.search(r'\d+', 'abc123')
2. 网络编程套接字编程: import sockets = socket.socket()s.connect(('localhost', 8080))
HTTP 请求: import requestsresponse = requests.get('https://www.')
3. 数据库操作SQLite: import sqlite3conn = sqlite3.connect('example.db')cursor = conn.cursor()cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)')
4. 并发编程多线程: import threadingdef worker(): print('Worker thread')t = threading.Thread(target=worker)t.start()
异步编程: import asyncioasync def main(): print('Hello') await asyncio.sleep(1) print('World')asyncio.run(main())
四、Python 实战- 项目开发:
- 部署与优化:
- 使用 Docker 容器化应用。
- 使用 Nginx 和 Gunicorn 部署 Web 应用。
- 开源贡献:
五、Python 生态- 常用第三方库
- 科学计算:numpy, scipy
- 数据分析:pandas, statsmodels
- 机器学习:scikit-learn, tensorflow, pytorch
- Web 开发:Flask, Django, FastAPI
- 自动化:selenium, pyautogui
- 工具与框架
- 虚拟环境:venv, virtualenv
- 包管理:pip, conda
- 任务调度:celery
- 日志管理:logging
|