> **GESP等级：二级 | 考级考点：GESP Python 二级**

# 第1节 列表基础——创建 + 索引 + 遍历

**GESP考点：** #23 列表创建与索引、#24 列表遍历

---

## 导入：一排编号盒子 = 列表

| 普通变量 | 列表 |
|:--------:|:----:|
| 一个变量装一个值 | 一个列表装多个值 |
| `color1 = "red"` | `colors = ["red", "blue", "green"]` |
| 100种颜色要100个变量 | 一个列表搞定 |

**图示：**

```
┌─────┬──────┬───────┐
│ red │ blue │ green │
├─────┼──────┼───────┤
│ [0] │ [1]  │  [2]  │  ← 编号（索引）从0开始！
└─────┴──────┴───────┘
```

> **记忆钩子："列表 = 一排贴编号盒子，编号从0开始"**

---

## ① 创建列表

```python
# 语法
列表名 = [元素1, 元素2, 元素3, ...]

# 示例
colors  = ["red", "blue", "green"]       # 字符串
numbers = [10, 20, 30, 40, 50]           # 整数
mix     = ["hello", 100, True, 3.14]     # 混合类型
```

**要点：**
- 方括号 `[]` 创建列表 ← "方盒子用方括号"
- 逗号 `,` 分隔元素
- 元素可以不同类型

---

## ② 正索引：从0开始

```python
colors = ["red", "blue", "green", "yellow", "purple"]
# 索引:   [0]     [1]      [2]      [3]       [4]

print(colors[0])   # "red"    ← 第1个
print(colors[2])   # "green"  ← 第3个
print(colors[4])   # "purple" ← 最后1个
```

**规则：**
- 第1个元素索引是 **0**
- 最后一个元素索引 = `len(列表) - 1`
- 可以修改：`colors[1] = "pink"`

> **⚠️ 常见错误：** `IndexError: list index out of range`（索引越界）

---

## ③ 负索引：-1是最后一个

```python
colors = ["red", "blue", "green", "yellow", "purple"]
# 正索引: [0]    [1]     [2]      [3]       [4]
# 负索引: [-5]   [-4]    [-3]     [-2]      [-1]

print(colors[-1])   # "purple"  ← 最后一个
print(colors[-2])   # "yellow"  ← 倒数第二个
print(colors[-3])   # "green"   ← 倒数第三个
```

**对照表：**

| 正索引 | 0 | 1 | 2 | 3 | 4 |
|:-----:|:-:|:-:|:-:|:-:|:-:|
| 元素 | red | blue | green | yellow | purple |
| 负索引 | -5 | -4 | -3 | -2 | -1 |

---

## ④ for循环遍历列表

### 方式一：直接遍历（推荐）

```python
colors = ["red", "blue", "green"]
for c in colors:
    print(c)

# 输出：
# red
# blue
# green
```

### 方式二：索引遍历

```python
for i in range(len(colors)):
    print(f"colors[{i}] = {colors[i]}")
```

### 方式三：enumerate

```python
for i, c in enumerate(colors):
    print(f"{i}: {c}")
```

---

## 演示：用颜色列表画彩虹圆

```python
import turtle
t = turtle.Turtle()
t.speed(3)

colors = ["red", "blue", "green"]

for c in colors:
    t.color(c)       # 设置颜色
    t.circle(30)     # 画圆
    t.penup()
    t.forward(80)    # 移动位置
    t.pendown()
```

**彩虹7色扩展：**
```python
rainbow = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
```

---

## 实操练习

### 练习1：填空索引

```python
fruits = ["apple", "banana", "cherry", "durian"]
# ① 取出 "apple"     → fruits[____]
# ② 取出 "cherry"    → fruits[____]
# ③ 取出 "durian"    → fruits[____] 或 fruits[____]
```

### 练习2：补全遍历

```python
pets = ["cat", "dog", "fish"]
____ p ____ pets:
    ____(p)
```

### 练习3：画彩虹圆

```python
colors = ["red", "orange", "yellow", "green", "blue"]
# 用 for 循环遍历 colors，用 t.color(c) + t.circle(25) 画出5个圆
```

---

## 小测

1. `colors = ["red", "blue", "green", "yellow", "purple"]`
   - `colors[0]` = ______
   - `colors[-1]` = ______

2. 下面哪个索引会报错？
   - A. `colors[0]`   B. `colors[4]`   C. `colors[5]`

3. 补全代码：
   ```python
   nums = [10, 20, 30]
   for n ____ nums:
       print(____)
   ```

---

## 记忆钩子

> **"列表 = 一排贴编号盒子，编号从0开始"**
> **"方盒子用方括号 []"**
> **"正索引 0 1 2 3... 负索引 -1最后一个 -2倒数第二个..."**
