some answers for chapter 3

3-2

names = ['mike','john','jack']
print("Hello,"+ names[0].title()+"!")
print("Hello,"+ names[1].title()+"!")
print("Hello,"+ names[2].title()+"!")
output
Hello,Mike!
Hello,John!
Hello,Jack!

3-4~ 3-7

friends = ['a','b','c']
for friend in friends:
    print(friend +",please have dinner with me.")
print("\n")
busy_friend = 'b'
print("Oh,"+ busy_friend +" is unable to arrive.")
friends.remove(busy_friend)
friends.append('d')
for friend in friends:
    print(friend +",please have dinner with me.")
print("\n")
print("Oh,I get a bigger table and I can invite more friends.")
friends.insert(0,'e')
friends.insert(2,'f')
friends.append('g')
for friend in friends:
    print(friend +",please have dinner with me.")
print("\n")
print("Sadly,the new table I buy won't arrive in time.I can only invite two friends now.")
whilelen(friends) >2 :
    one = friends.pop()
    print("I am sorry,"+ one +".Some accident happened and I couldn't have dinner with you this night.")
for friend in friends:
    print(friend +",we will still have dinner this night.")
del friends[0]
del friends[0]
for friend in friends:
    print("Oh,"+ friend +"is still in the list.")
output
a,please have dinner with me.
b,please have dinner with me.
c,please have dinner with me.
 
Oh,b is unable to arrive.
a,please have dinner with me.
c,please have dinner with me.
d,please have dinner with me.
 
Oh,I get a bigger table and I can invite more friends.
e,please have dinner with me.
a,please have dinner with me.
f,please have dinner with me.
c,please have dinner with me.
d,please have dinner with me.
g,please have dinner with me.
 
Sadly,the new table I buy won't arrive in time.I can only invite two friends now.
I am sorry,g.Some accident happened and I couldn't have dinner with you this night.
I am sorry,d.Some accident happened and I couldn't have dinner with you this night.
I am sorry,c.Some accident happened and I couldn't have dinner with you this night.
I am sorry,f.Some accident happened and I couldn't have dinner with you this night.
e,we will still have dinner this night.
a,we will still have dinner this night.

3-10

countries = ["China","England","America"]
print("The initial list:")
print(countries)
print("Its length is "+str(len(countries)))
print("The sorted list without changing the initial list:")
print(sorted(countries))
print("The reversed list:")
countries.reverse()
print(countries)
print("The sorted list in reverse:")
countries.sort(reverse = True)
print(countries)
output
The initial list:
['China', 'England', 'America']
Its length is 3
The sorted list without changing the initial list:
['America', 'China', 'England']
The reversed list:
['America', 'England', 'China']
The sorted list in reverse:
['England', 'China', 'America']

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值