1. 程式人生 > >python從入門到實踐第四章課後作業

python從入門到實踐第四章課後作業

'''
4-1 比薩:想出至少三種你喜歡的比薩,將其名稱儲存在一個列表中,再使用 for
迴圈將每種比薩的名稱都打印出來。
 修改這個 for 迴圈,使其列印包含比薩名稱的句子,而不僅僅是比薩的名稱。對
於每種比薩,都顯示一行輸出,如“I like pepperoni pizza”。
 在程式末尾新增一行程式碼,它不在 for 迴圈中,指出你有多喜歡比薩。輸出應包
含針對每種比薩的訊息,還有一個總結性句子,如“I really love pizza!”。
'''

pizzas = ['apple','banana','pear']

for pizza in pizzas:
	print(pizza+' ',end = '')
print()

for pizza in pizzas:
	print('I like '+pizza + ' pizza!')

print('I like pizza very much so that I would like to eat it every meal')
print('Apple pizza is delicious, banana pizza is fantastic, pear is my favourite.')
print('I really like pizza')

'''
4-2 動物:想出至少三種有共同特徵的動物,將這些動物的名稱儲存在一個列表中,
再使用 for 迴圈將每種動物的名稱都打印出來。
 修改這個程式,使其針對每種動物都列印一個句子,如“A dog would make a great
pet”。
 在程式末尾新增一行程式碼,指出這些動物的共同之處,如列印諸如“Any of these
animals would make a great pet!”這樣的句子。
'''

pets = ['dog','cat','rabbits']


for pet in pets:
	print('A '+pet + ' would make a great pet!')

print('Any of these animals are so cute!')

'''
4-3 數到 20:使用一個 for 迴圈列印數字 1~20(含)。
'''

for num in range(1,21):
	print(str(num))

'''
4-4 一百萬:建立一個列表,其中包含數字 1~1 000 000,再使用一個 for 迴圈將這
些數字打印出來(如果輸出的時間太長,按 Ctrl + C 停止輸出,或關閉輸出視窗)
'''

arrayNum = [value for value in range(1,1000001)]

for Num in arrayNum:
	print(Num)

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

arrayNum = [value for value in range(1,1000001)]
print(min(arrayNum))
print(max(arrayNum))
print(sum(arrayNum))

'''
4-6 奇數:通過給函式 range()指定第三個引數來建立一個列表,其中包含 1~20 的
奇數;再使用一個 for 迴圈將這些數字都打印出來。
'''

arrayNum = range(1,21,2)
for num in arrayNum:
	print(str(num)+' ',end='')

'''
4-7 3 的倍數:建立一個列表,其中包含 3~30 內能被 3 整除的數字;再使用一個 for
迴圈將這個列表中的數字都打印出來。
'''

values = range(3,31,3)
for value in values:
	print(str(value)+' ',end = '')

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

values=[]

for value in range(1,11):
	values.append(value**3)

for value in values:
	print(str(value)+' ',end='')

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


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

for value in values:
	print(str(value)+' ',end='')

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

pets = ['dog','cat','rabbits','hen','birds']


for pet in pets:
	print('A '+pet + ' would make a great pet!')

print('Any of these animals are so cute!')

print('The first three items in the list are:')
print(pets[:3])

print('Three items from the middle of the list are:')
print(pets[1:4])

print('The last three items in the list are:')
print(pets[-3:])

'''
4-11 你的比薩和我的比薩:在你為完成練習 4-1 而編寫的程式中,建立比薩列表的
副本,並將其儲存到變數 friend_pizzas 中,再完成如下任務。
 在原來的比薩列表中新增一種比薩。
 在列表 friend_pizzas 中新增另一種比薩。
 核實你有兩個不同的列表。為此,列印訊息“My favorite pizzas are:”,再使用一
個 for 迴圈來列印第一個列表;列印訊息“My friend’s favorite pizzas are:”,再使
用一個 for 迴圈來列印第二個列表。核實新增的比薩被新增到了正確的列表中。
'''

pizzas = ['apple','banana','pear']
friend_pizzas = pizzas[:]

pizzas.append('peach')
friend_pizzas.append('lemon')

print('My favorite pizzas are:')
for pizza in pizzas:
	print(pizza+', ',end='')
print()

print('My friend’s favorite pizzas are:')
for pizza in friend_pizzas:
	print(pizza+', ',end='')
print()

'''
4-12 使用多個迴圈:在本節中,為節省篇幅,程式 foods.py 的每個版本都沒有使用
for 迴圈來列印列表。請選擇一個版本的 foods.py,在其中編寫兩個 for 迴圈,將各個
食品列表都打印出來
'''

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
for pizza in my_foods:
	print(pizza+' ',end='')
print()

print("\nMy friend's favorite foods are:")
for pizza in friend_foods:
	print(pizza+' ',end='')
print()

'''
4-13 自助餐:有一家自助式餐館,只提供五種簡單的食品。請想出五種簡單的食
品,並將其儲存在一個元組中。
 使用一個 for 迴圈將該餐館提供的五種食品都打印出來。
 嘗試修改其中的一個元素,核實 Python 確實會拒絕你這樣做。
 餐館調整了選單,替換了它提供的其中兩種食品。請編寫一個這樣的程式碼塊:
給元組變數賦值,並使用一個 for 迴圈將新元組的每個元素都打印出來
'''

foods = ('cake','sushi','BBQ','fish','Porridge')

for food in foods:
	print(food+' ',end='')
print()

'''
food[0] = 'bear'

'''

foods =('cake','sushi','BBQ','Noodles','soup')
for food in foods:
	print(food+' ',end='')
print()