Python 零基础入门教程 — 最友好的编程语言

适用人群:零基础、数据分析师、后端开发者、AI/ML入门
学习时长:约3-5天(每天4小时)
Python版本:3.10+
适用场景:Web开发、数据分析、人工智能、自动化脚本、爬虫

一、Python 是什么?

Python 是全球最受欢迎的编程语言之一,以简洁易读的语法著称,广泛应用于AI、数据科学、Web开发等领域。

特点说明
上手难度★☆☆☆☆(最易入门的编程语言)
语法特点接近自然语言,用缩进表示代码块
主要用途AI/机器学习、数据分析、Web开发、自动化
市场需求AI时代需求爆发,薪资水平高
薪资水平初级10-18K,中级18-30K,高级30K+(AI方向更高)

二、环境搭建

2.1 安装 Python

# macOS(自带Python3,建议用Homebrew更新)
brew install python@3.12

# Ubuntu
sudo apt update
sudo apt install python3 python3-pip python3-venv

# Windows
# 下载:https://www.python.org/downloads/
# 安装时勾选 "Add Python to PATH"

# 验证安装
python3 --version
pip3 --version

2.2 虚拟环境(推荐)

# 创建虚拟环境
python3 -m venv myenv

# 激活虚拟环境
# macOS/Linux
source myenv/bin/activate
# Windows
myenv\Scripts\activate

# 退出虚拟环境
deactivate

# 安装包
pip install requests flask pandas
pip install -r requirements.txt

# 导出依赖
pip freeze > requirements.txt


三、Python 基础语法

3.1 变量与数据类型

# ====================
# 变量(无需声明类型)
# ====================
name = "张三"           # 字符串
age = 25               # 整数
height = 175.5         # 浮点数
is_student = True      # 布尔值
skills = ["Python", "SQL", "JavaScript"]  # 列表
user = {"name": "张三", "age": 25}        # 字典
coordinates = (116.4, 39.9)               # 元组(不可变)
unique_ids = {1, 2, 3, 3}                 # 集合(自动去重)

# 类型检查
print(type(name))      # <class 'str'>
print(isinstance(age, int))  # True

# 类型转换
num_str = "42"
num = int(num_str)     # 字符串转整数
float_num = float("3.14")
str_num = str(100)     # 整数转字符串

3.2 字符串操作

# ====================
# 字符串基础
# ====================
name = "张三"
greeting = f"你好,{name}!"  # f-string(推荐)
info = "姓名:{},年龄:{}".format("张三", 25)

# ====================
# 常用方法
# ====================
text = "  Hello, Python World!  "
text.strip()           # 去除首尾空格
text.lower()           # 转小写
text.upper()           # 转大写
text.replace("World", "Python")  # 替换
text.split(",")        # 分割:['  Hello', ' Python World!  ']
text.find("Python")    # 查找位置:10
text.count("o")        # 计数:2

# 判断方法
"hello".isalpha()      # 是否全是字母
"12345".isdigit()      # 是否全是数字
"hello123".isalnum()   # 是否字母数字混合

# 切片
s = "Hello Python"
s[0:5]     # "Hello"
s[6:]      # "Python"
s[::-1]    # "nohtyP olleH"(反转)

# 多行字符串
multi = """
第一行
第二行
第三行
"""

3.3 列表(最常用的数据结构)

# ====================
# 列表操作
# ====================
fruits = ["苹果", "香蕉", "橙子"]

# 增
fruits.append("葡萄")          # 末尾追加
fruits.insert(1, "草莓")       # 指定位置插入
fruits.extend(["西瓜", "芒果"]) # 扩展列表

# 删
fruits.remove("香蕉")   # 删除指定元素
fruits.pop()            # 删除最后一个
fruits.pop(0)           # 删除指定位置
del fruits[1]           # 删除指定位置

# 改
fruits[0] = "红苹果"

# 查
"苹果" in fruits        # True
fruits.index("橙子")    # 查找位置
fruits.count("苹果")    # 计数

# 排序
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()           # 原地排序
numbers.sort(reverse=True)  # 降序
sorted_numbers = sorted(numbers)  # 返回新列表

# 切片
numbers = [0, 1, 2, 3, 4, 5]
numbers[1:4]     # [1, 2, 3]
numbers[:3]      # [0, 1, 2]
numbers[2:]      # [2, 3, 4, 5]
numbers[::-1]    # [5, 4, 3, 2, 1, 0]

# ====================
# 列表推导式(Python特色)
# ====================
# 基本语法
squares = [x**2 for x in range(10)]  # [0, 1, 4, 9, 16, ...]

