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']            #按与字母顺序相反的顺序排列





猜你喜欢

转载自blog.csdn.net/sysu_alex/article/details/79532793