> **GESP等级：三级 | 考级考点：GESP Python 三级**

# 第01节 列表操作 — 颜色仓库管理员

---

## 课程概况

| 项目 | 内容 |
|------|------|
| **课程名称** | 列表操作：append / insert / pop / remove / del / clear |
| **对应GESP考点** | #25（列表添加元素）、#26（列表删除元素） |
| **适用对象** | Python初学者（8-14岁） |
| **课时** | 60分钟 |
| **教学形式** | 情景模拟（颜色仓库管理）+ 代码实操 + 闯关练习 |

---

## 教学目标

### 知识与技能
1. 掌握 `append()` 向列表末尾追加元素
2. 掌握 `insert(索引, 值)` 向指定位置插入元素
3. 掌握 `pop()` 弹出并移除末尾元素（可指定索引）
4. 掌握 `remove(值)` 按值删除第一个匹配项
5. 掌握 `del 列表[索引]` 按索引删除元素
6. 掌握 `clear()` 清空列表
7. 掌握 `len()` 获取列表长度

### 过程与方法
- 通过"颜色仓库管理"生活场景，将抽象概念具象化
- 用"操作追踪填空"强化每个方法的记忆
- 用 pop+append 实现"撤销"功能，理解栈的经典用法

### 情感态度与价值观
- 建立"数据是可以被管理操控的"自信
- 体验"撤销"这一经典功能背后的编程思维

---

## 教学重难点

| 类型 | 内容 |
|------|------|
| **重点** | append / pop / remove 的用法与区别 |
| **难点** | insert 索引位置的理解；pop 返回值与 remove 无返回值的区别 |
| **易错点** | remove 只删第一个匹配项；del 是语句而非方法；insert 插入位置是目标索引 |

---

## 教学流程

### 第一环节：导入情境（00:00 - 10:00）— 颜色仓库管理员

**教师引导语：**
> "假设我们是一个'颜色仓库'的管理员，有一排货架（列表），上面摆着各种颜色。今天有三项任务：**加颜色、删颜色、改颜色**。"

**互动提问：**
1. "要在货架**末尾**放一个新颜色，怎么放？" → 引出 `append`
2. "要在货架**中间**某个位置插一个颜色，怎么插？" → 引出 `insert`
3. "要把货架上**最后一个**颜色拿走，怎么拿？" → 引出 `pop`
4. "要**清掉**一个指定的颜色（不管它在哪），怎么办？" → 引出 `remove`
5. "要**一锅端**把所有颜色清空，怎么办？" → 引出 `clear`

**板书/屏幕展示：**
```
货架(列表): ["red", "blue", "green", "yellow"]

加末尾  →  append("pink")     → ["red","blue","green","yellow","pink"]
插中间  →  insert(2, "black") → ["red","blue","black","green","yellow"]
拿末尾  →  pop()              → "pink"拿走，剩下["red","blue","green","yellow"]
删指定  →  remove("blue")     → ["red","green","yellow"]
改颜色  →  colors[0]="black"  → ["black","green","yellow"]
全清空  →  clear()            → []
```

---

### 第二环节：知识点精讲（10:00 - 30:00）

#### ① `append(值)` — 追加到末尾

```python
colors = ["red", "blue", "green"]
colors.append("pink")     # 末尾追加
print(colors)             # ["red", "blue", "green", "pink"]
```

- 特点：**原地修改**列表，不返回新列表
- 记忆：就像排队时新同学站到队伍**最后**

#### ② `insert(索引, 值)` — 插入到指定位置

```python
colors = ["red", "blue", "green"]
colors.insert(1, "black")  # 在索引1位置插入"black"
print(colors)              # ["red", "black", "blue", "green"]
```

- 注意：插入后，原来该位置及其后的元素**依次后移**
- 记忆：插队！插到第几个人后面
- 常见误区：`insert(0, x)` 是在最前面插入

#### ③ `pop(索引)` — 弹出（拿走并返回）

```python
colors = ["red", "blue", "green"]
last = colors.pop()        # 弹出最后一个
print(last)                # green
print(colors)              # ["red", "blue"]

first = colors.pop(0)      # 弹出索引0
print(first)               # red
print(colors)              # ["blue"]
```

- 特点：**有返回值**（被弹出的元素）
- 不传索引 → 弹出最后一个；传索引 → 弹出指定位置
- 记忆：从货架上**拿下来**一个颜色

#### ④ `remove(值)` — 按值删除第一个匹配

```python
colors = ["red", "blue", "green", "blue"]
colors.remove("blue")      # 删除第一个"blue"
print(colors)              # ["red", "green", "blue"]
```

- 特点：**无返回值**（None），**只删第一个**匹配项
- 如果值不存在 → 抛出 ValueError
- 记忆：**点名**删掉某个人

#### ⑤ `del 列表[索引]` — 按索引删除

```python
colors = ["red", "blue", "green"]
del colors[1]              # 删除索引1
print(colors)              # ["red", "green"]
```

- `del` 是 **语句**（statement），不是方法
- 可以连用切片：`del colors[1:3]`
- 也可以直接删变量：`del colors`

