python學習打卡 Task3

2021-09-22 16:56:52 字數 2908 閱讀 8995

集合條件語句

迴圈語句

鍵必須不可變,所以可以用數字,字串或元組作為鍵,而列表不行

dic = 

print(dic['name'])

執行結果

python
dic['age'] = 100 		  #更改age的值

dic['date'] = '20190514' #增加date

print(dic)

執行結果

del dic['name']    #刪除name

print(dic)

dic.clear() #清空字典

print(dic)

執行結果

{}

dic = 

print(dic.items())

執行結果

dict_items([('name', 'python'), ('age', 10)])
print(dic.get('age'))
執行結果

10
print(dic.pop('name'))
執行結果

python
集合元素無序且不重複

集合運算

week = 

weekend =

print(week - weekend) #集合week中包含而集合weekend中不包含的元素

print(week & weekend) #集合week和weekend中都包含的元素

print(week ^ weekend) #不同時包含於兩個集合中的元素

執行結果

week.add('a week')

print(week)

執行結果

week.discard('a week')

print(week)

執行結果

a  = 

b =

print(a.union(b))

執行結果

print(a.isdisjoint(b))
執行結果

true
if condition_1:

statement_block_1

elif condition_2:

statement_block_2

else:

statement_block_3

import random

ran = random.randint(1,10)

num = int(input("請猜乙個1-10的整數:"))

if num > ran:

print("你猜大了!")

elif num < ran:

print("你猜小了!")

else:

print("恭喜你,猜對了!")

print('結果是:%d'%ran)

輸入7,執行結果

你猜大了!

結果是:3

x = 10

y = 20

print(x) if x>y else print(y)

執行結果

20
while 判斷條件:

語句

示例

n = 10

count = 1

sum = 0

while count <= n:

sum += count

count += 1

print(sum)

執行結果

55
count = 0

while count < 5:

print (count, " 小於 5")

count = count + 1

else:

print (count, " 大於或等於 5")

執行結果

0  小於 5

1 小於 5

2 小於 5

3 小於 5

4 小於 5

5 大於或等於 5

for in :

else:

示例

languages = ["c", "c++", "perl", "python"] 

for x in languages:

print (x)

執行結果

c

c++perl

python

for i in range(5):

print(i)

執行結果

012

34

python打卡 Task 3異常處理

try except 語句 try 檢測範圍 except exception as reason 出現異常後的處理 try except finally 語句 try 檢測範圍 except exception as reason 出現異常後的處理 finally 無論如何都會被執行的 try e...

python基礎 Task 3 異常處理

異常就是執行期檢測到的錯誤。計算機語言針對可能出現的錯誤定義了異常型別,某種錯誤引發對應的異常時,異常處理程式將被啟動,從而恢復程式的正常執行。try 檢測範圍 except exception as reason 出現異常後的處理 try 語句按照如下方式工作 例子 try f open test...

動手學深度學習Task3

過擬合 欠擬合及其解決方案 梯度消失 梯度 迴圈神經網路高階 1 過擬合和欠擬合 過擬合 模型的訓練誤差遠小於它在測試資料集上的誤差,我們稱該現象為過擬合 欠擬合 模型訓練誤差無法降低.如何應對欠擬合和過擬合?在這裡我們重點討論兩個因素 模型複雜度和訓練資料集大小。1.1.1 模型複雜度 為了解釋模...