3-1 姓名:将一些朋友的姓名存储到一个列表中,并将其命名为names,依次访问该列表中的每个元素,从而将每个朋友的姓名打印出来。
代码:
names = ["Jack", "Rose", "John", "Alice"] print(names[0]) print(names[1]) print(names[2]) print(names[-1])
结果:
Jack Rose John Alice
3-2 问候语:继续使用练习3-1的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
代码:
names = ["Jack", "Rose", "John", "Alice"] print("Hello, " + names[0] + "!" ) print("Hello, " + names[1] + "!" ) print("Hello, " + names[2] + "!" ) print("Hello, " + names[-1] + "!" )
结果:
Hello, Jack! Hello, Rose! Hello, John! Hello, Alice!
3-3 自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表,根据该列表打印一系列有关这些通勤方式的宣言,如:“I would like to own a Honda motorcycle”。
代码:
trans = ["car", "taxi", "subway", "plane", "train"] print("I would like to own a " + trans[0] + "." ) print("I would like to go to the market by " + trans[1] + "." ) print("I would like to have a trip from school by the " + trans[2] +".") print("I would like to travel by " + trans[3] + "." ) print("Taking a " + trans[-1] + "to travel is cheaper than taking a "+ trans[-2]+".")
结果:
I would like to own a car. I would like to go to the market by taxi. I would like to have a trip from school by the subway. I would like to travel by plane. Taking a trainto travel is cheaper than taking a plane.
3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人,然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
代码:
names = ["Jack", "Rose", "John", "Alice"] print(names[0] +", I would like to invite you to dinner tonight. ") print(names[1] +", I would like to invite you to dinner tonight. ") print(names[2] +", I would like to invite you to dinner tonight. ") print(names[3] +", I would like to invite you to dinner tonight. ")
结果:
Jack, I would like to invite you to dinner tonight. Rose, I would like to invite you to dinner tonight. John, I would like to invite you to dinner tonight. Alice, I would like to invite you to dinner tonight.
3-5 修改嘉宾名单: 你刚得知有位嘉宾无法赴约,因此需要另外邀请另外一位嘉宾。
以完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。
修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
再次打印出一系列的消息,向名单中的每位嘉宾发出邀请。
代码:
names = ["Jack", "Rose", "John", "Alice"] people = names.pop(2) names.insert(2,"Alen") print(names[0] +", I would like to invite you to dinner tonight. ") print(names[1] +", I would like to invite you to dinner tonight. ") print(names[2] +", I would like to invite you to dinner tonight. ") print(names[3] +", I would like to invite you to dinner tonight. ") print(people + " can't come for the dinner.")
结果:
Jack, I would like to invite you to dinner tonight. Rose, I would like to invite you to dinner tonight. Alen, I would like to invite you to dinner tonight. Alice, I would like to invite you to dinner tonight. John can't come for the dinner.
3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾,请想想你还想邀请哪三个嘉宾。
以完成练习3-4或3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。
使用insert()将一位新嘉宾添加到名单开头。
使用insert()将另一位新嘉宾添加到名单中间。
使用append()将最后一位新嘉宾添加到名单末尾。
打印一系列消息,向名单中的每位嘉宾发出邀请。
代码:
names = ["Jack", "Rose", "John", "Alice"] people = names.pop(2) names.insert(2,"Alen") names.insert(0,"Betty") names.insert(3,"Tim") names.append("Tom") print("I have found a bigger table! ") print(names[0] +", I would like to invite you to dinner tonight. ") print(names[1] +", I would like to invite you to dinner tonight. ") print(names[2] +", I would like to invite you to dinner tonight. ") print(names[3] +", I would like to invite you to dinner tonight. ") print(names[4] +", I would like to invite you to dinner tonight. ") print(names[5] +", I would like to invite you to dinner tonight. ") print(names[6] +", I would like to invite you to dinner tonight. ") print(people + " can't come for the dinner.")
结果:
I have found a bigger table! Betty, I would like to invite you to dinner tonight. Jack, I would like to invite you to dinner tonight. Rose, I would like to invite you to dinner tonight. Tim, I would like to invite you to dinner tonight. Alen, I would like to invite you to dinner tonight. Alice, I would like to invite you to dinner tonight. Tom, I would like to invite you to dinner tonight. John can't come for the dinner.
3-7 缩减名单:你刚得知新购卖的餐桌无法及时到送达,因此只能邀请两位嘉宾。
以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印出一条你只能邀请两位嘉宾共进晚餐的消息。
使用pop()不断地删除名单中的嘉宾,直到直邮两位嘉宾为止,每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
对于余下的两位嘉宾中的每一位,都打印出一条消息,指出他依然在受邀人之列。
使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
代码:
names = ["Jack", "Rose", "John", "Alice"] people = names.pop(2) names.insert(2,"Alen") names.insert(0,"Betty") names.insert(3,"Tim") names.append("Tom") print("I have found a bigger table! ") print(names[0] +", I would like to invite you to dinner tonight. ") print(names[1] +", I would like to invite you to dinner tonight. ") print(names[2] +", I would like to invite you to dinner tonight. ") print(names[3] +", I would like to invite you to dinner tonight. ") print(names[4] +", I would like to invite you to dinner tonight. ") print(names[5] +", I would like to invite you to dinner tonight. ") print(names[6] +", I would like to invite you to dinner tonight. ") print(people + " can't come for the dinner.") print("I can only invite two of you to the dinner.") people = names.pop() print(people + ", I am sorry that I can't invite you. ") people = names.pop() print(people + ", I am sorry that I can't invite you. ") people = names.pop() print(people + ", I am sorry that I can't invite you. ") people = names.pop() print(people + ", I am sorry that I can't invite you. ") people = names.pop() print(people + ", I am sorry that I can't invite you. ") print(names[0]+ ", you can come for the dinner.") print(names[1]+ ", you can come for the dinner.") del names[0] del names[0] print(names)
结果:
I have found a bigger table! Betty, I would like to invite you to dinner tonight. Jack, I would like to invite you to dinner tonight. Rose, I would like to invite you to dinner tonight. Tim, I would like to invite you to dinner tonight. Alen, I would like to invite you to dinner tonight. Alice, I would like to invite you to dinner tonight. Tom, I would like to invite you to dinner tonight. John can't come for the dinner. I can only invite two of you to the dinner. Tom, I am sorry that I can't invite you. Alice, I am sorry that I can't invite you. Alen, I am sorry that I can't invite you. Tim, I am sorry that I can't invite you. Rose, I am sorry that I can't invite you. Betty, you can come for the dinner. Jack, you can come for the dinner. []
3-8 放眼世界: 想出至少5个你渴望去旅游的地方。
将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表,不要考虑输出是否整洁的问题,只管打印原始的Python列表。
使用sorted()按字母顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
再次使用sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实列表顺序确实变了。
使用sort()修改该列表,使其元素按字母顺序相反的顺序排列。打印该列表,核实列表顺序确实变了。
代码:
places = ["Beijing", "Hangzhou", "Shanghai", "America", "London"] 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)
结果:
['Beijing', 'Hangzhou', 'Shanghai', 'America', 'London'] ['America', 'Beijing', 'Hangzhou', 'London', 'Shanghai'] ['Beijing', 'Hangzhou', 'Shanghai', 'America', 'London'] ['Shanghai', 'London', 'Hangzhou', 'Beijing', 'America'] ['Beijing', 'Hangzhou', 'Shanghai', 'America', 'London'] ['London', 'America', 'Shanghai', 'Hangzhou', 'Beijing'] ['Beijing', 'Hangzhou', 'Shanghai', 'America', 'London'] ['America', 'Beijing', 'Hangzhou', 'London', 'Shanghai'] ['Shanghai', 'London', 'Hangzhou', 'Beijing', 'America']
3-9 晚餐嘉宾: 在完成练习3-4~练习3-7时编写的程序之一中,使用len()打印一条消息,指出你邀请了多少位嘉宾与你共进晚餐。
代码:(在3-4练习基础上)
names = ["Jack", "Rose", "John", "Alice"] print(names[0] +", I would like to invite you to dinner tonight. ") print(names[1] +", I would like to invite you to dinner tonight. ") print(names[2] +", I would like to invite you to dinner tonight. ") print(names[3] +", I would like to invite you to dinner tonight. ") print(len(names))
结果:
Jack, I would like to invite you to dinner tonight. Rose, I would like to invite you to dinner tonight. John, I would like to invite you to dinner tonight. Alice, I would like to invite you to dinner tonight. 4
3-11: 有意引发错误: 如果你还没有在程序中遇到过索引错误,就尝试引发一个这种错误,在你的一个程序中,修改其中的索引,以引发索引错误,关闭程序前,务必消除这个错误。
代码:(错误代码)
names = ["Jack", "Rose", "John", "Alice"] print(names[4])
结果:(错误代码)
Traceback (most recent call last): File "hello_world.py", line 2, in <module> print(names[4]) IndexError: list index out of range
代码:(修改后)
names = ["Jack", "Rose", "John", "Alice"] print(names[3])
结果:(修改后)
Alice