python中字典的使用非常普遍,字典也有很多方法,今天遇到了字典的get方法,用法如下:

Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。

语法:

dict.get(key, default=None)
  • key -- 字典中要查找的键。
  • default -- 如果指定键的值不存在时,返回该默认值值。

for example:

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

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

“”“
以上实例输出结果为:

Value : 27
Value : Never
”“”

  

 

相关文章:

  • 2021-07-30
  • 2022-12-23
  • 2022-01-07
  • 2021-08-06
  • 2021-07-19
  • 2022-01-08
猜你喜欢
  • 2022-01-27
  • 2021-09-11
  • 2022-12-23
  • 2021-09-11
  • 2021-11-18
  • 2022-12-23
  • 2022-01-12
相关资源
相似解决方案