Python作业02-列表

Description

Select from exercise 3-1 to 4-15

Code

3-1 & 3-2

1
2
3
4
5
6
7
8
# 3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为names。
# 依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
# 3-2 问候语:继续使用练习3-1中的列表,但不打印每个朋友的姓名,
# 而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ["tony", "marry","tom","ma"]
for name in names:
print(name.title()+": ")
print("Hello, "+name+" .")

result:

1
2
3
4
5
6
7
8
9
10
Tony: 
Hello, tony .
Marry:
Hello, marry .
Tom:
Hello, tom .
Ma:
Hello, ma .

Process finished with exit code 0

3-4 ~ 3-7

3-4

1
2
3
4
5
6
# 3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),
# 你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;
# 然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
names = ["Tom","Raker", "Gakki"]
for name in names:
print("Hi, "+name+" .Would you like to have dinner with me?")

result:

1
2
3
Hi, Tom .Would you like to have dinner with me?
Hi, Raker .Would you like to have dinner with me?
Hi, Gakki .Would you like to have dinner with me?

3-5

1
2
3
4
5
6
7
8
# 3-5 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。
# 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
# 再次打印一系列消息,向名单中的每位嘉宾发出邀请。
new_name = "Tony"
print(names[1]+" cannot come, "+new_name+" will instead.")
names[1] = new_name
for name in names:
print("Hi, " + name + " .Would you like to have dinner with me?")

result:

1
2
3
4
Raker cannot come, Tony will instead.
Hi, Tom .Would you like to have dinner with me?
Hi, Tony .Would you like to have dinner with me?
Hi, Gakki .Would you like to have dinner with me?

3-6

1
2
3
4
5
6
7
8
9
10
11
12
# 3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
# 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print 语句,指出你找到了一个更大的餐桌。
# 使用insert() 将一位新嘉宾添加到名单开头。
# 使用insert() 将另一位新嘉宾添加到名单中间。
# 使用append() 将最后一位新嘉宾添加到名单末尾。
# 打印一系列消息,向名单中的每位嘉宾发出邀请。
print("Good news! I find a larger table!")
names.insert(0,"Dollus")
names.insert(2,"Sam")
names.append("Lilith")
for name in names:
print("Hi, " + name + " .Would you like to have dinner with me?")

result:

1
2
3
4
5
6
7
Good news! I find a larger table!
Hi, Dollus .Would you like to have dinner with me?
Hi, Tom .Would you like to have dinner with me?
Hi, Sam .Would you like to have dinner with me?
Hi, Tony .Would you like to have dinner with me?
Hi, Gakki .Would you like to have dinner with me?
Hi, Lilith .Would you like to have dinner with me?

3-7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
# 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
# 使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
# 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
# 使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
print("I'm sorry to tell you that, my new table cannot arrive in time, so I can only invite 2.")
while len(names)>2:
sorry_name = names.pop()
print("Hi "+sorry_name+", I'm sorry to tell you that I cannot have dinner with you.")
for name in names:
print("Hey "+name+", you are still in my invite list!")
del names[0]
del names[0]
print(names)

result:

1
2
3
4
5
6
7
8
I'm sorry to tell you that, my new table cannot arrive in time, so I can only invite 2.
Hi Lilith, I'm sorry to tell you that I cannot have dinner with you.
Hi Gakki, I'm sorry to tell you that I cannot have dinner with you.
Hi Tony, I'm sorry to tell you that I cannot have dinner with you.
Hi Sam, I'm sorry to tell you that I cannot have dinner with you.
Hey Dollus, you are still in my invite list!
Hey Tom, you are still in my invite list!
[]

3-8 ~ 3-11

3-8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 3-8 放眼世界 :想出至少5个你渴望去旅游的地方。

places = ["Egypt", "Tibet", "France", "Russia", "Australia"]
# 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。

print("按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表:")
print(places)

print("使用sorted() 按字母顺序打印这个列表,同时不要修改它:")
print(sorted(places))

print("再次打印该列表,核实排列顺序未变:")
print(places)

print("使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它:")
print(sorted(places, reverse=True))

print("再次打印该列表,核实排列顺序未变:")
print(places)

print("使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了:")
places.reverse()
print(places)

print("使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序:")
places.reverse()
print(places)

print("使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了:")
places.sort()
print(places)

print("使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了:")
places.sort(reverse=True)
print(places)

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表:
['Egypt', 'Tibet', 'France', 'Russia', 'Australia']
使用sorted() 按字母顺序打印这个列表,同时不要修改它:
['Australia', 'Egypt', 'France', 'Russia', 'Tibet']
再次打印该列表,核实排列顺序未变:
['Egypt', 'Tibet', 'France', 'Russia', 'Australia']
使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它:
['Tibet', 'Russia', 'France', 'Egypt', 'Australia']
再次打印该列表,核实排列顺序未变:
['Egypt', 'Tibet', 'France', 'Russia', 'Australia']
使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了:
['Australia', 'Russia', 'France', 'Tibet', 'Egypt']
使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序:
['Egypt', 'Tibet', 'France', 'Russia', 'Australia']
使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了:
['Australia', 'Egypt', 'France', 'Russia', 'Tibet']
使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了:
['Tibet', 'Russia', 'France', 'Egypt', 'Australia']

