python-小练习(8)

from list_sort_practise import practical_travel_list
from list_slice import my_foods,friend_foods
# 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:”,再使用切片来打印列表
# 末尾的三个元素。
print("The first three items in the list are:",practical_travel_list[0:3])
print('-------------------------------------------------------------------')

print("Three items from the middle of the list are:",practical_travel_list[1:4])

print('-------------------------------------------------------------------')
print("The last three items in the list are:",practical_travel_list[2:])
print('-------------------------------------------------------------------')

# 4-11 你的比萨和我的比萨:在你为完成练习4-1而编写的程序中,创建比
# 萨列表的副本,并将其存储到变量friend_pizzas中,再完成如下任
# 务。
# 在原来的比萨列表中添加一种比萨。
# 在列表friend_pizzas中添加另一种比萨。
# 核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,
# 再使用一个for循环来打印第一个列表;打印消息“My friend's favorite
# pizzas are:”,再使用一个for循环来打印第二个列表。核实新增的比
# 萨被添加到了正确的列表中。
my_pizzas=['sudi','sipa','shuiguo']
friend_pizzas = my_pizzas[:]

my_pizzas.append('sala')
friend_pizzas.append('shuiguo')
print("My favorite pizzas are:")
print(my_pizzas)

print("My friend's favorite pizzas are:")
print(friend_pizzas)
print('-------------------------------------------------------------------')
# 4-12 使用多个循环:在本节中,为节省篇幅,程序foods.py的每个版本都
# 没有使用for循环来打印列表。请选择一个版本的foods.py,在其中编写
# 两个for循环,将各个食品列表都打印出来。
for  my_food in my_foods:
    print('我爱吃的食物是:',my_food)
for friend_food in friend_foods:
    print("朋友爱吃的食物是:",friend_food)

执行结果

The first three items in the list are: ['yading', 'xianggelila', 'weizhoudao']
-------------------------------------------------------------------
Three items from the middle of the list are: ['xianggelila', 'weizhoudao', 'sanya']
-------------------------------------------------------------------
The last three items in the list are: ['weizhoudao', 'sanya', 'dali']
-------------------------------------------------------------------
My favorite pizzas are:
['sudi', 'sipa', 'shuiguo', 'sala']
My friend's favorite pizzas are:
['sudi', 'sipa', 'shuiguo', 'shuiguo']
-------------------------------------------------------------------
我爱吃的食物是: pizza
我爱吃的食物是: falafel
我爱吃的食物是: carrot cake
我爱吃的食物是: cannoli
朋友爱吃的食物是: pizza
朋友爱吃的食物是: falafel
朋友爱吃的食物是: carrot cake
朋友爱吃的食物是: ice cream
哈哈哈,列表学完啦!

进程已结束,退出代码为 0
posted @ 2022-04-08 16:29  NiceTwocu  阅读(64)  评论(0编辑  收藏  举报