69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

Python 字典 Dictionary get()方法

Python 字典 Dictionary get()方法

Python 字典 DictionaryPython 字典 Dictionary

Python 字典(Dictionary) get() 函數(shù)返回指定鍵的值。

 

語法

get()方法語法:

dict.get(key, default=None)

 

參數(shù)

  • key -- 字典中要查找的鍵。
  • default -- 如果指定鍵的值不存在時,返回該默認值。

 

返回值

返回指定鍵的值,如果鍵不在字典中返回默認值 None 或者設(shè)置的默認值。

 

實例

以下實例展示了 get()函數(shù)的使用方法:

#!/usr/bin/python

dict = {'Name': 'codebaoku', 'Age': 27}

print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Sex', "Not Available")    

以上實例輸出結(jié)果為:

Value : 27
Value : Not Available

Python 字典 DictionaryPython 字典 Dictionary

下一節(jié):Python 字典(Dictionary) has_key()方法

Python 教程

相關(guān)文章