Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Python 基础教程

下面是 Python 编程基础知识最系统、最清晰的导图式总结,非常适合自学、复习、打基础。

内容分为 8 大模块,从最基础到能写实际项目。

🎯 1. Python 基础语法

✅ 1.1 注释

# 单行注释
"""
多行注释
"""

✅ 1.2 变量与基本类型(常用的)

详细文档:变量

  • int(整数)
  • float(浮点数)
  • bool(布尔)
  • str(字符串)
  • list(列表)
  • dict(字典)
  • datetime(日期时间)
  • None(空)

✅ 1.3 基本运算

详细文档:基本运算

  • 算术:+ - * / // % **
  • 比较:== != > < >= <=
  • 逻辑:and or not

🎯 2. 数据结构(Python 的核心)

✅ 2.1 列表 list

详细文档:列表

nums = [1, 2, 3]
nums.append(4)

✅ 2.2 元组 tuple(不可变)

详细文档:元组

t = (1, 2, 3)

✅ 2.3 集合 set(去重集合)

s = {1, 2, 3}

✅ 2.4 字典 dict(键值对)

person = {"name": "Tom", "age": 20}

🎯 3. 控制语句

✅ 3.1 条件判断

if x > 0:
    ...
elif x == 0:
    ...
else:
    ...

✅ 3.2 循环

for i in range(5):
    ...

while condition:
    ...

break / continue

🎯 4. 函数

函数定义

def add(a, b):
    return a + b

默认参数

def greet(name="Tom"):
    print("Hi", name)

可变参数

def f(*args, **kwargs):
    ...

🎯 5. 模块与包

导入模块

import math
from datetime import datetime

自己创建模块(文件 .py)

安装第三方库

pip install requests

🎯 6. 面向对象 OOP 基础

类与对象

class Dog:
    def __init__(self, name):
        self.name = name
    
    def bark(self):
        print(self.name, "barks")

d = Dog("Coco")
d.bark()

继承

class A:
    pass

class B(A):
    pass

方法类型:

  • 实例方法(self)
  • 类方法(@classmethod)
  • 静态方法(@staticmethod)

🎯 7. 文件操作

文件读取

with open("a.txt") as f:
    text = f.read()

文件写入

with open("a.txt", "w") as f:
    f.write("hello")

🎯 8. 常用标准库

用途
os 文件系统
sys 解释器相关
datetime 日期时间
json JSON 解析
math 数学函数
random 随机数
re 正则表达式

🎯 9. 错误与异常处理

try:
    x = 1 / 0
except ZeroDivisionError:
    print("division by zero")
finally:
    print("done")

🎯 10. Python 重要特性(进阶必知)

列表推导式

[x * 2 for x in range(5)]

生成器

def gen():
    yield 1
    yield 2

lambda

f = lambda x: x + 1

装饰器

def deco(fn):
    def wrapper():
        print("before")
        fn()
    return wrapper