练习4-1: 比萨 想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用 for 循环将每种比萨的名称打印出来。
I really love pizza!
favorite_pizzas = ['pepperoni', 'hawaiian', 'veggie']
for favorite_pizza in favorite_pizzas:
print(favorite_pizza)
print(f"I like {favorite_pizza} pizza.")
print("\nI really love pizza!")
输出:
pepperoni
I like pepperoni pizza.
hawaiian
I like hawaiian pizza.
veggie
I like veggie pizza.
I really love pizza!
练习4-2: 动物 想出至少三种有共同特征的动物,将其名称存储在一个列表中,再使用 for 循环将每种动物的名称打印出来。
Any of these animals would make a great pet!
animals = ['rabbit', 'monkey', 'kangaroo']
for animal in animals:
print(animal)
print(f"A {animal} would make a great pet.")
print(f"\nAny of these animals would make a great pet!")
输出:
rabbit
A rabbit would make a great pet.
monkey
A monkey would make a great pet.
kangaroo
A kangaroo would make a great pet.
Any of these animals would make a great pet!
练习4-3: 数到20 使用一个for 循环打印数1~20(含)。
numbers = list(range(1, 21))
for number in numbers:
print(number)
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
练习4-4: 一百万 创建一个包含数1~1 000 000的列表,再使用一个 for 循环将这些数打印出来。(如果输出的时间太长,按Ctrl + C停止输出或关闭输出窗口。)
millions = list(range(1, 1_000_001))
for million in millions:
print(million)
输出:略
练习4-5: 一百万求和 创建一个包含数1~1 000 000的列表,再使用min() 和max() 核实该列表确实是从1开始、到1000 000结束的。另外,对这个列表调用函数sum() ,看看Python将一百万个数相加需要多长时间。
millions = list(range(1, 1_000_001))
print(min(millions))
print(max(millions))
print(sum(millions))
输出:
1
1000000
500000500000
练习4-6: 奇数 通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数,再使用一个for 循环将这些数打印出来。
odds = list(range(1, 21, 2))
for odd in odds:
print(odd)
输出:
1
3
5
7
9
11
13
15
17
19
练习4-7: 3的倍数 创建一个列表,其中包含3~30能被3整除的数,再使用一个for 循环将这个列表中的数打印出来。
multiples_of_three = list(range(3, 31, 3))
for multiple_of_three in multiples_of_three:
print(multiple_of_three)
输出:
3
6
9
12
15
18
21
24
27
30
练习4-8: 立方 将同一个数乘三次称为立方 。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(1~10)的立方,再使用一个for 循环将这些立方数打印出来。
cubes = []
for cube in range(1, 11):
cubes.append(cube ** 3)
for cube in cubes:
print(cube)
输出:
1
8
27
64
125
216
343
512
729
1000
练习4-9: 立方解析 使用列表解析生成一个列表,其中包含前10个整数的立方。
cube_anlyses = [value**3 for value in range(1, 11)]
for cube_anlysis in cube_anlyses:
print(cube_anlysis)
输出:
1
8
27
64
125
216
343
512
729
1000
练习4-10: 切片 选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
foods = ['pizza', 'falafel', 'carrot cake', 'chocolates', 'ice cream']
print("The first three items in the list are:")
for food in foods[:3]:
print(food)
print("Three items from the middle of the list are:")
for food in foods[1:4]:
print(food)
print("The last three items in the list are:")
for food in foods[-3:]:
print(food)
输出:
The first three items in the list are:
pizza
falafel
carrot cake
Three items from the middle of the list are:
falafel
carrot cake
chocolates
The last three items in the list are:
carrot cake
chocolates
ice cream
练习4-11: 你的比萨,我的比萨 在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其赋给变量friend_pizzas ,再完成如下任务。
favorite_pizzas = ['pepperoni', 'hawaiian', 'veggie']
friend_pizzas = favorite_pizzas[:]
favorite_pizzas.append('cannoli')
friend_pizzas.append('ice cream')
print("My favorite pizzas are:")
for pizza in favorite_pizzas:
print(pizza)
print("My friend's favorite pizzas are:")
for pizza in friend_pizzas:
print(pizza)
输出:
My favorite pizzas are:
pepperoni
hawaiian
veggie
cannoli
My friend's favorite pizzas are:
pepperoni
hawaiian
veggie
ice cream
练习4-12: 使用多个循环 在本节中,为节省篇幅,程序foods.py的每个版本都没有使用 for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个食品列表打印出来。
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
for food in my_foods:
print(food)
print("\nMy friend's favorite foods are:")
for food in friend_foods:
print(food)
输出:
My favorite foods are:
pizza
falafel
carrot cake
cannoli
My friend's favorite foods are:
pizza
falafel
carrot cake
ice cream
练习4-13: 自助餐 有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
buffets = ('rockfish sandwich', 'halibut nuggets', 'smoked salmon chowder',
'salmon burger', 'crab cakes')
print("You can choose from the following buffets:")
for buffet in buffets:
print(f"-{buffet}")
# buffets[0] = 'hearty steak'
print("\nOur buffets has been updated.")
print("You can now choose from the following buffets:")
buffets = ('rockfish sandwich', 'halibut nuggets', 'smoked salmon chowder',
'black cod tips', 'king crab legs')
for buffet in buffets:
print(f"-{buffet}")
输出:
You can choose from the following buffets:
-rockfish sandwich
-halibut nuggets
-smoked salmon chowder
-salmon burger
-crab cakes
Our buffets has been updated.
You can now choose from the following buffets:
-rockfish sandwich
-halibut nuggets
-smoked salmon chowder
-black cod tips
-king crab legs
练习4-14: PEP 8 请访问Python网站并搜索“PEP 8 — Style Guide for Python Code”,阅读PEP 8格式设置指南。当前,这些指南适用的情况不多,但可以大致浏览一下。
练习4-15: 代码审核 从本章编写的程序中选择三个,根据PEP 8指南对它们进行修改。