# 1.列表类型基础

## 1.1创建数字列表

numbers = list(range(1,4))print(numbers)
[1, 2, 3]

num2 = list(range(2,11,2))print(num2)
[2, 4, 6, 8, 10]

## 1.2访问列表中的值

list1 = ["小明","小红",1,2]list2 = [1,2,3,4,5,6,7,8]print("list1:",list1[0])print("list2:",list2[1:5])
list1: 小明list2: [2, 3, 4, 5]

list = [1,2,3,4,5,6,7,8]print(list[-1])
8

# 2.列表的基本操作

## 2.1更新列表元素

name = ["小明","小红","小军"]print(name)name[0] = "小华"print(name)
['小明', '小红', '小军']['小华', '小红', '小军']

## 2.2插入新的元素

name = ["小明","小红","小军"]print(name)name.insert(0,"小华")print(name)
['小明', '小红', '小军']['小华', '小明', '小红', '小军']

name = ["小明","小红","小军"]print(name)name.append("小华")print(name)
['小明', '小红', '小军']['小明', '小红', '小军', '小华']

## 2.3在列表中删除元素

### 使用del语句删除

name = ["小明","小红","小军","小华"]print(name)del name[0]print(name)
['小明', '小红', '小军', '小华']['小红', '小军', '小华']

### 使用pop()删除元素

name = ["小明","小红","小军","小华"]print(name)name.pop(3)print(name)
['小明', '小红', '小军', '小华']['小明', '小红', '小军']

### 根据元素值删除元素

name = ["小明","小红","小军","小华"]print(name)name.remove("小华")print(name)
['小明', '小红', '小军', '小华']['小明', '小红', '小军'

• 0
点赞
• 0
评论
• 0
收藏
• 扫一扫，分享海报

03-11 6070
04-26 69
12-07 4418
07-12 41
08-04 178
08-02 47
11-25 245
07-09 1万+
07-31 3514
05-06 554