创建字典
>>> names={'Tom':'Tom is very good.','Jack':'Jack is very tall.'}
>>> names
{'Tom': 'Tom is very good.', 'Jack': 'Jack is very tall.'}
>>>
字典查询
>>> print(names['Jack'])
Jack is very tall.
>>>
在字典查询的过程中遇到没有的key,我们可能会遇到程序出现崩溃的情况,所以查询的时候更加建议用get()方法查询,代码示例:
>>> print(names.get('Mary'))
None
>>>
字典get()自定义返回值
>>> names.get('Mary','There is no such person.')
'There is no such person.'
>>>
568

被折叠的 条评论
为什么被折叠?



