Python 从入门到实践 第四次课后习题

4-1 比萨 :想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。 在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。

Pizza = ["Banana","Beef","Orange","Apple"]
for i in Pizza:
#     print( "I like {} pizza ! ".format(i))
      print( "I like %s pizza ! "%i)
print("I really love pizza !")    
I like Banana pizza ! 
I like Beef pizza ! 
I like Orange pizza ! 
I like Apple pizza ! 
I really love pizza !

4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。 修改这个程序,使其针对每种动物都打印一个句子,如“Adogwould makea great pet”。 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any oftheseanimals would makea great pet!”这样的句子。

Anamial = ["Panda","Pig","cat","mouse"]
for i in Anamial:
    print("A {} would make a great pet !".format(i))
print("Any of these animals would makea great pet!")
A Panda would make a great pet !
A Pig would make a great pet !
A cat would make a great pet !
A mouse would make a great pet !
Any of these animals would makea great pet!

4-3 数到20 :使用一个for 循环打印数字1~20(含)。

for i in range(1,21):
    print(i,end=" ")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

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

lis = []
for i in range(1,1000000):
    lis.append(i)
a=min(lis)
b=max(lis)
c=sum(lis)
print(a,b,c)
1 999999 499999500000

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

number = []
for i in range(1,21,2):#range(1,21,2)从1 开始生成列表,到20结束(包含20),步长为2
    number.append(i)
for n in number:
    print(n,end=" ")
print(number)
print(min(number))
print(max(number))
print(sum(number))
number.reverse()
print(number)
1 3 5 7 9 11 13 15 17 19 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
1
19
100
[19, 17, 15, 13, 11, 9, 7, 5, 3, 1]
number = list(range(1, 21, 2))
print(number)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

4-7 3的倍数 :创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for 循环将这个列表中的数字都打印出来。

number =[]
for i in range(3,30):
    if i%3==0:
        number.append(i)
for n in number:
    print(n,end=" ")
print(number)
print(min(number))
print(max(number))
print(sum(number))
number.reverse()
print(number)
3 6 9 12 15 18 21 24 27 [3, 6, 9, 12, 15, 18, 21, 24, 27]
3
27
135
[27, 24, 21, 18, 15, 12, 9, 6, 3]
numbers = list(range(3,31,3))
for num in numbers:
	print(num,end=" ")
3 6 9 12 15 18 21 24 27 30 

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

number = [n**3 for n in range(1,11)] #在这个示例中,for 循环为for n in range(1,11) ,它将值 1~10提供给表达式n**3。
for i in number:
    print(i, end=" ")
print(number)
number =  list(range(1,11))
for i in [n**3 for n in number]:
    print(i,end=" ")
1 8 27 64 125 216 343 512 729 1000 

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

number = [n**3 for n in range(1,11)] #在这个示例中,for 循环为for n in range(1,11) ,它将值 1~10提供给表达式n**3。
for i in number:
    print(i, end=" ")
print(number)
1 8 27 64 125 216 343 512 729 1000 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
打印消息“Thefirst threeitems in thelistare:”,再使用切片来打印列表的前三个元素。
打印消息“Threeitems fromthe middle ofthelistare:”,再使用切片来打印列表中间的三个元素。
打印消息“Thelast threeitems in thelistare:”,再使用切片来打印列表末尾的三个元素。

Fruit = ["苹果","梨","橘子","火龙果","banana","Orange"]
print("Thefirst threeitems in thelistare:" + Fruit[:3])
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

 in 
      1 Fruit = ["苹果","梨","橘子","火龙果","banana","Orange"]
----> 2 print("Thefirst threeitems in thelistare:" + Fruit[:3])


TypeError: can only concatenate str (not "list") to str
zifu = "Things"
print("Thefirst threeitems in thelistare:" + zifu[:3])
print("Threeitems fromthe middle ofthelistare:" + zifu[2:5] )
print("Thelast threeitems in thelistare:" + zifu[-3:])
print("Thelast threeitems in thelistare:" + zifu[-1:-4:-1])
Thefirst threeitems in thelistare:Thi
Threeitems fromthe middle ofthelistare:ing
Thelast threeitems in thelistare:ngs
Thelast threeitems in thelistare:sgn
my_foods = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
print("The first three items in the list are:")
print(my_foods[:3])  # 4-10-1
print("\nThree items from the middle of the list are:")
print(my_foods[1:4])  # 4-10-2
print("\nThree items from the middle of the list are:")
print(my_foods[-3:])  # 4-10-3
The first three items in the list are:
['pizza', 'falafel', 'carrot cake']

