遍历整个列表
students = ['zhangsan','lisi','wangwu','zhaoliu'] for student in students: print(student) zhangsan lisi wangwu zhaoliu [Finished in 0.2s]
由于两条print 语句都缩进了,因此它们都将针对列表中的每位学生执行一次。第二条print 语句中的换行符"\n" (见❶)在每次迭代结束后都插入一个空行,从而整洁地 将针对各位学生的消息编组:
students = ['zhangsan','lisi','wangwu','zhaoliu'] for student in students: print(student.title() + ", that was a great trick!") print("I can't wait to see your next trick ," + student.title() + ".\n") Zhangsan, that was a great trick! I can't wait to see your next trick ,Zhangsan. Lisi, that was a great trick! I can't wait to see your next trick ,Lisi. Wangwu, that was a great trick! I can't wait to see your next trick ,Wangwu. Zhaoliu, that was a great trick! I can't wait to see your next trick ,Zhaoliu. [Finished in 0.3s]
在for 循环后面,没有缩进的代码都只执行一次,而不会重复执行。下面来打印一条向全学生感谢的消息,感谢他们的精彩表演。想要在打印给各位魔学生的消息后面打印 一条给全体学生的致谢消息,需要将相应的代码放在for 循环后面,且不缩进:
students = ['zhangsan','lisi','wangwu','zhaoliu'] for student in students: print(student.title() + ", that was a great trick!") print("I can't wait to see your next trick ," + student.title() + ".\n") print("thank you ,everyone ,that was a great magic show!") Zhangsan, that was a great trick! I can't wait to see your next trick ,Zhangsan. Lisi, that was a great trick! I can't wait to see your next trick ,Lisi. Wangwu, that was a great trick! I can't wait to see your next trick ,Wangwu. Zhaoliu, that was a great trick! I can't wait to see your next trick ,Zhaoliu. thank you ,everyone ,that was a great magic show! [Finished in 0.3s]
创建数值列表
使用函数range()
for value in range(1,6): print(value) 1 2 3 4 5 [Finished in 0.1s]
使用range() 创建数字列表
numbers = list(range(1,6)) print(numbers) [1, 2, 3, 4, 5] [Finished in 0.1s]
指定步长(不断加2和加1)
numbers = list(range(1,11,2)) print(numbers) numbers = list(range(1,11,1)) print(numbers) [1, 3, 5, 7, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [Finished in 0.1s]
创建一个列表,其中包含前10个整数(即1~10)的平方,在Python中,两个星号(** )表示乘方运算。
squares = [] for value in range(1,11): square = value**2 squares.append(square) print(squares) [1] [1, 4] [1, 4, 9] [1, 4, 9, 16] [1, 4, 9, 16, 25] [1, 4, 9, 16, 25, 36] [1, 4, 9, 16, 25, 36, 49] [1, 4, 9, 16, 25, 36, 49, 64] [1, 4, 9, 16, 25, 36, 49, 64, 81] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [Finished in 0.3s]
为让这些代码更简洁,可不使用临时变量square ,而直接将每个计算得到的值附加到列表末尾:
squares = [] for value in range(1,11): squares.append(value**2) print(squares) [1] [1, 4] [1, 4, 9] [1, 4, 9, 16] [1, 4, 9, 16, 25] [1, 4, 9, 16, 25, 36] [1, 4, 9, 16, 25, 36, 49] [1, 4, 9, 16, 25, 36, 49, 64] [1, 4, 9, 16, 25, 36, 49, 64, 81] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [Finished in 0.3s]
对数字列表执行简单的统计计算
>>> digits = [1,2,3,4,5,6,7,8,9,0] >>> min(digits) 0 >>> max(digits) 9 >>> sum(digits) 45
列表解析
列表解析 将for 循环和创建新元素的代码合并成一行,并自动附加新元素
squares = [value**2 for value in range(1,11)] print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [Finished in 0.3s]
使用列表的一部分
切片
players = ['charles','martina','michael','florence','eli'] print(players) print(players[0:4]) print(players[1:4])
['charles', 'martina', 'michael', 'florence', 'eli'] ['charles', 'martina', 'michael', 'florence']
['martina', 'michael', 'florence'] [Finished in 0.2s]
players = ['charles','martina','michael','florence','eli'] print(players) print(players[:4]) print(players[2:]) print(players[-3:]) ['charles', 'martina', 'michael', 'florence', 'eli'] ['charles', 'martina', 'michael', 'florence'] ['michael', 'florence', 'eli'] ['michael', 'florence', 'eli'] [Finished in 0.3s]
遍历切片
players = ['charles','martina','michael','florence','eli'] print("here are the first three playerson my team:") for player in players[:3]: print(player.title()) here are the first three playerson my team: Charles Martina Michael [Finished in 0.2s]
复制列表