python编程:从入门到实践习题4-10~4-11

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:”,再使用切片来打印列表末尾的三 个元素。 

list=[value**3 for value in range(1,11)]

print(list)

for num in list:

print(num)

print("The first three items in the list are:")

print(list[:3])

print("Three items from the middle of the list are:")

print(list[1:4])

print("The last three items in the list are:")

print(list[-3:])

4-11 你的比萨和我的比萨:

在你为完成练习 4-1而编写的程序中,创建比萨列表的 副本,并将其存储到变量 friend_pizzas 中,再完成如下任务。 

 在原来的比萨列表中添加一种比萨。 

 在列表 friend_pizzas 中添加另一种比萨。 

 核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一 个 for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使 用一个 for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。

favorite_foods=['disanxian','chaomian','danbaobao']

for food in favorite_foods:

print("I like "+food+"\n")

print("I love "+favorite_foods[0]+","+favorite_foods[1]+","+favorite_foods[2]+",I really love eating foods!")

friend_foods=favorite_foods[:]

favorite_foods.append("guobaorou")

friend_foods.append("kaolengmian")

print("My favorite foods are:")

for food in favorite_foods:

print(food)

print("My friend's favorite foods are:")

for food in friend_foods:

print(food)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容