Python编程:从⼊门到实践——练习题答案(第四章)
第4章操作列表
4-1 ⽐萨:想出⾄少三种你喜欢的⽐萨,将其名称存储在⼀个列表中,再使⽤ for循环将每种⽐萨的名称都打印出来。
pizzas =['fruit pizza','seafood pizza','cheese pizza']
for pizza in pizzas:
print(pizza)
#修改这个 for 循环,使其打印包含⽐萨名称的句⼦,⽽不仅仅是⽐萨的名称。对于每种⽐萨,
#都显⽰⼀⾏输出,如“I like pepperoni pizza”。
for pizza in pizzas:
print('I like '+ pizza +'.')
#在程序末尾添加⼀⾏代码,它不在 for 循环中,指出你有多喜欢⽐萨。
#输出应包含针对每种⽐萨的消息,还有⼀个总结性句⼦,如“I really love pizza!”。
print('\nI really like pizza!')
运⾏结果:
fruit pizza
seafood pizza
cheese pizza
I like fruit pizza.
I like seafood pizza.
I like cheese pizza.
I really like pizza!
4-2 动物:想出⾄少三种有共同特征的动物,将这些动物的名称存储在⼀个列表中,再使⽤ for 循环将每种动物的名称都打印出来。
pets =['dog','cat','bird']
for pet in pets:
print(pet)
#修改这个程序,使其针对每种动物都打印⼀个句⼦,如“A dog would make a greatpet”。
for pet in pets:
print('A '+ pet +' would make a great pet.')
#在程序末尾添加⼀⾏代码,指出这些动物的共同之处,
#如打印诸如“Any of theseanimals would make a great pet!”这样的句⼦。
print('Any of these animals would make a great pet!')
运⾏结果:
dog
cat
bird
ospf毕业论文
A dog would make a great pet.
A cat would make a great pet.
A bird would make a great pet.
Any of these animals would make a great pet!
4-3 数到 20:使⽤⼀个 for 循环打印数字 1~20(含)。
for number in range(1,21):
print(number)
运⾏结果:
1
2
省略..
20
4-4 ⼀百万:创建⼀个列表,其中包含数字 1~1 000 000,再使⽤⼀个 for 循环将这些数字打印出来(如果输出的时间太长,按 Ctrl + C 停⽌输出,或关闭输出窗⼝)。
numbers =list(range(1,1000001))
for number in numbers:
print(number)
运⾏结果:
1
spring netty2
省略..
1000000
4-5 计算 1~1 000 000 的总和:创建⼀个列表,其中包含数字 1~1 000 000,再使⽤min()和 max()核实该列表确实是从 1 开始,到1 000 000 结束的。另外,对这个列表调⽤函数 sum(),看看 Python 将⼀百万个数字相加需要多长时间。
numbers =list(range(1,1000001))
print(min(numbers))
print(max(numbers))
sum(numbers)
运⾏结果:
1
1000000
4-6 奇数:通过给函数 range()指定第三个参数来创建⼀个列表,其中包含 1~20 的奇数;再使⽤⼀个 f
or 循环将这些数字都打印出来。
numbers =list(range(1,21,2))
for number in numbers:
print(number)
运⾏结果:
1
3
5
省略..
19
4-7 3 的倍数:创建⼀个列表,其中包含 3~30 内能被 3 整除的数字;再使⽤⼀个 for循环将这个列表中的数字都打印出来。
numbers =list(range(3,31,3))
for number in numbers:
print(number)
运⾏结果:
3
6
9
省略..
30
4-8 ⽴⽅:将同⼀个数字乘三次称为⽴⽅。例如,在 Python 中, 2 的⽴⽅⽤ 2 * * 3表⽰。请创建⼀个列表,其中包含前 10 个整数(即1~10)的⽴⽅,再使⽤⼀个 for 循环将这些⽴⽅数都打印出来。
numbers =[]
for number in range(1,11):
numbers.append(number **3)
for number in numbers:
print(number)
运⾏结果:
1
8
27
省略..
1000
4-9 ⽴⽅解析:使⽤列表解析⽣成⼀个列表,其中包含前 10 个整数的⽴⽅。
numbers =[number**3for 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:”,再使⽤切⽚来打印列表的前三个元素。
players =['charles','martina','michael','florence','eli']
print('The first three items in the list are:')
print(players[0:3])
#打印消息“Three items from the middle of the list are:”,
#再使⽤切⽚来打印列表中间的三个元素。
print('Three items from the middle of the list are:')
print(players[1:4])
#打印消息“The last three items in the list are:”,再使⽤切⽚来打印列表末尾的三个元素。
print('The last three items in the list are:')
print(players[-3:])
运⾏结果:
电子表格里的通配符是什么The first three items in the list are:
['charles', 'martina', 'michael']
Three items from the middle of the list are:
['martina', 'michael', 'florence']
The last three items in the list are:
['michael', 'florence', 'eli']
python基础代码练习
4-11 你的⽐萨和我的⽐萨:在你为完成练习 4-1 ⽽编写的程序中,创建⽐萨列表的
副本,并将其存储到变量 friend_pizzas 中,再完成如下任务。
pizzas =['fruit pizza','seafood pizza','cheese pizza']
friend_pizzas = pizzas[:]
#在原来的pizza列表中添加⼀种披萨。
pizzas.append('apple pizza')
#在friend_pizzas列表中添加零⼀种披萨。
xlsx是什么格式的文件friend_pizzas.append('banana pizza')
#核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,
#再使⽤⼀个 for 循环来打印第⼀个列表;打印消息“My friend’s favorite pizzas are:”,
#再使⽤⼀个 for 循环来打印第⼆个列表。核实新增的⽐萨被添加到了正确的列表中。
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:
fruit pizza
seafood pizza
cheese pizza
apple pizza
My friend's favorite pizzas are:
fruit pizza
seafood pizza
cheese pizza
banana pizza
4-12 使⽤多个循环:在本节中,为节省篇幅,程序 的每个版本都没有使⽤
for 循环来打印列表。请选择⼀个版本的 ,在其中编写两个 for 循环,将各个
⾷品列表都打印出来。
stream客服my_foods =['pizza','falafel','carrot cake']
frends_foods = my_foods[:]
print('My favorite foods are :')
for my_food in my_foods:
print(my_food)
print("\nMy friend's favorite foods are :")
for frends_food in frends_foods:
print(frends_food)
运⾏结果:
My favorite foods are :
pizza
falafel
carrot cake
My friend's favorite foods are :
pizza
falafel
carrot cake
4-13 ⾃助餐:有⼀家⾃助式餐馆,只提供五种简单的⾷品。请想出五种简单的⾷品,并将其存储在⼀个元组中。
foods =('vegetable','fruit','beef','chicken','eggs')
#使⽤for循环将五种⾷物打印出来。
for food in foods:
print(food)
#尝试修改其中的⼀个元素,核实 Python 确实会拒绝你这样做。
'''
foods[0] = 'abc'
'''
#餐馆调整了菜单,替换了它提供的其中两种⾷品。请编写⼀个这样的代码块:#给元组变量赋值,并使⽤⼀个 for 循环将新元组的每个元素都打印出来。foods =('vegetable','fruit','beef','fish','duck')
for food in foods:
print(food)
运⾏结果:
vegetable
fruit
beef
chicken
eggs
vegetable
fruit
beef
fish
duck

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。