[python作业] [第二周]

注:有的代码包含了多个题目的解答。

3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为 names。依次访问
该列表中的每个元素,从而将每个朋友的姓名都打印出来。

3-2 问候语: 继续使用练习 3-1 中的列表,但不打印每个朋友的姓名,而为每人打
印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

>>> names = ['Edison', 'Cathy', 'Charlie']
>>> for name in names:
    print('Hello' + name)

Hello Edison
Hello Cathy
Hello Charlie

3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),
你会邀请哪些人?请创建一个列表,其中包含至少 3 个你想邀请的人;然后,使用这个
列表打印消息,邀请这些人来与你共进晚餐。

3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
 以完成练习 3-4 时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪
位嘉宾无法赴约。
 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
 再次打印一系列消息,向名单中的每位嘉宾发出邀请。

3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀
请哪三位嘉宾。
 以完成练习 3-4 或练习 3-5 时编写的程序为基础,在程序末尾添加一条 print 语
句,指出你找到了一个更大的餐桌。
 使用 insert()将一位新嘉宾添加到名单开头。

>>> names = ['Edison', 'Cathy', 'Charlie']
>>> for name in names:
    print("Hello " + name)


Hello Edison
Hello Cathy
Hello Charlie

>>> for name in names:
    print(name + ' would you like to join our dinner?')


A would you like to join our dinner?
B would you like to join our dinner?
C would you like to join our dinner?

A can't join us.
>>> names[0] = 'D'
>>> for name in names:
    print(name + ' would you like to join our dinner?')


D would you like to join our dinner?
B would you like to join our dinner?
C would you like to join our dinner?

>>> names.insert(0, 'Edison')
>>> print(names)
['Edison', 'D', 'B', 'C']
>>> names.insert(2, 'Cathy')
>>> print(names)
['Edison', 'D', 'Cathy', 'B', 'C']
>>> names.insert(4, 'Charlie')
>>> print(names)
['Edison', 'D', 'Cathy', 'B', 'Charlie', 'C']

4-2 动物:想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,
再使用 for 循环将每种动物的名称都打印出来。

animals = ['whale', 'dolphin', 'penguin']
for animal in animals:
    print('A ' + animal + ' would be a great pet.')
print('Any of these animial would make a great pet!')
Output:
A whale would be a great pet.
A dolphin would be a great pet.
A penguin would be a great pet.
Any of these animial would make a great pet!

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

#使用列表解析生成列表
nums = [num for num in range(1, 1000001)]
#使用range()生成列表
#nums = list(range(1, 1000001))
print(min(nums))
print(max(nums))
print(sum(nums))
Output:
min: 1
max: 1000000
sum: 500000500000

4-8 立方:将同一个数字乘三次称为立方。例如,在 Python 中, 2 的立方用 2**3
表示。请创建一个列表,其中包含前 10 个整数(即 1~10)的立方,再使用一个 for 循
环将这些立方数都打印出来。

4-9 立方解析:使用列表解析生成一个列表,其中包含前 10 个整数的立方。

nums = [num ** 3 for num in range(1, 11)]
print('The cube of 1 to 10 is:')
for num in nums:
    print(num, end = ' ')
Output:
The cube of 1 to 10 is:
1 8 27 64 125 216 343 512 729 1000 

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:”,再使用切片来打印列表末尾的三
个元素。

nums = [num ** 3 for num in range(1, 11)]
print('The first three items in the list are:\n', nums[0:4])
print('The items from the middle of the list are:\n', nums[4:8])
print('The last three items in the list are:\n', nums[-3:])
Output:
The first three items in the list are:
 [1, 8, 27, 64]
The items from the middle of the list are:
 [125, 216, 343, 512]
The last three items in the list are:
 [512, 729, 1000]

4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食
品,并将其存储在一个元组中。
 使用一个 for 循环将该餐馆提供的五种食品都打印出来。
 尝试修改其中的一个元素,核实 Python 确实会拒绝你这样做。
 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块:
给元组变量赋值,并使用一个 for 循环将新元组的每个元素都打印出来。

>>> foods = ('Apple', 'Banana', 'Chocolate', 'Pizza', 'Pasta')
>>> for food in foods:
    print(food, end = ' ')


Apple Banana Chocolate Pizza Pasta 
>>> foods[0] = 'Milk'
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    foods[0] = 'Milk'
TypeError: 'tuple' object does not support item assignment
>>> foods = ('Apple', 'Banana', 'Chocolate', 'Fish', 'Noodle')
>>> for food in foods:
    print(food, end = ' ')


Apple Banana Chocolate Fish Noodle 

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/79527108