第三章作业

本文通过一系列Python代码示例展示了如何使用列表进行基本操作,包括打印列表元素、向列表中添加和删除元素等。同时,还介绍了如何通过列表来管理嘉宾邀请的过程。

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

"""3.1 姓名"""
names = ['Niki', 'Bob', 'Anddy']
for item in names:
print(item)

"""3.2 问候语"""
names = ['Niki', 'Bob', 'Anddy']
for item in names:
print(item + ", how are you?")

"""3.3 自己的列表"""
transports = ['motorcycle', 'bus', 'car', 'plane', 'rocket']
for item in transports:
print("I woould like to own a " + item + ".")

"""3.4 嘉宾名单"""
names = ['Einstein','Plonk','Huangdi']
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")


"""3.5 修改嘉宾名单"""
names = ['Einstein','Plonk','Huangdi']
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("Sorry, " + names[1] + " do not have time in the evening.")
names[1] = 'Caesar'
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")


"""3.6 添加嘉宾"""
names = ['Einstein','Plonk','Huangdi']
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("Sorry, " + names[1] + " do not have time in the evening.")
names[1] = 'Caesar'
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("I find a more big table")
names.insert(0, 'Qin Shihuang')
names.insert(0, 'Li Bai')
names.append('Han Xin')
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")


"""3.7 缩减名单"""
names = ['Einstein','Plonk','Huangdi']
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("Sorry, " + names[1] + " do not have time in the evening.")
names[1] = 'Caesar'
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("I find a more big table")
names.insert(0, 'Qin Shihuang')
names.insert(0, 'Li Bai')
names.append('Han Xin')
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("Because the new table can not arrive on time, I can just invite two person to have a dinner together.")
for item in names[2:6]:
names.pop()
print(item + ", I am so sorry that I can not have dinner with you.")
for item in names:
print("Hello, " + item + ", I still would like to invite you to have a dinner with me.")
del names[0]
del names[0]
print(names)


"""3.8 放眼世界"""
places = ['Paris', 'Shanghai', 'Rome', 'New York', 'Maldives']
print(places)
print(sorted(places))
print(places)
print(sorted(places, reverse = True))
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse = True)
print(places)


"""3.9 晚餐嘉宾"""
names = ['Einstein','Plonk','Huangdi']
for item in names:
print("Hello, " + item + ", I would like to invite you to have a dinner with me.")
print("I have invite " + str(len(names)) + " people to have a dinner together")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值