PythonCrashCourse 第四章習題


Python 從入門到實踐第四章習題

4.1想出至少三種你喜歡的比薩,將其名稱存儲在一個列表中,再使用for 循環將每種比薩的名稱都打印出來

  • 修改這個for 循環,使其打印包含比薩名稱的句子,而不僅僅是比薩的名稱。對於每種比薩,都顯示一行輸出,如“I like pepperoni pizza”。

  • 在程序末尾添加一行代碼,它不在for 循環中,指出你有多喜歡比薩。輸出應包含針對每種比薩的消息,還有一個總結性句子,如“I really love pizza!”。

pizzas = ['Neapolitan pizza','pepperoin pizza','pan pizza']
for pizza in pizzas:
	print(f"I like {pizza}")
for pizza in pizzas:
	print("I really like " + pizza +"very much")
print("I really love pizza")

4.2想出至少三種有共同特征的動物,將這些動物的名稱存儲在一個列表中,再使用for 循環將每種動物的名稱都打印出來

  • 修改這個程序,使其針對每種動物都打印一個句子,如“A dog would make a great pet”。
  • 在程序末尾添加一行代碼,指出這些動物的共同之處,如打印諸如“Any of these animals would make a great pet!”這樣的句子。
pets = ['dog','cat','tortoise']
for pet in pets:
	print(f"A {pet} would make a great pet")
print("\nAny of these animals would make a great pet!")

4.3使用一個for 循環打印數字1~20(含)

for value in range(1,21):
	print(value)

#method two
values = [value for value in range(1,21)]
print(values)

4.4 創建一個列表,其中包含數字1~1 000 000,再使用一個for 循環將這些數字打印出來(如果輸出的時間太長,按Ctrl + C停止輸出,或關閉輸出窗口)

for value in range(1,10 ** 6 +1):
	print(value)

4.5創建一個列表,其中包含數字1~1 000 000,再使用min() 和max() 核實該列表確實是從1開始,到1 000 000結束的。另外,對這個列表 調用函數sum() ,看看Python將一百萬個數字相加需要多長時間

values = [value for value in range(1,10 ** 6 +1)]
print(min(values))
print(max(values))
print(sum(values))

4.6 通過給函數range() 指定第三個參數來創建一個列表,其中包含1~20的奇數;再使用一個for 循環將這些數字都打印出來

odd_numbers = [value for value in range(1,20,2)]
print(odd_numbers)

4.7創建一個列表,其中包含3~30內能被3整除的數字;再使用一個for 循環將這個列表中的數字都打印出來

three_numbers = [value for value in range(3,30,3)]
print(three_numbers)

#method two
for value in range(3,30,3):
	print(value)

4.8 將同一個數字乘三次稱為立方。例如,在Python中,2的立方用2**3 表示。請創建一個列表,其中包含前10個整數(即1~10)的立方,再使用一個for 循 環將這些立方數都打印出來

cubes = []
for value in range(1,11):
	cube = value ** 3
	cubes.append(cube)
print(cubes)

4.9 使用列表解析生成一個列表,其中包含前10個整數的立方

cubes = [value ** 3 for value in range(1,11)]
print(cubes)

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:”,再使用切片來打印列表末尾的三個元素。
players = ['charles','martina','michael','florence','eli']

print("The first three items in the list are:")
three_players = players[:3]
print(three_players)

print("\nThree items from the middle of the list are:")
# print(int((len(players)-3)/2))
# print(int((len(players)-3)/2)+3)
three_players2 = players[int((len(players)-3)/2):int((len(players)-3)/2)+3]
print(three_players2)

print("\nThe last three items in the list are:")
print(players[-3:])

4.11在你為完成練習4-1而編寫的程序中,創建比薩列表的副本,並將其存儲到變量friend_pizzas 中,再完成如下任務。

  • 在原來的比薩列表中添加一種比薩。
  • 在列表friend_pizzas 中添加另一種比薩。
  • 核實你有兩個不同的列表。為此,打印消息“My favorite pizzas are:”,再使用一個for 循環來打印第一個列表;打印消息“My friend's favorite pizzas are:”,再使用一 個for 循環來打印第二個列表。核實新增的比薩被添加到了正確的列表中。
pizzas = ['Neapolitan pizza','pepperoin pizza','pan pizza']
friend_pizzas = pizzas[:]
pizzas.append("Domino's Pizza")
friend_pizzas.append("ROC Pizza")
print("My favorite pizzas are:")
for pizza in pizzas:
	print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in friend_pizzas:
	print(pizza)

4.12在本節中,為節省篇幅,程序foods.py的每個版本都沒有使用for 循環來打印列表。請選擇一個版本的foods.py,在其中編寫兩個for 循環,將各個食品列表都打印出來

my_foods = ['pizza','falafel','carrot','cake']
friend_foods = my_foods[:]
my_foods.append("cannoli")
friend_foods.append("ice cream")

print("My favorite foods are:")
for food in my_foods:
	print(food)

print("\nMy friend's favorite foods are:")
for food in friend_foods:
	print(food)

4.13有一家自助式餐館,只提供五種簡單的食品。請想出五種簡單的食品,並將其存儲在一個元組中

  • 使用一個for 循環將該餐館提供的五種食品都打印出來。
  • 嘗試修改其中的一個元素,核實Python確實會拒絕你這樣做。
  • 餐館調整了菜單,替換了它提供的其中兩種食品。請編寫一個這樣的代碼塊:給元組變量賦值,並使用一個for 循環將新元組的每個元素都打印出來。
foods = ('pizza','falafel','carrot','cake','ice cream')
for food  in foods:
    print(food)
# foods[2] ='cannoli' 核實Python確實會拒絕這樣做
print("\nModified the menu")
foods =('pizza','falafel','cake','cannoli','tomato')
for food in foods:
    print(food)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM