1. 遍历整个列表,使用for循环。
magicians = ['yu qian','zhou jie lun','yin shi','li xiang lan','xue zhi qian']
for magician in magicians:
print(magician.title())
print(magician.upper())
Yu Qian
YU QIAN
Zhou Jie Lun
ZHOU JIE LUN
Yin Shi
YIN SHI
Li Xiang Lan
LI XIANG LAN
Xue Zhi Qian
XUE ZHI QIAN
2. 使用f''进行字符串变量赋值
fav_magician = f'My favourite magician is {magicians[-2]}.'
print(fav_magician)
My favourite magician is Li Xiang Lan.
3. 修改列表中元素的值,列表索引赋值。
print(magicians)
magicians[0] = 'mag'
print(magicians)
['mag', 'zhou jie lun', 'yin shi', 'li xiang lan', 'xue zhi qian']
4. 在列表中插入元素 insert(索引X,元素XX),在索引X前插入元素XX。
magicians.insert(0,'lu xun')
print(magicians)
['lu xun', 'mag', 'zhou jie lun', 'yin shi', 'li xiang lan', 'xue zhi qian', 'yu wei wen']
5. 删除列表中的元素,del list[索引X],删除列表中索引X对应的元素,注意语法' 方括号'。
del magicians[0]
print(magicians)
['mag', 'zhou jie lun', 'yin shi', 'li xiang lan', 'xue zhi qian', 'yu wei wen']
6. 使用pop方法删除元素,list.pop(索引),不输入索引默认删除列表最后一个元素,返回值为删除的元素。
data_list = [1,2,3,4,5,6,7,8,9,0]
data_pop1 = data_list.pop()
print(data_list,data_pop1,sep='\n')
data_pop2 = data_list.pop(1)
print(data_list,data_pop2,sep='\n')
[1, 2, 3, 4, 5, 6, 7, 8, 9]
0
[1, 3, 4, 5, 6, 7, 8, 9]
2
7. 使用remove()根据值删除元素,注:只能删除第一个指定值。
data_list2 = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6]
data_list2.remove(1)
print(data_list2)
[1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
8. for循环练习,结合元素操作和f''语句,打印答语。
magicians_2 = ['alice','advid','carolina']
for magician in magicians_2:
print(f'{magician.title()},what a great trick!')
Alice,what a great trick!
Advid,what a great trick!
Carolina,what a great trick!
9. 创建数值列表 range ()方法,“含左不含右”。
for number in range(1,10):
print(number)
1
2
3
4
5
6
7
8
9
10. 将range()结果直接保存为列表。
numbers = []
for number in range(1,13):
numbers.append(number)
print(len(numbers),numbers,sep='\n')
12
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
11. 使用list()方法,将range()结果直接保存为列表
numbers_1 = list(range(1,13))
print(numbers_1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
12. #range()中加入步长参数,range(起始值,终止值,步长)
numbers_2 = list(range(0,100,5))
print(numbers_2)
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
13. 对数字列表进行简单的统计计算,最小值min(),最大值max(),求和sum()。
print(min(numbers_2),max(numbers_2),sum(numbers_2))
0 95 950
14. 列表解析,将for循环和创建新元素的代码合并成一行,并自动附加新元素
squares = [i**2 for i in range(1,10)] #首先指定列表名称,然后指定一个方括号[],定义一个包含变量的表达式,最后通过for循环给变量赋值
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
15. 列表切片,list[起始索引 : 结束索引]
print(numbers_2[3:6])
[15, 20, 25]
16. 负数索引,表示从列表尾部倒数,同样含左不含右。
print(numbers_2[-4:])
[80, 85, 90, 95]
17. 遍历切片
for number in numbers_2[-6:-1]:
print(number)
70
75
80
85
90
18. 列表解析:编写一行代码即可生成目标列表。
1.指定一个描述性列表名;
2.指定一个方括号并定义一个表达式,用于生成要存储到列表中的值。
以下代码,表达式为 value^2, 计算平方值,for循环用于给表达式提供值。
squares_1 = [value**2 for value in range(1,30)]
print(squares_1)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841]
19. 复制列表, list1 = list[:]将list的副本赋值给list1
numbers_3 = list(range(1,20))
print(numbers_3)
numbers_4 = numbers_3[:]
numbers_3.append(55)
numbers_4.append(66)
print(numbers_3,numbers_4,sep='\n')
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 55]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 66]
20. list1 = list,不能创建新的列表,两个列表(list,list1)将指向同一个列表,每个列表变化时,二者同时变化,一直相等。
numbers_5 = list(range(2,31,2))
print(numbers_5)
numbers_6 = numbers_5
numbers_6.append(44)
print(numbers_5,numbers_6,sep='\n')
numbers_5.append(77)
numbers_6.append(88)
print(numbers_5,numbers_6,sep='\n')
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 44]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 44]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 44, 77, 88]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 44, 77, 88]