Description
Select from exercise 3-1 to 4-15
Code
3-1 & 3-2
1 | # 3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为names。 |
result:
1 | Tony: |
3-4 ~ 3-7
3-4
1 | # 3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的), |
result:
1 | Hi, Tom .Would you like to have dinner with me? |
3-5
1 | # 3-5 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。 |
result:
1 | Raker cannot come, Tony will instead. |
3-6
1 | # 3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。 |
result:
1 | Good news! I find a larger table! |
3-7
1 | # 3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。 |
result:
1 | I'm sorry to tell you that, my new table cannot arrive in time, so I can only invite 2. |
3-8 ~ 3-11
3-8
1 | # 3-8 放眼世界 :想出至少5个你渴望去旅游的地方。 |
result:
1 | 按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表: |
3-9
1 | # 使用len()打印一条消息,指出想要游玩多少个国家。 |
result:
1 | I want to go 5 places. |
3-11
1 | # 3-11 有意引发错误 :如果你还没有在程序中遇到过索引错误,就尝试引发一个这种错误。 |
result:
1 | File "D:/pyproject/homework1/3-8.py", line 43, in <module> |
4-2
1 | # 4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。 |
result:1
2
3
4
5
6
7
8
9dog
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 | # 4-3 数到20 :使用一个for 循环打印数字1~20(含)。 |
result:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
201
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4-5
1 | # 4-5 计算1~1 000 000的总和 :创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始, |
result:1
2
31
1000000
500000500000
4-6
1 | # 4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。 |
result:1
2
3
4
5
6
7
8
9
101
3
5
7
9
11
13
15
17
19
4-9
1 | # 4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。 |
result:1
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
4-10 & 4-11
4-10
1 | # 4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。 |
result:1
2
3
4
5
6The 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 list1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18print("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
13list 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 | # 4-13 自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。 |
result:1
2
3
4
5apple
juice
pizza
noodles
egg
1 | # 尝试修改其中的一个元素,核实Python确实会拒绝你这样做。 |
result:1
2
3
4
5
6Traceback (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 | # 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块: |
result:1
2
3
4
5apple
cola
cabbage
noodles
egg
PEP格式指南
- 四空格缩进
- 行最长不超过80字符
- 不要在程序中过多地使用空行
- 适当的注释
- …