高级编程技术作业(第3,4章)

本篇博客探讨了Python编程中与列表相关的操作,包括遍历列表、修改列表内容、排序和切片等。通过一系列编程练习,如嘉宾名单管理、旅行目的地排序以及比萨种类展示,深入理解for循环、sorted()、reverse()和sort()等函数的用法。此外,还介绍了如何创建列表的副本并进行独立修改,展示了对比萨口味的个人喜好差异。
摘要由CSDN通过智能技术生成

第三章


3-1 姓名 将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。

names=['Alice','Zayn','John']for name in names:    print(name)

输出为:

John
Alice
Zayn

3-4 嘉宾名单 如果你可以邀请任何人一起共进晚餐,你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐

names=['Alice','Zayn','John','Chen']
print("I wanna invite " +names[0]+', '+names[1]+', '+names[2] +' and ' +names[3] +" to have dinner with me!")

输出:

I wanna invite Alice, Zayn, John and Chen to have dinner with me!

3-5 修改嘉宾名单: 
你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾: 
·以完成3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。 
·修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。 
·再次打印一系列消息,向名单中的每位嘉宾发出邀请

names=['Alice','Zayn','John','Chen']print("I wanna invite " +names[0]+', '+names[1]+', '+names[2] +' and ' +names[3] +" to have dinner with me!" )
print("Sorry, " + names[3] + " is too busy.")
names[3]='Bob'
print("I'd like to invite "     
    +names[0]+','+names[1]+','+names[2] +' and '+names[3] 
    +" to have dinner with me!")

输出:

I wanna invite Alice, Zayn, John and Chen to have dinner with me!
Sorry, Chen is too busy.I'd like to invite Alice,Zayn,John and Bob to have dinner with me!

3-8 放眼世界 
想出至少5个你渴望去旅游的地方: 
·将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的 
·按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表 
·使用sorted()按字母顺序打印这个列表,同时不要修改它 
·再次打印该列表,核实排列顺序未变 
·使用sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它 
·再次打印该列表,核实排列顺序未变 
·使用reverse()修改列表元素的排列顺序,打印该列表,核实排列顺序确实变了 
·使用reverse()再次修改该列表元素的排列顺序,打印该列表,核实已回复到原来的排列顺序 
·使用sort()修改该列表,使其元素按字母顺序排列,打印该列表,核实已恢复到原来的排列顺序 
·使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列,打印该列表,核实排列顺序确实变了

places=['American','New Zealand','Iceland','Australia','Shangri-la']
#按原始顺序打印
print(places)#使用sorted排序打印
print(sorted(places))#证实顺序未变print(places)#使用sorted反向排序
print(sorted(places,reverse=True))
#证实顺序未变
print(places)
#使用reverse逆序
places.reverse()
#证实顺序改变
print(places)
#使用reverse逆序
places.reverse()
#证实顺序改变
print(places)#使用sort排序
places.sort()#证实顺序改变
print(places)#使用sort反向排序
places.sort(reverse=True)#证实顺序改变
print(places)

输出:

['American', 'New Zealand', 'Iceland', 'Australia', 'Shangri-la']
['American', 'Australia', 'Iceland', 'New Zealand', 'Shangri-la']
['American', 'New Zealand', 'Iceland', 'Australia', 'Shangri-la']
['Shangri-la', 'New Zealand', 'Iceland', 'Australia', 'American']
['American', 'New Zealand', 'Iceland', 'Australia', 'Shangri-la']
['Shangri-la', 'Australia', 'Iceland', 'New Zealand', 'American']
['American', 'New Zealand', 'Iceland', 'Australia', 'Shangri-la']
['American', 'Australia', 'Iceland', 'New Zealand', 'Shangri-la']
['Shangri-la', 'New Zealand', 'Iceland', 'Australia', 'American']

第四章

4-1 比萨:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。 
·修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。 
·在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!

pizzas=['tomato','pepperoni','beef']
for pizza in pizzas:
    print("I like "+ pizza + " pizza")

输出:

I like tomato pizza
I like pepperoni pizza
I like beef pizza

4-3 数到20:使用一个for循环打印数字1~20(含)

for number in range(1,21):
    print(number, end = ' ')

输出:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

4-7 3的倍数:创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for循环将这个列表中的数字都打印出来

numbers=list(range(3,31,3))
for number in numbers:
    print(number)

输出:

3
6
9
12
15
18
21
24
27
30

4-9 立方解析:使用列表解析生成一个列表,其中包含前10个整数的立方

numbers=[num**3 for numin range(1,11)]
print(numbers)

输出:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。 
·打印消息“The first threeitems in thelistare:”,再使用切片来打印列表的前三个元素。 
·打印消息“Three items fromthe middle ofthelistare:”,再使用切片来打印列表中间的三个元素。 
·打印消息“The last threeitems in thelistare:”,再使用切片来打印列表末尾的三个元素。

numbers=[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
print("The first three items in the list are:")
print(numbers[0:3])
print("Three items from the middle of the list are:")
print(numbers[4:7])
print("The last three items in the list are:")
print(numbers[-3:-1])

输出:

The first three items in the list are:
[1, 8, 27]
Three items from the middle of the list are:
[125, 216, 343]
The last three items in the list are:
[512, 729]

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

pizzas=['tomato','pepper','chicken']
friend_pizzas=pizzas[:]

pizzas.append('potato')
friend_pizzas.append('beef')

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:
tomato
pepper
chicken
potato
My friend's favorite pizzas are:
tomato
pepper
chicken
beef
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值