Three items from the middle of the list are:
['falafel', 'carrot cake', 'cannoli']

Three items from the middle of the list are:
['carrot cake', 'cannoli', 'ice cream']

4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
在原来的比萨列表中添加一种比萨。 在列表friend_pizzas 中添加另一种比萨。 核实你有两个不同的列表。为此,打印消息“My favorite pizzasare:”,再使用一个for 循环来打印第一个列表;打印消息“My friend’s favorite pizzasare:”,再使用一 个for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。

my_foods = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
friend_pizzas = my_foods[:]
my_foods.append('cheese')  # 4-11-1
friend_pizzas.append('seafood')  # 4-11-2
for i in my_foods:
	print("My favorite pizzas are: " + i)
for i in friend_pizzas:
	print("My friend’s favorite pizzas are: " + i)
My favorite pizzas are: pizza
My favorite pizzas are: falafel
My favorite pizzas are: carrot cake
My favorite pizzas are: cannoli
My favorite pizzas are: ice cream
My favorite pizzas are: cheese
My friend’s favorite pizzas are: pizza
My friend’s favorite pizzas are: falafel
My friend’s favorite pizzas are: carrot cake
My friend’s favorite pizzas are: cannoli
My friend’s favorite pizzas are: ice cream
My friend’s favorite pizzas are: seafood
my_foods = ["水果","牛肉","海鱼","大闸蟹","核桃"]
friend_pizzas = my_foods[:]
#要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:] )。这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个 列表。
my_foods.append('鸡肉')  
friend_pizzas.append('猪肉') 
for i in my_foods:
	print("My favorite pizzas are: " + i)
for i in friend_pizzas:
	print("My friend’s favorite pizzas are: " + i)
print(my_foods[1:4]) 

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

HlepDinner = ("烤金针菇","骨肉相连","羊肉串","牛肉面","水果沙拉")
print(HlepDinner)
HlepDinner[0]="宫保鸡丁"
('烤金针菇', '骨肉相连', '羊肉串', '牛肉面', '水果沙拉')



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

 in 
      1 HlepDinner = ("烤金针菇","骨肉相连","羊肉串","牛肉面","水果沙拉")
      2 print(HlepDinner)
----> 3 HlepDinner[0]="宫保鸡丁"


TypeError: 'tuple' object does not support item assignment
HlepDinner = ("烤金针菇","骨肉相连",["烤青椒","冰淇淋","毛血旺"],"羊肉串","牛肉面","水果沙拉")
print(HlepDinner)
print(type(HlepDinner))
for i in HlepDinner:
    print(i,end=" ")
('烤金针菇', '骨肉相连', ['烤青椒', '冰淇淋', '毛血旺'], '羊肉串', '牛肉面', '水果沙拉')

烤金针菇 骨肉相连 ['烤青椒', '冰淇淋', '毛血旺'] 羊肉串 牛肉面 水果沙拉 
HlepDinner = ("烤金针菇","骨肉相连","羊肉串","牛肉面","水果沙拉")
HlepDinner = ("烤金针菇","骨肉相连",["烤青椒","冰淇淋","毛血旺"],"羊肉串","牛肉面","水果沙拉")
print(HlepDinner)
print(HlepDinner[-1:-5:-1])
HlepDinner =HlepDinner[-1:-5:-1] + ("烤红薯","涮肚","薯片")
print(HlepDinner)
('烤金针菇', '骨肉相连', ['烤青椒', '冰淇淋', '毛血旺'], '羊肉串', '牛肉面', '水果沙拉')
('水果沙拉', '牛肉面', '羊肉串', ['烤青椒', '冰淇淋', '毛血旺'])
('水果沙拉', '牛肉面', '羊肉串', ['烤青椒', '冰淇淋', '毛血旺'], '烤红薯', '涮肚', '薯片')

你可能感兴趣的:(Python,python,列表,面试,程序人生,经验分享)