# 带条件
even = [x for x in range(20) if x % 2 == 0]  # [0, 2, 4, 6, ...]

# 带操作
names = ["张三", "李四", "王五"]
upper_names = [name.upper() for name in names]

# 嵌套
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat = [num for row in matrix for num in row]  # [1, 2, 3, 4, 5, 6, 7, 8, 9]

3.4 字典(键值对)

# ====================
# 字典操作
# ====================
user = {
    "name": "张三",
    "age": 25,
    "email": "zhangsan@example.com",
    "skills": ["Python", "SQL"]
}

# 增/改
user["phone"] = "13800138000"   # 新增
user["age"] = 26                # 修改
user.update({"city": "北京", "age": 27})  # 批量更新

# 删
del user["phone"]
email = user.pop("email")  # 删除并返回值

# 查
name = user["name"]          # 直接访问(不存在会报错)
name = user.get("name")     # 安全访问(不存在返回None)
name = user.get("name", "未知")  # 不存在返回默认值

# 判断
"name" in user              # True
user.keys()                 # 所有键
user.values()               # 所有值
user.items()                # 所有键值对

# ====================
# 字典推导式
# ====================
squares = {x: x**2 for x in range(6)}  # {0:0, 1:1, 2:4, 3:9, 4:16, 5:25}

# 从列表创建字典
names = ["张三", "李四", "王五"]
name_dict = {name: len(name) for name in names}  # {"张三": 2, "李四": 2, "王五": 2}

3.5 条件与循环

# ====================
# 条件判断
# ====================
age = 25

if age >= 18:
    print("成年人")
elif age >= 12:
    print("青少年")
else:
    print("儿童")

# 三元表达式
status = "成年" if age >= 18 else "未成年"

# ====================
# 循环
# ====================
# for循环
for i in range(5):          # 0, 1, 2, 3, 4
    print(i)

for i in range(2, 10, 2):   # 2, 4, 6, 8
    print(i)

# 遍历列表
fruits = ["苹果", "香蕉", "橙子"]
for fruit in fruits:
    print(fruit)

# 带索引遍历
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

# 遍历字典
user = {"name": "张三", "age": 25}
for key, value in user.items():
    print(f"{key}: {value}")

# while循环
count = 0
while count < 5:
    print(count)
    count += 1

# break 和 continue
for i in range(10):
    if i == 3:
        continue  # 跳过3
    if i == 7:
        break     # 到7停止
    print(i)


四、函数

# ====================
# 基本函数
# ====================
def greet(name):
    return f"你好,{name}!"

print(greet("张三"))

# ====================
# 默认参数
# ====================
def create_user(name, role="user", active=True):
    return {"name": name, "role": role, "active": active}

user = create_user("张三")  # role和active使用默认值

# ====================
# 可变参数
# ====================
def calc_sum(*args):
    return sum(args)

print(calc_sum(1, 2, 3, 4))  # 10

def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_info(name="张三", age=25, city="北京")

# ====================
# Lambda 匿名函数
# ====================
add = lambda x, y: x + y
print(add(3, 4))  # 7

# 在排序中常用
users = [{"name": "张三", "age": 25}, {"name": "李四", "age": 20}]
users.sort(key=lambda u: u["age"])

# ====================
# 类型提示(Python 3.5+)
# ====================
def add_numbers(a: int, b: int) -> int:
    return a + b

def get_user(user_id: int) -> dict | None:
    # 返回字典或None
    pass


五、面向对象编程

# ====================
# 类与对象
# ====================
class User:
    # 类属性(所有实例共享)
    count = 0
    
    # 构造方法
    def __init__(self, name: str, age: int):
        self.name = name      # 实例属性
        self.age = age
        User.count += 1       # 类属性自增
    
    # 实例方法
    def greet(self) -> str:
        return f"我是{self.name},今年{self.age}岁"
    
    # 静态方法(不需要实例化)
    @staticmethod
    def is_adult(age: int) -> bool:
        return age >= 18
    
    # 类方法(操作类属性)
    @classmethod
    def get_count(cls) -> int:
        return cls.count
    
    # 魔术方法
    def __str__(self) -> str:
        return f"User({self.name}, {self.age})"
    
    def __repr__(self) -> str:
        return f"User('{self.name}', {self.age})"

# 使用
user1 = User("张三", 25)
user2 = User("李四", 30)

print(user1.greet())          # 我是张三,今年25岁
print(User.get_count())       # 2
print(User.is_adult(20))      # True
print(str(user1))             # User(张三, 25)