Process finished with exit code 0

3-9

1
2
# 使用len()打印一条消息,指出想要游玩多少个国家。
print("I want to go "+str(len(places))+" places.")

result:

1
I want to go 5 places.

3-11

1
2
3
4
# 3-11 有意引发错误 :如果你还没有在程序中遇到过索引错误,就尝试引发一个这种错误。
# 在你的一个程序中,修改其中的索引,以引发索引错误。关闭程序前,
# 务必消除这个错误。
print(places[7])

result:

1
2
3
File "D:/pyproject/homework1/3-8.py", line 43, in <module>
print(places[7])
IndexError: list index out of range

4-2

1
2
3
4
5
6
7
8
9
10
# 4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。
animals = ["dog", "cat", "hamster"]
for animal in animals:
print(animal)

# 修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
for animal in animals:
print("A "+animal+" would make a good pet.")
# 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子。
print("Any of these animals would make a greate pet!")

result:

1
2
3
4
5
6
7
8
9
dog
cat
hamster
A dog would make a good pet.
A cat would make a good pet.
A hamster would make a good pet.
Any of these animals would make a greate pet!

Process finished with exit code 0

4-3 ~ 4-9

4-3

1
2
3
# 4-3 数到20 :使用一个for 循环打印数字1~20(含)。
for number in range(1, 21):
print(number)

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

4-5

1
2
3
4
5
6
# 4-5 计算1~1 000 000的总和 :创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始,
# 到1 000 000结束的。另外,对这个列表调用函数sum() ,看看Python将一百万个数字相加需要多长时间。
numbers = range(1, 1000001)
print(min(numbers))
print(max(numbers))
print(sum(numbers))

result:

1
2
3
1
1000000
500000500000

4-6

1
2
3
4
# 4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。
odd_numbers = range(1,20,2)
for odd_number in odd_numbers:
print(odd_number)

result:

1
2
3
4
5
6
7
8
9
10
1
3
5
7
9
11
13
15
17
19

4-9

1
2
3
# 4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
cube_numbers = [ number**3 for number in range(1,11)]
print(cube_numbers)

result:

1
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 & 4-11

4-10

1
2
3
4
5
6
7
8
9
10
# 4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
# 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
# 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
# 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素。
print("The first three items in the list are:")
print(cube_numbers[:3])
print("Three items from the middle of the list are:")
print(cube_numbers[3:6])
print("The last three items in the list are:")
print(cube_numbers[6:])

result:

1
2
3
4
5
6
The first three items in the list are:
[1, 8, 27]
Three items from the middle of the list are:
[64, 125, 216]
The last three items in the list are:
[343, 512, 729, 1000]

4-11

copy a list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
print("list A:")
A = ["A", "E", "I", "O"]
print(A)
print("list B copy from A:")
B = A[:]
print(B)
print("Change B and A didn't change:")
B.append("U")
print(A)
print(B)
print("C equal to A directly:")
C = A
print(A)
print(C)
print("C change and make A change:")
C.pop()
print(A)
print(C)

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
list A:
['A', 'E', 'I', 'O']
list B copy from A:
['A', 'E', 'I', 'O']
Change B and A didn't change:
['A', 'E', 'I', 'O']
['A', 'E', 'I', 'O', 'U']
C equal to A directly:
['A', 'E', 'I', 'O']
['A', 'E', 'I', 'O']
C change and make A change:
['A', 'E', 'I']
['A', 'E', 'I']

4-13

1
2
3
4
5
# 4-13 自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
# 使用一个for 循环将该餐馆提供的五种食品都打印出来。
foods = ("apple", "juice", "pizza", "noodles", "egg")
for food in foods:
print(food)

result:

1
2
3
4
5
apple
juice
pizza
noodles
egg

1
2
# 尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
foods[3] = "chicken"

result:

1
2
3
4
5
6
Traceback (most recent call last):
File "D:/pyproject/homework1/4-2.py", line 18, in <module>
foods[3] = "chicken"
TypeError: 'tuple' object does not support item assignment

Process finished with exit code 1

1
2
3
4
5
# 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块:
# 给元组变量赋值,并使用一个for 循环将新元组的每个元素都打印出来。
foods = ("apple", "cola", "cabbage", "noodles", "egg")
for food in foods:
print(food)

result:

1
2
3
4
5
apple
cola
cabbage
noodles
egg

PEP格式指南

  • 四空格缩进
  • 行最长不超过80字符
  • 不要在程序中过多地使用空行
  • 适当的注释