分享

Python 从入门到精通知识点梳理

 网摘文苑 2025-03-01

一、Python 基础

1. Python 简介

  • Python 特点:解释型、动态类型、面向对象、跨平台。
  • 应用领域:Web 开发、数据分析、人工智能、自动化运维、科学计算等。
  • 安装与环境配置
    • 安装 Python(官网下载或使用 Anaconda)。
    • 配置环境变量。
    • 使用 IDE(如 PyCharm、VS Code)或 Jupyter Notebook。
Python 从入门到精通知识点梳理

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. 数据结构

  • 列表(List)
    • 创建:my_list = [1, 2, 3]。
    • 访问:my_list[0]。
    • 修改:my_list[0] = 10。
    • 常用方法:append(), remove(), pop(), sort(), reverse()。
  • 元组(Tuple)
    • 创建:my_tuple = (1, 2, 3)。
    • 不可变性:元组内容不可修改。
  • 字典(Dict)
    • 创建:my_dict = {'name': 'Alice', 'age': 25}。
    • 访问:my_dict['name']。
    • 常用方法:keys(), values(), items(), get()。
  • 集合(Set)
    • 创建: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 从入门到精通知识点梳理

二、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 从入门到精通知识点梳理

三、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 实战

  1. 项目开发
    • 设计项目结构。
    • 使用 Git 进行版本控制。
  1. 部署与优化
    • 使用 Docker 容器化应用。
    • 使用 Nginx 和 Gunicorn 部署 Web 应用。
  1. 开源贡献
    • 参与开源项目,提交代码和文档。

五、Python 生态

  1. 常用第三方库
    • 科学计算:numpy, scipy
    • 数据分析:pandas, statsmodels
    • 机器学习:scikit-learn, tensorflow, pytorch
    • Web 开发:Flask, Django, FastAPI
    • 自动化:selenium, pyautogui
  1. 工具与框架
    • 虚拟环境:venv, virtualenv
    • 包管理:pip, conda
    • 任务调度:celery
    • 日志管理:logging
Python 从入门到精通知识点梳理

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多