# ====================
# 继承
# ====================
class Admin(User):
    def __init__(self, name: str, age: int, role: str = "admin"):
        super().__init__(name, age)
        self.role = role
    
    def greet(self) -> str:
        return super().greet() + f"({self.role})"
    
    def delete_user(self, user: User) -> str:
        return f"管理员{self.name}删除了用户{user.name}"

admin = Admin("管理员", 30)
print(admin.greet())  # 我是管理员,今年30岁(admin)

# ====================
# 属性装饰器
# ====================
class Circle:
    def __init__(self, radius: float):
        self._radius = radius
    
    @property
    def radius(self) -> float:
        return self._radius
    
    @radius.setter
    def radius(self, value: float):
        if value < 0:
            raise ValueError("半径不能为负数")
        self._radius = value
    
    @property
    def area(self) -> float:
        import math
        return math.pi * self._radius ** 2

circle = Circle(5)
print(circle.area)    # 78.54...
circle.radius = 10    # 通过setter修改


六、文件操作与异常处理

# ====================
# 文件读写
# ====================
# 写入文件
with open("data.txt", "w", encoding="utf-8") as f:
    f.write("第一行\n")
    f.write("第二行\n")

# 读取文件
with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()        # 读取全部
    # 或逐行读取
    lines = f.readlines()     # ['第一行\n', '第二行\n']

# 追加写入
with open("data.txt", "a", encoding="utf-8") as f:
    f.write("第三行\n")

# ====================
# JSON 操作
# ====================
import json

# 写入JSON
data = {"name": "张三", "age": 25, "skills": ["Python", "SQL"]}
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

# 读取JSON
with open("data.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    print(data["name"])

# 字符串转换
json_str = json.dumps(data, ensure_ascii=False)
data = json.loads(json_str)

# ====================
# 异常处理
# ====================
try:
    result = 10 / 0
except ZeroDivisionError:
    print("不能除以零")
except Exception as e:
    print(f"发生错误:{e}")
else:
    print("没有错误发生")
finally:
    print("无论如何都会执行")

# 自定义异常
class ValidationError(Exception):
    pass

def validate_age(age: int):
    if age < 0 or age > 150:
        raise ValidationError(f"年龄无效:{age}")

try:
    validate_age(200)
except ValidationError as e:
    print(e)  # 年龄无效:200


七、常用标准库

# ====================
# os - 操作系统相关
# ====================
import os

os.getcwd()                    # 当前工作目录
os.listdir(".")                # 列出目录内容
os.makedirs("data/output", exist_ok=True)  # 创建目录
os.path.exists("file.txt")    # 文件是否存在
os.path.join("data", "file.txt")  # 路径拼接

# ====================
# datetime - 日期时间
# ====================
from datetime import datetime, timedelta

now = datetime.now()           # 当前时间
today = datetime.today().date() # 今天日期
formatted = now.strftime("%Y-%m-%d %H:%M:%S")  # 格式化
parsed = datetime.strptime("2024-01-15", "%Y-%m-%d")  # 解析

# 时间计算
tomorrow = now + timedelta(days=1)
last_week = now - timedelta(weeks=1)

# ====================
# random - 随机数
# ====================
import random

random.randint(1, 100)         # 随机整数
random.choice(["a", "b", "c"]) # 随机选择
random.random()                # 0-1随机浮点数
random.shuffle([1, 2, 3, 4])  # 打乱列表

# ====================
# re - 正则表达式
# ====================
import re

text = "我的手机号是13800138000,邮箱是test@example.com"
phone = re.search(r"1[3-9]\d{9}", text)
if phone:
    print(phone.group())  # 13800138000

emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)  # ['test@example.com']


八、Flask Web 框架入门

# app.py
from flask import Flask, request, jsonify

app = Flask(__name__)

# 模拟数据库
users = [
    {"id": 1, "name": "张三", "email": "zhangsan@example.com"},
    {"id": 2, "name": "李四", "email": "lisi@example.com"}
]

# GET - 获取用户列表
@app.route("/api/users", methods=["GET"])
def get_users():
    keyword = request.args.get("keyword", "")
    if keyword:
        filtered = [u for u in users if keyword in u["name"]]
        return jsonify(filtered)
    return jsonify(users)

# GET - 获取单个用户
@app.route("/api/users/<int:user_id>", methods=["GET"])
def get_user(user_id):
    user = next((u for u in users if u["id"] == user_id), None)
    if user:
        return jsonify(user)
    return jsonify({"error": "用户不存在"}), 404

# POST - 创建用户
@app.route("/api/users", methods=["POST"])
def create_user():
    data = request.get_json()
    if not data or not data.get("name"):
        return jsonify({"error": "用户名不能为空"}), 400
    
    new_user = {
        "id": max(u["id"] for u in users) + 1,
        "name": data["name"],
        "email": data.get("email", "")
    }
    users.append(new_user)
    return jsonify(new_user), 201

