Python 字典 Dictionary get()方法
Python 字典 Dictionary get()方法
Python 字典(Dictionary) get() 函數(shù)返回指定鍵的值。
語法
get()方法語法:
dict.get(key, default=None)
參數(shù)
- key -- 字典中要查找的鍵。
- default -- 如果指定鍵的值不存在時,返回該默認值。
返回值
返回指定鍵的值,如果鍵不在字典中返回默認值 None 或者設置的默認值。
實例
以下實例展示了 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 簡介
- Python 字典 Dictionary
- Python 文件I/O
- Python GUI編程 Tkinter
- Python 列表
- Python 詞典
- Python 搜索樹
- Python 算法類
- Python3 for while 循環(huán)語句
- Python3 正則表達式
- Python3 網(wǎng)絡編程
- Python3 XML 解析
- Python round() 函數(shù)
- Python sqrt() 函數(shù)
- Python seed() 函數(shù)
- Python File flush() 方法
- Python os.rmdir() 方法
- Python decode()方法
- Python startswith()方法
- Python List remove()方法