python 字典元素的获取

# -*-coding:utf-8 -*-
# @Time :  11:53
# @Author: 黄荣津
# @File : 11.字典元素的获取.py
# @Software: PyCharm

'''获取字典的元素'''
scores={'张三':100, '李四':98, '王五':45}

#第一种方式,使用[]
print(scores['张三'])
# print(scores['皇后']) KeyError: '皇后'

#第二种方式,使用get()方法
print(scores.get('张三'))
print(scores.get('皇后'))  #None
print(scores.get('马丁',99)) #99是在查找马丁所对的value不存在的情况下提供的默认值



你可能感兴趣的:(笔记,python)