Python编程 从入门到实践 第三章习题

3-1 姓名
>>> name = ['Alex', 'Leo', 'Mike']
>>> print(name[0])
Alex
>>> print(name[1])
Leo
>>> print(name[2])
Mike

3-2 问候语

>>> message = 'Hello, '+name[0] +', how are you ?'
>>> print(message)
Hello, Alex, how are you ?
>>> print(message1)
Hello, Leo, how are you ?
>>> message2 = 'Hello, '+name[2] +', how are you ?'
>>> print(message2)
Hello, Mike, how are you ?

3-4 嘉宾名单

>>> list = ['Alex', 'Demmo', 'Mike']
>>> print(list[0] + ',May I have the honour to have a dinner with you ?')
Alex,May I have the honour to have a dinner with you ?
>>> print(list[1] + ',May I have the honour to have a dinner with you ?')
Demmo,May I have the honour to have a dinner with you ?
>>> print(list[2] + ',May I have the honour to have a dinner with you ?')
Mike,May I have the honour to have a dinner with you ?

3-5 修改嘉宾名单

>>> list = ['Alex', 'Demmo', 'Mike']
>>> print(list[0] + " is unable to attent the dinner !")
Alex is unable to attent the dinner !
>>> list[0] = 'Leo'
>>> print(list[0] + ',May I have the honour to have a dinner with you ?')
Leo,May I have the honour to have a dinner with you ?
>>> print(list[1] + ',May I have the honour to have a dinner with you ?')
Demmo,May I have the honour to have a dinner with you ?
>>> print(list[2] + ',May I have the honour to have a dinner with you ?')
Mike,May I have the honour to have a dinner with you ?

3-6 添加嘉宾

>>> list = ['Alex', 'Demmo', 'Mike']
>>> print('We can invite more people to join us!')
We can invite more people to join us!
>>> list.insert(0,'Jack')
>>> list.insert(2,'Bessie')
>>> list.append('Lili')
>>> list
['Jack', 'Alex', 'Bessie', 'Demmo', 'Mike', 'Lili']
>>> print(list[0] + ',May I have the honour to have a dinner with you ?')
Jack,May I have the honour to have a dinner with you ?
>>> print(list[1] + ',May I have the honour to have a dinner with you ?')
Alex,May I have the honour to have a dinner with you ?
>>> print(list[2] + ',May I have the honour to have a dinner with you ?')
Bessie,May I have the honour to have a dinner with you ?
>>> print(list[3] + ',May I have the honour to have a dinner with you ?')
Demmo,May I have the honour to have a dinner with you ?
>>> print(list[4] + ',May I have the honour to have a dinner with you ?')
Mike,May I have the honour to have a dinner with you ?
>>> print(list[5] + ',May I have the honour to have a dinner with you ?')
Lili,May I have the honour to have a dinner with you ?

3-8 放眼望世界

>>> travel_location = ['Guangzhou','Wuhan','Beijing','Shanghai','Nanjing']
>>> print(travel_location)                
['Guangzhou', 'Wuhan', 'Beijing', 'Shanghai', 'Nanjing']            #原始顺序
>>> print(sorted(travel_location))
['Beijing', 'Guangzhou', 'Nanjing', 'Shanghai', 'Wuhan']            #Sorted()排序
>>> print(travel_location)
['Guangzhou', 'Wuhan', 'Beijing', 'Shanghai', 'Nanjing']            #sorted()排序未改变原始顺序 核查
>>> travel_location.reverse()        
>>> print(travel_location)
['Nanjing', 'Shanghai', 'Beijing', 'Wuhan', 'Guangzhou']            #reverse()改变原始顺序
>>> travel_location.reverse()
>>> print(travel_location)
['Guangzhou', 'Wuhan', 'Beijing', 'Shanghai', 'Nanjing']            #reverse()恢复原始顺序
>>> travel_location.sort()
>>> print(travel_location)
['Beijing', 'Guangzhou', 'Nanjing', 'Shanghai', 'Wuhan']            #sort()改变顺序
>>> travel_location.sort(reverse = True)
>>> print(travel_location)
['Wuhan', 'Shanghai', 'Nanjing', 'Guangzhou', 'Beijing']            #按与字母顺序相反的顺序排列





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值