高階程式設計技術 第二週作業

2021-08-17 05:03:42 字數 1575 閱讀 2600

本週的主要內容是list和tuple的使用以及其基礎操作。

3-1、3-2:list的建立、查詢

names = ['king', 'water sheep', 'ty']

for i in names:

print(i + ', what \'s up?')

3-8、3-11:list的簡單處理以及索引錯誤

place_of_interest = ['brandenburg', 'île-de-france', 'prague', 'hechingen', 'vienna']

print(place_of_interest)

print(sorted(place_of_interest))

print(place_of_interest)

print(sorted(place_of_interest, reverse=true))

print(place_of_interest)

place_of_interest.reverse()

print(place_of_interest)

place_of_interest.reverse()

print(place_of_interest)

place_of_interest.sort()

print(place_of_interest)

place_of_interest.sort(reverse=true)

print(place_of_interest)

4-4、4-5:生成乙個list並計算總和

arr = 

for i in range(1, 1000001):

print(min(i))

print(max(i))

print(sum(i))

4-7、4-9:range()的第三個引數以及解析

arr = 

for i in range(3, 31, 3):

print(arr)

arr = [i**2 for i in range(1, 11)]

print(arr)

4-13:tuple的基礎操作

food = ('french fries', 'hamburge', 'bacon', 'sandwich', 'fish and chips')

for i in food:

print(i.title())

food = ('potato', 'ubi potato', 'bacon', 'sandwich', 'fish and chips')

for i in food:

print(i.title())

food[-1] = 'cola'

最後一行執行的時候會報錯並返回以下資訊:

typeerror: 'tuple' object does not support item assignment
由此可見tuple的item是不可以進行修改的。

高階程式設計技術作業 5

題目描述 使用乙個for迴圈列印數字1 20 包含 展示 for number in range 1,21 print number input null output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 題目描述 通過給函式rang...

高階程式設計技術作業 7

題目描述 使用乙個字典來儲存一些人喜歡的數字。請想5個人的名字,並將這些名字用作字典中 的鍵 想出每個人喜歡的乙個數字,並將這些數字作為值儲存在字典中。列印每個人的名字和喜歡 的數字。展示 dic for name,number in dic.items print name str number ...

高階程式設計技術作業 8

題目描述 編寫乙個迴圈,提示使用者輸入一系列的比薩配料,並在使用者輸入 quit 時結束迴圈。每當 使用者輸入一種配料後,都列印一條訊息,說我們會在比薩中新增這種配料。input potato tomato fish quit output please input an ingredient we...