第一期 Python 程式設計入門共學營作業任務七


今天要跟大家介紹Python dict物件的一個方法dict.get()
以下是官方文件的介紹:

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

描述
Python 字典dict.get()函數返回指定的值,如果值不在字典中返回默認值

語法

dict.get(key, default=None)

以下是一個使用範例:

K = {'Name': 'Kevin', 'Age': 31}

print ("Age 值為 : %s" %  K.get('Age'))
print ("Sex 值為 : %s" %  K.get('Sex', "NA"))
#以上輸出為
Age 值為 : 31   
Sex 值為 : NA

程式設計實作題
程式設計實作題

#第一期Python程式設計入門共學營






你可能感興趣的文章

Blank line in trello card

Blank line in trello card

JS中的淺拷貝和深拷貝

JS中的淺拷貝和深拷貝

[Math] 平方根(square root)範例

[Math] 平方根(square root)範例






留言討論