# PUT - 更新用户
@app.route("/api/users/<int:user_id>", methods=["PUT"])
def update_user(user_id):
    user = next((u for u in users if u["id"] == user_id), None)
    if not user:
        return jsonify({"error": "用户不存在"}), 404
    
    data = request.get_json()
    user.update(data)
    return jsonify(user)

# DELETE - 删除用户
@app.route("/api/users/<int:user_id>", methods=["DELETE"])
def delete_user(user_id):
    global users
    users = [u for u in users if u["id"] != user_id]
    return jsonify({"message": "删除成功"})

if __name__ == "__main__":
    app.run(debug=True, port=5000)

# 安装Flask
pip install flask

# 运行
python app.py

# 访问 http://localhost:5000/api/users


九、Django 框架入门

# 安装Django
pip install django

# 创建项目
django-admin startproject myproject
cd myproject

# 创建应用
python manage.py startapp myapp

# 运行开发服务器
python manage.py runserver

# myapp/models.py
from django.db import models

class User(models.Model):
    username = models.CharField(max_length=50, unique=True)
    email = models.EmailField()
    age = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'users'
    
    def __str__(self):
        return self.username

# myapp/views.py
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .models import User
import json

def get_users(request):
    users = list(User.objects.values())
    return JsonResponse(users, safe=False)

@csrf_exempt
def create_user(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        user = User.objects.create(**data)
        return JsonResponse({'id': user.id, 'username': user.username}, status=201)

# myapp/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('api/users/', views.get_users),
    path('api/users/create/', views.create_user),
]


十、Python 爬虫入门

# ====================
# requests + BeautifulSoup
# ====================
# pip install requests beautifulsoup4

import requests
from bs4 import BeautifulSoup

# 发送请求
url = "https://example.com"
response = requests.get(url)
response.encoding = "utf-8"

# 解析HTML
soup = BeautifulSoup(response.text, "html.parser")

# 提取数据
title = soup.find("h1").text
links = [a["href"] for a in soup.find_all("a")]
articles = soup.find_all("article")
for article in articles:
    print(article.find("h2").text)

# ====================
# 实战:爬取B站视频信息
# ====================
import requests

def get_bilibili_video(bvid):
    url = f"https://api.bilibili.com/x/web-interface/view?bvid={bvid}"
    headers = {
        "User-Agent": "Mozilla/5.0",
        "Referer": "https://www.bilibili.com"
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    
    if data["code"] == 0:
        video = data["data"]
        return {
            "title": video["title"],
            "author": video["owner"]["name"],
            "play": video["stat"]["view"],
            "danmaku": video["stat"]["danmaku"],
            "duration": video["duration"]
        }
    return None

video = get_bilibili_video("BV1Lx4y1D7em")
print(video)


十一、数据分析基础(Pandas)

# pip install pandas openpyxl

import pandas as pd

# ====================
# 读取数据
# ====================
# 读取CSV
df = pd.read_csv("data.csv")

# 读取Excel
df = pd.read_excel("data.xlsx")

# 读取JSON
df = pd.read_json("data.json")

# ====================
# 基本操作
# ====================
df.head()           # 查看前5行
df.shape            # (行数, 列数)
df.info()           # 数据信息
df.describe()       # 统计摘要

# 选择列
df["name"]          # 单列
df[["name", "age"]] # 多列

# 筛选行
df[df["age"] > 25]                    # 条件筛选
df[(df["age"] > 25) & (df["city"] == "北京")]  # 多条件

# 排序
df.sort_values("age", ascending=False)

# 分组统计
df.groupby("city").agg({
    "age": "mean",
    "name": "count"
})

# ====================
# 数据清洗
# ====================
df.dropna()                    # 删除空值行
df.fillna(0)                   # 空值填充0
df.drop_duplicates()           # 删除重复行
df["name"].str.strip()         # 去除空格

# ====================
# 导出数据
# ====================
df.to_csv("output.csv", index=False)
df.to_excel("output.xlsx", index=False)


学习建议

  1. 先掌握基础语法,Python语法简洁,2-3天就能入门
  2. 重点掌握列表推导式和字典操作,这是Python的特色
  3. 选择一个方向深入:Web开发(Flask/Django) 或 数据分析(Pandas)
  4. 多写小脚本,用Python解决实际问题(文件处理、数据清洗等)
  5. 学完基础后学异步编程(asyncio),提升编程能力

下一步学习

返回首页