python從入門到實踐課後習題第四章
阿新 • • 發佈:2018-12-12
""" 4-1 比薩:想出至少三種你喜歡的比薩,將其名稱儲存在一個列表中,再使用for迴圈將每種比薩的名稱都打印出來。 修改這個 for 迴圈,使其列印包含比薩名稱的句子,而不僅僅是比薩的名稱。對於每種比薩,都顯示一行輸出, 如 “I like pepperoni pizza” 。在程式末尾新增一行程式碼,它不在 for 迴圈中,指出你有多喜歡比薩。 輸出應包含針對每種比薩的訊息,還有一個總結性句子,如 “I really love pizza!” 。 """ pizzas = ['chicken', 'durian', 'tomato'] for pizza in pizzas: print("I like " + pizza + " pizza!~") 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 = ['cat', 'dog', 'pig'] for pet in pets: print("A " + pet + " would make a great pet!") print("Any of these animals would make a great pet")
""" 4-3 數到 20 :使用一個 for 迴圈列印數字 1~20 (含)。 """ nums = list(range(1, 21)) # 直接生成一個1-20的數值列表 print(nums) for num in nums: print(num)
""" 4-4 建立一個列表,其中包含數字 1~1 000 000 ,再使用一個 for 迴圈將這些數字打印出來 (如果輸出的時間太長,按 Ctrl + C 停止輸出,或關閉輸出視窗)。 """ nums = list(range(1, 1000001)) for num in nums: print(num)
""" 4-5 計算 1~1 000 000 的總和 :建立一個列表,其中包含數字 1~1 000 000 ,再使用 min() 和 max() 核實該列表確實是從 1 開始,到 1 000 000 結束的。另外,對這個列表 呼叫函式 sum() ,看看 Python 將一百萬個數字相加需要多長時間。 """nums = list(range(1, 1000001)) print(min(nums)) print(max(nums)) print(sum(nums))
""" 4-6 奇數 :通過給函式 range() 指定第三個引數來建立一個列表, 其中包含 1~20 的奇數;再使用一個 for 迴圈將這些數字都打印出來。 """ nums = list(range(1, 21, 2)) print(nums) for num in nums: print(num)
""" 4-7 3 的倍數 :建立一個列表,其中包含 3~30 內能被 3 整除的數字; 再使用一個 for 迴圈將這個列表中的數字都打印出來。 """ nums = list(range(3, 30, 3)) print(nums) for num in nums: print(num)
""" 4-8 立方 :將同一個數字乘三次稱為立方。例如,在 Python 中, 2 的立方用 2**3 表示。請建立一個列表, 其中包含前 10 個整數(即 1~10 )的立方,再使用一個 for 迴圈將這些立方數都打印出來。 """ null = [] # 建立一個空列表用於接收階乘立方後的值 nums = range(1, 11) # 建立一個1-10的數值列表 for value in nums: # 遍歷數值列表並將每個數值階乘立方 num = value ** 3 null.append(num) # 將階乘立方後的值新增進空列表並列印 print(null)
""" 4-9 立方解析 :使用列表解析生成一個列表,其中包含前 10 個整數的立方。 """ blank_list = [] for value in range(1, 10): num = value ** 3 blank_list.append(num) print(blank_list)
""" 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:” ,再使用切片來列印列表末尾的三個元素。 """ food = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream'] print("The first three items in the list are:") print(food[:3]) print("Three items from the middle of the list are:") print(food[2:5]) print("The last three items in the list are:") print(food[-3:])
""" 4-11 你的比薩和我的比薩 :在你為完成練習 4-1 而編寫的程式中,建立比薩列表的副本,並將其儲存到變數 friend_pizzas 中, 再完成如下任務。在原來的比薩列表中新增一種比薩。在列表 friend_pizzas 中新增另一種比薩,核實你有兩個不同的列表。 為此,列印訊息 “My favorite pizzas are:” ,再使用一個 for 迴圈來列印第一個列表; 列印訊息 “My friend's favorite pizzas are:” ,再使用一個 for 迴圈來列印第二個列表。 核實新增的比薩被新增到了正確的列表中。 """ pizzas = ['chicken', 'strawberry', 'beef', 'blueberries'] friend_pizzas = pizzas[:] pizzas.append("fruit") friend_pizzas.append("egg") #校驗pizzas是否與friend_pizzas相同 if pizzas == friend_pizzas: print("True") else: print("False") print("My favorite pizzas are:") for pizza in pizzas: print(pizza) print("My friend's favorite pizzas are:") for friend_pizza in friend_pizzas: print(friend_pizza)
""" 4-12 使用多個迴圈 :在本節中,為節省篇幅,程式 foods.py 的每個版本都沒有使用 for 迴圈來列印列表。 請選擇一個版本的 foods.py ,在其中編寫兩個 for 迴圈,將各個食品列表都打印出來 """ my_foods = ['pizza', 'falafel', 'carrot cake'] # 將my_foods的值全部賦值給friend_foods 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 friend_food in friend_foods: print(friend_food)
""" 4-13 自助餐 :有一家自助式餐館,只提供五種簡單的食品。請想出五種簡單的食品,並將其儲存在一個元組中。 使用一個 for 迴圈將該餐館提供的五種食品都打印出來。 嘗試修改其中的一個元素,核實 Python 確實會拒絕你這樣做。 餐館調整了選單,替換了它提供的其中兩種食品。請編寫一個這樣的程式碼塊: 給元組變數賦值,並使用一個 for 迴圈將新元組的每個元素都打印出來。 """ foods = ('pizza', 'falafel', 'carrot cake', 'beef', 'rice') # 執行下方程式碼可驗證元組不能夠修改 # foods[0] = 'milk' print(foods) foods = ('beef', 'rice') print(foods)