【第二周】第三章全部习题3-1~3-11

3-1姓名

names = ['LucyZheng','JairZhu','HHL','WilliamZheng']
for name in names:
    print (name)
3-2问候语
names = ['LucyZheng','JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + ",nice to meet you!")

3-3自己的列表

trans = ['train','bike','car']
for tran in trans:
    print("I would like to own " + tran)

3-4嘉宾名单

names = ['JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + " ,would you like to have a dinner with me?")

3-5修改嘉宾名单

names = ['JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
    print (name + " ,would you like to have a dinner with me?")

3-6添加嘉宾

names = ['JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oh!I have found a bigger table!")
names.insert(0,'Manman') #插入元素
names.insert(2,'Miracle')
names.append('Wonder')
for name in names:
    print (name + " ,would you like to have a dinner with me?")

3-7缩减名单

names = ['JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oh!I have found a bigger table!")
names.insert(0,'Manman') #插入元素
names.insert(2,'Miracle')
names.append('Wonder')
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oops! I only have to invite 2 friends!")
length=len(names)
while length > 2: #判断是否删除到只剩下两位嘉宾
        length-=1
        print(names[length] + " ,I am so sorry!")
        names.pop(-1)
for name in names: #输出两位嘉宾的信息
    print(name + " ,I'm looking forward to your visit.")
del names[0]
del names[0] #删除最后两位嘉宾

3-8放眼世界

cities=['Paris','Chengdu','Mosco','London','Beijing']
print(cities)
print(sorted(cities))  #按字母顺序排序输出,不改变原值
print(cities)
print(sorted(cities,reverse = True))  #按字母反向排序,不改变原值
print(cities)
cities.reverse()  #倒序输出
print(cities)
cities.reverse() #再倒序输出
print(cities)
sorted(cities) #按字母顺序排序修改
print(cities)
sorted(cities,reverse=True) #按字母顺序倒序排序修改
print(cities)

3-9晚餐嘉宾

names = ['JairZhu','HHL','WilliamZheng']
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
    print (name + " ,would you like to have a dinner with me?")
print(len(names))

3-10尝试使用各个函数

cities=['Paris','Chengdu','Mosco','London','Beijing']
cities.append('Shanghai')  #尾部添加
print(cities)
cities.insert(0,'Guangzhou') #中间插入
print(cities)
del cities[3] #删除
print(cities)
cities.pop(-1) #特定位置删除
print(cities)
cities.remove('Beijing') #根据值删除
print(cities)
cities.sort() #排序
print(cities)
cities.reverse() #倒序排列
print(cities)
print(len(cities)) #输出列表长度

3-11有意引发错误,使操作一个越界的值即可。

有意触发:

names=[]
print(names[-1])

修改:

names=['Haha']
print(names[-1])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值