tests = ["aaa","bbb","ccc","ddd"]
for test in tests:
print(test)
运行结果:
aaa
bbb
ccc
ddd
在 for 循环中执行更多的操作
tests = ["aaa","bbb","ccc","ddd"]
for test in tests:
print(test + ",欢迎!")
运行结果:
aaa,欢迎!
bbb,欢迎!
ccc,欢迎!
ddd,欢迎!
在 for 循环结束后执行一些操作
tests = ["aaa","bbb","ccc","ddd"]
for test in tests:
print(test + ",欢迎!")
print("感谢你们的到来!")
运行结果:
aaa,欢迎!
bbb,欢迎!
ccc,欢迎!
ddd,欢迎!
感谢你们的到来!
4-1 比萨:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用 for 循环将每种比萨的名称都打印出来。
- 修改这个 for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。
- 在程序末尾添加一行代码,它不在 for 循环中,指出你有多喜欢比萨。输出应包 含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。
tests = ["aaa","bbb","ccc","ddd"]
for test in tests:
print("I like "+ test + " pizza!" )
print("I really love pizza!")
4-2 动物:想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中, 再使用 for 循环将每种动物的名称都打印出来。
- 修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
- 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子
tests = ["aaa","bbb","ccc","ddd"]
for test in tests:
print("I like "+ test + " pizza!" )
print("I really love pizza!")
创建数值列表
Python函数range()让你能够轻松地生成一系列的数字,函数range()让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止,因此输出 不包含第二个值
for test in range(1,5): print(test) 运行结果: 1 2 3 4
可使用函数list()将range()的结果直接转换为列表。如果将range()作为操作列表 list()的参数,输出将为一个数字列表。
tests = list(range(1,5)) print(tests) 运行结果: [1, 2, 3, 4]
使用函数range()时,还可指定步长。例如,下面的代码打印1~10内的偶数
tests = list(range(2,11,2)) print(tests) 运行结果: [2, 3, 4, 5, 6, 7, 8, 9, 10]
在Python中,两个星号(**)表示乘方运算。下面的代码演示了 如何将前10个整数的平方加入到一个列表中
tests = [] for test in list(range(1,11)): tests.append(test ** 2) print(tests) 运行结果: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
对数字列表执行简单的统计计算
tests = [1,2,3,4,5] print(min(tests)) print(max(tests)) print(sum(tests)) 运行结果: 1 5 15
列表解析
tests = [test ** 2 for test in range(1,11)] print(tests) 运行结果: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4-3 数到 20:使用一个 for 循环打印数字 1~20(含)。
for test in range(1,21): print(test)
4-4 一百万:创建一个列表,其中包含数字 1~1 000 000,再使用一个 for 循环将这 些数字打印出来(如果输出的时间太长,按 Ctrl + C 停止输出,或关闭输出窗口)。
tests = [test for test in range(1,1000001)] for test in tests: print(test)
4-5 计算 1~1 000 000 的总和:创建一个列表,其中包含数字 1~1 000 000,再使用 min()和 max()核实该列表确实是从 1 开始,到 1 000 000 结束的。另外,对这个列表调 用函数 sum(),看看 Python 将一百万个数字相加需要多长时间。
tests = [test for test in range(1,1000001)] print(min(tests)) print(max(tests)) print(sum(tests))
4-6 奇数:通过给函数 range()指定第三个参数来创建一个列表,其中包含 1~20 的 奇数;再使用一个 for 循环将这些数字都打印出来。
tests = [test for test in range(1,21,2)] for test in tests: print(test)
4-7 3 的倍数:创建一个列表,其中包含 3~30 内能被 3 整除的数字;再使用一个 for 循环将这个列表中的数字都打印出来。
tests = [test for test in range(3,31,3)] for test in tests: print(test)
4-8 立方:将同一个数字乘三次称为立方。例如,在 Python 中,2 的立方用 2**3 表示。请创建一个列表,其中包含前 10 个整数(即 1~10)的立方,再使用一个 for 循 环将这些立方数都打印出来。
tests = [] for test in range(1,11): tests.append(test ** 3) for test in tests: print(test)
4-9 立方解析:使用列表解析生成一个列表,其中包含前 10 个整数的立方。
tests = [test**3 for test in range(1,11)] for test in tests: print(test)使用列表的一部分
切片,指定要使用的第一个元素和最后一个元素的索引。与函数range()一样,Python 在到达你指定的第二个索引前面的元素后停止。
tests = ["aaa","bbb","ccc","ddd"] print(tests[1:3]) 运行结果: ['bbb', 'ccc']
遍历切片,可在for循环中使用切片
tests = ["aaa","bbb","ccc","ddd"] for test in tests[:3]: print(test) 运行结果: aaa bbb ccc
复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:])。 这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表
tests = ["aaa","bbb","ccc","ddd"] tests2 = tests[:] print(tests2) 运行结果: ['aaa', 'bbb', 'ccc', 'ddd']
4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
- 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个 元素。
- 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
- 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三 个元素。
tests = ["aaa","bbb","ccc","ddd","eee"]
print("The first three items in the list are:" + str(tests[:3]))
print("Three items from the middle of the list are:" + str(tests[1:4]))
print("The last three items in the list are:" + str(tests[2:]))
4-11 你的比萨和我的比萨:在你为完成练习 4-1 而编写的程序中,创建比萨列表的副本,并将其存储到变量 friend_pizzas 中,再完成如下任务。
- 在原来的比萨列表中添加一种比萨。
- 在列表 friend_pizzas 中添加另一种比萨。
- 核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一 个 for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使 用一个 for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。
my_pizzas = ["aaa","bbb","ccc","ddd","eee"]
friend_pizzas = my_pizzas[:]
my_pizzas.append("fff")
friend_pizzas.append("ggg")
print("My favorite pizzas are:")
for my_pizza in my_pizzas:
print(my_pizza)
print("My friend’s favorite pizzas are:")
for friend_pizza in friend_pizzas:
print(friend_pizza)
元组
Python将不能修改的值称为不可变的,而不可变的列表被称为元组。元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来 访问其元素,就像访问列表元素一样。
tests = ("aaa","bbb","ccc","ddd")
print(tests[0])
print(tests[2])
运行结果:
aaa
ccc
遍历元组中所有的值,像列表一样,也可以使用for循环来遍历元组中的所有值:
tests = ("aaa","bbb","ccc","ddd")
for test in tests:
print(test)
运行结果:
aaa
bbb
ccc
ddd
修改元组变量,虽然不能修改元组的元素,但可以给存储元组的变量赋值。
tests = ("aaa","bbb","ccc","ddd")
print(tests)
tests = ("111","222","333","444")
print(tests)
运行结果:
('aaa', 'bbb', 'ccc', 'ddd')
('111', '222', '333', '444')
4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
- 使用一个 for 循环将该餐馆提供的五种食品都打印出来。
- 尝试修改其中的一个元素,核实 Python 确实会拒绝你这样做。
- 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块: 给元组变量赋值,并使用一个 for 循环将新元组的每个元素都打印出来
tests = ("aaa","bbb","ccc","ddd","eee")
print("菜单:")
for test in tests:
print(test)
print("新菜单:")
tests = ("aaa","bbb","ccc","fff","ggg")
for test in tests:
print(test)



