W2-C1 作业

本文通过一系列Python代码示例展示了如何使用列表进行基本操作,包括打印列表元素、邀请名单管理和更新等。具体涉及列表的创建、元素访问、修改、插入、删除及排序等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3-1
names=["fang yuan","han ning","yang qinglian","gao henan"]
print(names[0].title())
print(names[1].title())
print(names[2].title())
print(names[3].title())

#注意title后面有()

3-2

names=["fang yuan","han ning","yang qinglian","gao henan"]
print(names[0].title()+", long time no see!")
print(names[1].title()+", long time no see!")
print(names[2].title()+", long time no see!")
print(names[3].title()+", long time no see!")


3-4

guest=["fang yuan","han ning","yang qinglian","gao henan"]
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")

3-5(3-9)
guest=["fang yuan","han ning","yang qinglian","gao henan"]
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print(guest[0].title()+" cannot come")
guest[0]="xia mingze"
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print("I have invited "+str(len(guest))+" people for dinner")

3-6
guest=["fang yuan","han ning","yang qinglian","gao henan"]
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print("I have found a bigger table!")
guest.insert(0,"xia mingze")
guest.insert(2,"hua xianhe")
guest.append("wang heying")
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print("Dear "+guest[4].title()+", I want to have dinner with you!")
print("Dear "+guest[5].title()+", I want to have dinner with you!")
print("Dear "+guest[6].title()+", I want to have dinner with you!")

3-7
guest=["fang yuan","han ning","yang qinglian","gao henan"]
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print("I have found a bigger table!")
guest.insert(0,"xia mingze")
guest.insert(2,"hua xianhe")
guest.append("wang heying")
print("Dear "+guest[0].title()+", I want to have dinner with you!")
print("Dear "+guest[1].title()+", I want to have dinner with you!")
print("Dear "+guest[2].title()+", I want to have dinner with you!")
print("Dear "+guest[3].title()+", I want to have dinner with you!")
print("Dear "+guest[4].title()+", I want to have dinner with you!")
print("Dear "+guest[5].title()+", I want to have dinner with you!")
print("Dear "+guest[6].title()+", I want to have dinner with you!")
print("It is a pity that the table cannot be delivered on time")
pop_guest=guest.pop(2).title()
print("Dear "+pop_guest+", sorry the table isn't big enough, and maybe next time!")
pop_guest=guest.pop(5).title()
print("Dear "+pop_guest+", sorry the table isn't big enough, and maybe next time!")
pop_guest=guest.pop(3).title()
print("Dear "+pop_guest+", sorry the table isn't big enough, and maybe next time!")
pop_guest=guest.pop().title()
print("Dear "+pop_guest+", sorry the table isn't big enough, and maybe next time!")
pop_guest=guest.pop().title()
print("Dear "+pop_guest+", sorry the table isn't big enough, and maybe next time!")
print("Dear "+guest[0].title()+", you are still welcomed to come")
print("Dear "+guest[1].title()+", you are still welcomed to come")
del guest[0]
del guest[0]
print(guest)

3-8
places=["Paris","Beijing","Hongkong","London","Shanghai"]
print(places)
print(sorted(places))
print(places)
print(sorted(places,reverse=True))
#注意reverse和true的大小写
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse=True)
print(places)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值