今天要跟大家介紹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
程式設計實作題
程式設計實作題