Python第四章课后练习_m0
练习4-1:比萨 想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称打印出来。修改这个for 循环,使其打印包含比萨名称的句子,而不 仅仅是比萨的名称。对于每种比萨,都显示一行输出,下面是一个例子。
I like pepperoni pizza.
在程序末尾添加一行代码,它不在for 循环中,指出你有 多喜欢比萨。输出应包含针对每种比萨的消息,还有一个 总结性句子,下面是一个例子。
I really love pizza!
- pizzas = ['a','b','c']
- for pizza in pizzas:
- print(pizza)
- #修改
- if pizza == 'a':
- print(f"I don't like {pizza} pizza")
- else:
- print(f"I like {pizza} pizza")
- print(f"I really don't like {pizzas[0]} pizza!
"
- f"I really like {pizzas[1]} and {pizzas[2]} pizzas!")
a
I don't like a pizza
b
I like b pizza
c
I like c pizza
I really don't like a pizza!
I really like b and c pizzas!
练习4-2:动物 想出至少三种有共同特征的动物,将其名称 存储在一个列表中,再使用for 循环将每种动物的名称打印出来。修改这个程序,使其针对每种动物都打印一个句子,下面是一个例子。
A dog would make a great pet.
在程序末尾添加一行代码,指出这些动物的共同之处,如打印下面这样的句子。
Any of these animals would make a great pet!
- animals = ['parrot','pigeon','bird']
- for animal in animals:
- print(animal)
- #修改
- print(f"修改后:A {animal} would make a great pet")
- print(f"Any of these animal would make a great pet!")
parrot
修改后:A parrot would make a great pet
pigeon
修改后:A pigeon would make a great pet
bird
修改后:A bird would make a great pet
Any of these animal would make a great pet!
练习4-3:数到20 使用一个for 循环打印数1~20(含)。
- for number in range(1,21):
- print(number)
练习4-4:一百万 创建一个包含数1~1 000 000的列表,再使 用一个for 循环将这些数打印出来。(如果输出的时间太长, 按Ctrl + C停止输出或关闭输出窗口。)
- numbers = list(range(1,1000_001))
- for number in numbers:
- print(number)
练习4-5:一百万求和 创建一个包含数1~1 000 000的列 表,再使用min() 和max() 核实该列表确实是从1开始、到1 000 000结束的。另外,对这个列表调用函数sum() ,看看 Python将一百万个数相加需要多长时间。
- import time
- numbers = [number*1 for number in range(1,1000_001)]
- print(f"最小值:{min(numbers)}
最大值:{max(numbers)}")
- start = time.time()
- print(f"求和:{sum(numbers)}")
- end = time.time()
- print(f"Running time:{end-start}s")
最小值:1
最大值:1000000
求和:500000500000
Running time:0.0359039306640625s
练习4-6:奇数 通过给函数range() 指定第三个参数来创建 一个列表,其中包含1~20的奇数,再使用一个for 循环将这 些数打印出来。
- numbers = list(range(1,21,2))
- for number in numbers:
- print(number)
练习4-7:3的倍数 创建一个列表,其中包含3~30能被3整除 的数,再使用一个for 循环将这个列表中的数打印出来。
- numbers = []
- for number in range(3,31):
- if number%%3 == 0 :
- numbers.append(number)
- if number%%3 != 0 :
- print(f"{number}这个数不能被3整除")
- print(numbers)
- for number in numbers:
- print(number)
4这个数不能被3整除
5这个数不能被3整除
7这个数不能被3整除
8这个数不能被3整除
10这个数不能被3整除
11这个数不能被3整除
13这个数不能被3整除
14这个数不能被3整除
16这个数不能被3整除
17这个数不能被3整除
19这个数不能被3整除
20这个数不能被3整除
22这个数不能被3整除
23这个数不能被3整除
25这个数不能被3整除
26这个数不能被3整除
28这个数不能被3整除
29这个数不能被3整除
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
3
6
9
12
15
18
21
24
27
30
练习4-8:立方 将同一个数乘三次称为立方 。例如,在 Python中,2的立方用2**3 表示。请创建一个列表,其中包含 前10个整数(1~10)的立方,再使用一个for 循环将这些立 方数打印出来。
- numbers = []
- for number in range(1,11):
- number = number **3
- numbers.append(number)
- print(len(numbers))
- print(numbers)
10
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
练习4-9:立方解析 使用列表解析生成一个列表,其中包含前10个整数的立方。
- numbers = [number**3 for number in range(1,11)]
- print(numbers)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
练习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:”,再使用切片来打印列表的末尾三个元素。
- names = ['zhang','li','ai','bai','cai']
- print("The first three items in the list are:")
- print(names[:3])
- print("Three items from the middle of the list are:")
- print(names[1:-1])
- print("The last three items in the list are:")
- print(names[-3:])
The first three items in the list are:
['zhang', 'li', 'ai']
Three items from the middle of the list are:
['li', 'ai', 'bai']
The last three items in the list are:
['ai', 'bai', 'cai']
练习4-11:你的比萨,我的比萨 在你为完成练习4-1而编写 的程序中,创建比萨列表的副本,并将其赋给变量 friend_pizzas ,再完成如下任务。
在原来的比萨列表中添加一种比萨。 在列表friend_pizzas 中添加另一种比萨。 核实有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一个for 循环来打印第一个列表;打 印消息“My friend's favorite pizzas are:”,再使用一个for 循环来打印第二个列表。核实新增的比萨被添加到了正确 的列表中。
- pizzas = ['a','b','c']
- friend_pizzas = pizzas[:2]
- pizzas.append('d')
- friend_pizzas.append('e')
- print("My favorite pizzas are:")
- for pizza in pizzas:
- print(pizza)
- print("My friend's favorite pizzas are:")
- for friend_pizza in friend_pizzas:
- print(friend_pizza)
My favorite pizzas are:
a
b
c
d
My friend's favorite pizzas are:
a
b
e
练习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 my_food in my_foods:
- print(my_food)
- print(my_foods)
- print("
My friend's favorite foods are:")
- for friend_food in friend_foods:
- print(friend_food)
- print(friend_foods)
My favorite foods are:
pizza
falafel
carrot cake
cannoli
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
pizza
falafel
carrot cake
ice cream
['pizza', 'falafel', 'carrot cake', 'ice cream']
练习4-13:自助餐 有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
使用一个for 循环将该餐馆提供的五种食品都打印出来。 尝试修改其中的一个元素,核实Python确实会拒绝你这样 做。 餐馆调整了菜单,替换了它提供的其中两种食品。请编写 一个这样的代码块:给元组变量赋值,并使用一个for 循 环将新元组的每个元素都打印出来的。
- foods = ('cabbage','carrots','eggs','mushrooms','fish')
- for food in foods:
- print(food)
- #foods[0] = 'meat',已确认出错
- print(f"
换菜:")
- foods = ('potatoes','beans','eggs','mushrooms','fish')
- for food in foods:
- print(food)
cabbage
carrots
eggs
mushrooms
fish
换菜:
potatoes
beans
eggs
mushrooms
fish
练习4-14:PEP 8 请访问Python网站并搜索“PEP 8 — Style Guide for Python Code”,阅读PEP 8格式设置指南。当前,这些指南适用的情况不多,但可以大致浏览一下。
练习4-15:代码审核 从本章编写的程序中选择三个,根据 PEP 8指南对它们进行修改。每级缩进都使用四个空格。对你使用的文本编辑器进行设 置,使其在你按Tab键时插入四个空格。如果你还没有这 样做,现在就去做吧(有关如何设置,请参阅附录B)。每行都不要超过80字符。对你使用的编辑器进行设置,使 其在第80个字符处显示一条垂直参考线。不要在程序文件中过多使用空行。