《Python编程——从入门到实践》第三章部分习题解

# 3-3
traffic_tools = ['bicycle' , 'car' , 'motorcycle']
print('I would like to own a ' + traffic_tools[0])
print('I would like to own a ' + traffic_tools[1])
print('I would like to own a ' + traffic_tools[2])

# 3-7
guests = ['Mike' , 'Wilt' , 'Bill']
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print(guests[2] + ',welcome to have a dinner with me tonight!')
print('Sorry ' + guests[2] + ", I'm sorry to tell you that you can't eat dinner with me tonight.")
guests = ['Mike' , 'Wilt']
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print("I'm glad to tell you all that I've found a bigger table for the dinner and I can invite more guests!")
guests.insert(0,'Shaq')
guests.insert(1,'Oscar')
guests.append('Elvin')
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print(guests[2] + ',welcome to have a dinner with me tonight!')
print(guests[3] + ',welcome to have a dinner with me tonight!')
print(guests[4] + ',welcome to have a dinner with me tonight!')
print("I'm sorry to tell you all that I could only invite two guests.")
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
del guests[0]
del guests[0]
print(guests)

# 3-10
elements = ['mountain' , 'river' , 'country' , 'city' , 'language']
elements.append('sport')
elements.insert(0,'comic')
print(elements)
del elements[6]
popped = elements.pop()
print(popped)
elements.remove('country')
print(elements)
temp = sorted(elements)
print(temp)
print(elements)
elements.sort()
print(elements)
elements.reverse()
print(elements)
print(len(elements))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值