第四章作業
4-1 比薩 : 想出至少是三種你喜歡的比薩,將其名稱存儲在一個列表中,再使用for循環將每種比薩的名稱都打印出來。
修改這個for循環,使其打印包含比薩名稱的矩陣,而不僅僅是比薩的名稱。對於每種比薩,都顯示一行輸出,如“I like pepperoni pizza”。
在程序末尾添加一行代碼,它不在for循環中,指出你有多喜歡比薩。輸出應包含針對每種比薩的消息,還有一個總結性矩陣,如“I really love pizza!”
pizza = ['banana','durian','apple'] for x in pizza: print("I like %s pizza"%x) print("I really love Pizza")
輸出:
I like banana pizza
I like durian pizza
I like apple pizza
I really love Pizza
4-2 動物 :想出至少三種有共同特征的動物,將這些動物的名稱存儲在一個列表中,再使用for 循環將每種動物的名稱都打印出來。
修改這個程序,使其針對每種動物都打印一個句子,如“A dog would make a great pet”。
在程序末尾添加一行代碼,指出這些動物的共同之處,如打印諸如“Any of these animals would make a great pet!”這樣的句子。
animals = ['dog','cat','mouse'] for x in animals: print("A %s would make a great pet"%x) print("Any of these animals would make a great pets")
輸出:
A dog would make a great pet
A cat would make a great pet
A mouse would make a great pet
Any of these animals would make a great pets
4-3 數到20 :使用一個for 循環打印數字1~20(含)。
numbers = list(range(1,21)) for num in numbers: print(num)
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
4-4 一百萬 :創建一個列表,其中包含數字1~1 000 000,再使用一個for 循環將這些數字打印出來(如果輸出的時間太長,按Ctrl + C停止輸出,或關閉輸出窗口)。
numbers = list(range(1,1000000)) for num in numbers: print(num)
4-5 計算1~1 000 000的總和 :創建一個列表,其中包含數字1~1 000 000,再使用min() 和max() 核實該列表確實是從1開始,到1 000 000結束的。另外,對這個列表 調用函數sum() ,看看Python將一百萬個數字相加需要多長時間。
numbers = list(range(1,1000001)) print(min(numbers)) print(max(numbers)) print(sum(numbers))
輸出:
1 1000000 500000500000
4-6 奇數 :通過給函數range() 指定第三個參數來創建一個列表,其中包含1~20的奇數;再使用一個for 循環將這些數字都打印出來。
numbers = list(range(1,21,2)) for num in numbers: print(num)
輸出:
1 3 5 7 9 11 13 15 17 19
4-7 3的倍數 :創建一個列表,其中包含3~30內能被3整除的數字;再使用一個for 循環將這個列表中的數字都打印出來。
numbers = list(range(3,31,3)) for num in numbers: print(num)
輸出:
3 6 9 12 15 18 21 24 27 30
4-8 立方 :將同一個數字乘三次稱為立方。例如,在Python中,2的立方用2**3 表示。請創建一個列表,其中包含前10個整數(即1~10)的立方,再使用一個for 循 環將這些立方數都打印出來。
numbers = list(range(1,10)) result =[] for num in numbers: result.append(num**3) for num in result: print(num)
輸出:
1 8 27 64 125 216 343 512 729
4-9 立方解析 :使用列表解析生成一個列表,其中包含前10個整數的立方
result = [num**3 for num in range(1,11)] for x in result: print(x)
輸出:
1 8 27 64 125 216 343 512 729 1000
4-10 切片 : 選擇你在本章編寫的一個程序, 在末尾添加幾行代碼, 以完成如下任務。
打印消息“The first three items in the list are:”, 再使用切片來打印列表的前三個元素。
打印消息“Three items fromthe middle ofthe list are:”, 再使用切片來打印列表中間的三個元素。
打印消息“The last three items in the list are:”, 再使用切片來打印列表末尾的三個元素。
4-11 你的比薩和我的比薩 : 在你為完成練習 4-1而編寫的程序中, 創建比薩列表的副本, 並將其存儲到變量friend_pizzas 中, 再完成如下任務。
在原來的比薩列表中添加一種比薩。
在列表friend_pizzas 中添加另一種比薩。
核實你有兩個不同的列表。 為此, 打印消息“My favorite pizzas are:”, 再使用一個for 循環來打印第一個列表; 打印消息“My friend's
favorite pizzas are:”, 再使用一
個for 循環來打印第二個列表。 核實新增的比薩被添加到了正確的列表中。
4-12 使用多個循環 : 在本節中, 為節省篇幅, 程序foods.py的每個版本都沒有使用for 循環來打印列表。
請選擇一個版本的foods.py, 在其中編寫兩個for 循環, 將各
個食品列表都打印出來
答:4-10
items= list(range(1,6)) print('“The first three items in the list are:”') print(items[0:3]) print('\n“Three items fromthe middle ofthe list are:”') print(items[1:4]) print('\n“The last three items in the list are:”') print(items[-3:])
輸出:
“The first three items in the list are:” [1, 2, 3] “Three items fromthe middle ofthe list are:” [2, 3, 4] “The last three items in the list are:” [3, 4, 5]
4-11
friend_pizzas = pizza[:] friend_pizzas.append('pumkin') print("My favorite pizzas are:") for pi in pizza: print(pi) print("My friend's favorite pizzas are:")#打/n后無法打印,可能循環就這樣,是\n不是/n for pi in friend_pizzas: print(pi)
輸出;
My favorite pizzas are: banana durian apple My friend's favorite pizzas are: banana durian apple pumkin
4-12
my_foods = ['pizza','falafel','carrot cake'] friend_food = my_foods[:] print("My favorite foods are:") for food in my_foods: print(food) print("\nMy friend's favorite foods are:") for food in friend_food: print(food)
輸出:
My favorite foods are: pizza falafel carrot cake My friend's favorite foods are: pizza falafel carrot cake
4-13 自 助餐 : 有一家自助式餐館, 只提供五種簡單的食品。 請想出五種簡單的食品, 並將其存儲在一個元組中。
使用一個for 循環將該餐館提供的五種食品都打印出來。
嘗試修改其中的一個元素, 核實Python確實會拒絕你這樣做。
餐館調整了菜單, 替換了它提供的其中兩種食品。 請編寫一個這樣的代碼塊: 給元組變量賦值, 並使用一個for 循環將新元組的每個元素都打印出來。
foods = ('tea','meat','cocala','cake','water') print(foods) for food in foods: print(food) #foods[0] = ('apple')#無法修改元組中的一個元素 foods= ['milk','orange','cocala','cake','water'] for food in foods: print(food)
輸出:
tea
meat
cocala
cake
water
milk
orange
cocala
cake
water
4-14 PEP 8 : 請訪問https://python.org/dev/peps/pep-0008/ , 閱讀PEP 8格式設置指南。 當前, 這些指南適用的不多, 但你可以大致瀏覽一下。
4-15 代碼審核 : 從本章編寫的程序中選擇三個, 根據PEP 8指南對它們進行修改。
每級縮進都使用四個空格。 對你使用的文本編輯器進行設置, 使其在你按Tab鍵時都插入四個空格; 如果你還沒有這樣做, 現在就去做吧(有關如何設置, 請參
閱附錄B) 。
每行都不要超過80字符。 對你使用的編輯器進行設置, 使其在第80個字符處顯示一條垂直參考線。
不要在程序文件中過多地使用空行。
答:4-14略
4-15 略