#### ⑥ `clear()` — 清空所有元素

```python
colors = ["red", "blue", "green"]
colors.clear()
print(colors)              # []
```

- 效果等同于 `colors[:] = []`
- 列表对象还在，只是变空了

#### ⑦ `len()` — 获取长度

```python
colors = ["red", "blue", "green"]
print(len(colors))         # 3
```

- 每增删一个元素，len 自动变化

---

### 第三环节：演示与讲解（30:00 - 40:00）

**现场演示 1 — 基础操作**
```python
colors = ["red", "blue", "green"]
print("初始:", colors)

colors.append("pink")
print("append pink后:", colors)

colors[0] = "black"        # 修改第一个元素
print("修改索引0为black后:", colors)

colors.insert(2, "purple")
print("insert purple到索引2:", colors)

removed = colors.pop()
print("pop弹出:", removed)
print("pop后:", colors)

colors.remove("blue")
print("remove blue后:", colors)

print("最终长度:", len(colors))
```

**现场演示 2 — 用pop做"撤销"**
```python
# 模拟一个简单的"操作记录"
history = []

def do_action(action):
    history.append(action)            # 记录操作
    print(f"执行: {action}，当前历史: {history}")

def undo():
    if history:
        last = history.pop()          # 撤销最后一次
        print(f"撤销: {last}，剩余历史: {history}")
    else:
        print("没有可撤销的操作")

do_action("添加红色")
do_action("添加蓝色")
do_action("添加绿色")
undo()
undo()
```

---

### 第四环节：实操闯关（40:00 - 55:00）

#### 实操①：操作追踪填空（10分钟）

在代码中补充正确的方法名：

```python
colors = ["red", "blue", "green"]

# 1. 在末尾追加"yellow"
colors.______("yellow")              # 填: append

# 2. 在索引1处插入"black"
colors.______(1, "black")            # 填: insert

# 3. 弹出最后一个颜色
last = colors.______()               # 填: pop

# 4. 删除第一个"blue"
colors.______("blue")                # 填: remove

# 5. 删除索引0的元素
____ colors[0]                       # 填: del

# 6. 清空列表
colors.______()                      # 填: clear
```

#### 实操②：补全颜色库管理（15分钟）

```python
# 任务：补全下面的代码，实现一个简单的颜色仓库管理
color_library = ["red", "blue", "green", "yellow"]

def show_library():
    print("当前颜色库:", color_library, "| 数量:", len(color_library))

def add_color(color):
    # TODO: 将颜色追加到末尾

def insert_color(index, color):
    # TODO: 在指定位置插入颜色

def remove_color(color):
    # TODO: 按名称删除颜色

def modify_color(index, new_color):
    # TODO: 修改指定索引的颜色

def pop_last():
    # TODO: 弹出最后一个颜色并打印

# 测试代码
show_library()
add_color("pink")
show_library()
insert_color(2, "black")
show_library()
remove_color("blue")
show_library()
modify_color(0, "dark_red")
show_library()
popped = pop_last()
print("弹出的颜色:", popped)
show_library()
```

#### 实操③：用pop+append实现"撤销"（10分钟）

```python
# 任务：实现一个"颜色操作"的撤销功能
# 每次往列表中添加一个颜色，都记录到历史栈中
# 调用undo()时撤销最后一次添加

color_list = []
history = []    # 记录每次添加的颜色

def add(color):
    # TODO: 添加颜色到color_list，同时记录到history

def undo():
    # TODO: 如果history不为空，撤销最后一次添加

def show():
    print("当前颜色:", color_list)

# 测试
add("red")
add("blue")
add("green")
show()          # ["red", "blue", "green"]
undo()
show()          # ["red", "blue"]
undo()
show()          # ["red"]
```

---

### 第五环节：小测与总结（55:00 - 60:00）

#### 小测题目

**选择题：**
> **`append` 和 `insert` 的区别是什么？**

A. append 可以加在任意位置，insert 只能加在末尾
B. append 加在末尾，insert 可以加在指定位置
C. append 和 insert 效果一样
D. append 只能加一个，insert 可以加多个

**正确答案：B**

**简答题：**
> 使用 `remove("blue")` 和 `del colors[2]` 删除元素有什么不同？

参考答案：
- `remove("blue")` 按**值**删除，不需要知道索引；只删除第一个匹配项；值不存在会报错
- `del colors[2]` 按**索引**删除，需要事先知道位置；可以结合切片批量删除

#### 🧠 记忆钩子

```
append  追加尾      →  末尾追加
insert  插中间      →  指定位置插入
pop     弹最后      →  弹出（有返回值）
remove  删指定      →  按值删除第一个
del     按索引删    →  语句删除
clear   全清空      →  清空列表
len     查长度      →  获取元素个数
```

#### 本节课GESP考点对应

| GESP考点编号 | 考点内容 | 对应知识点 |
|-------------|---------|-----------|
| #25 | 列表添加元素 | append / insert |
| #26 | 列表删除元素 | pop / remove / del / clear |

---

## 课后作业

详见 `课后练习卡_第01节_列表操作.md`

## 教师参考

详见 `教师参考_答案.md`
