一、将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
names=['Tom','Mike','Jan']
print(names[0])
print(names[1])
print(names[2])
二、继续上一道题,但不打印每个朋友的姓名,而为每个人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names=['Tom','Mike','Jan']
message=names[0]+",how are you?"
print(message)
message=names[1]+",how are you?"
print(message)
message=names[2]+",how are you?"
print(message)
三、想想你喜欢的通勤方式,如骑摩托或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“i would like to own a honda motorcycle”。
vehicles=['bike','car','plane']
message='i would like to own a GIANT '+vehicles[0]
print(message)
message='i would like to own a BMW '+vehicles[1]
print(message)
message='i would like to own a Boeing '+vehicles[2]
print(message)
四、如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
star=['马云','雷军','刘强东']
message=star[0]+",can you have dinner with me?"
print(message)
message=star[1]+",can you have dinner with me?"
print(message)
message=star[2]+",can you have dinner with me?"
print(message)
五、你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。在上一个程序末尾添加一条print语句,指出哪位嘉宾无法赴约。
修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
再打印一系列消息,向名单中的每位嘉宾发出邀请。
print("刘强东 may be absent")
star[2]='马化腾'
message=star[0]+",can you have dinner with me?"
print(message)
message=star[1]+",can you have dinner with me?"
print(message)
message=star[2]+",can you have dinner with me?"
print(message)
六、你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。在程序末添加一条print语句,指出你找到了一个更大的餐桌。
使用insert()将一位新嘉宾添加到名单开头。
使用insert()将另一位新嘉宾添加到名单中间。
使用append()将最后一位新嘉宾添加到名单末尾。
打印一系列消息,向名单中的每位嘉宾发出邀请。
print("i have found a bigger table")
star.insert(0,'李彦宏')
star.insert(2,'王健林')
star.append('李嘉诚')
message=star[0]+",can you have dinner with me?"
print(message)
message=star[1]+",can you have dinner with me?"
print(message)
message=star[2]+",can you have dinner with me?"
print(message)
message=star[3]+",can you have dinner with me?"
print(message)
message=star[4]+",can you have dinner with me?"
print(message)
message=star[5]+",can you have dinner with me?"
print(message)
七、你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。在程序末添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
使用pop()不断地删除名单中的嘉宾,知道只有两位嘉宾为止。每次从名单中弹出一位嘉宾时都打印一条消息,让该嘉宾知晓你很抱歉,无法邀请他来共进晚餐。
对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
print("sorry everyone,because of the problem of table,i only can invite two people.")
pop_people=star.pop()
print('sorry '+pop_people)
pop_people=star.pop()
print('sorry '+pop_people)
pop_people=star.pop()
print('sorry '+pop_people)
pop_people=star.pop()
print('sorry '+pop_people)
print("congratulations! "+star[0])
print("congratulations! "+star[1])
del star[0]
del star[0]
print(star)
八、想出至少5个你渴望去旅游的地方。将这些地方存储在一个列表里,并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。
使用sorted()按字母顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
使用sorted()按与字母相反的顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
使用reserve()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
places=['xizang','hainan','heilongjiang','beijing','xianggang']
print(places)
print(sorted(places))
print(places)
print(sorted(places,reverse=True))
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse=True)
print(places)
九、使用len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。
print(len(star))
我的博客: