example_dict = {"key1":"value1", "key2":"value2", "key3":"value3"}
首先创建一个字典用的是大括号
fruit_or_veget
{'apple': 'fruit', 'carrot': 'vegetable', 'banana': 'fruit', 'grape': 'fruit', 'onion': 'veget'}
一个方括号不行
fruit_or_veget.update['caomei','fruit']
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
fruit_or_veget.update['caomei','fruit']
TypeError: 'builtin_function_or_method' object is not subscriptable
一个圆括号不行
fruit_or_veget.update('camei','fruit')
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
fruit_or_veget.update('camei','fruit')
TypeError: update expected at most 1 argument, got 2
一个方括号加一个圆括号不行
fruit_or_veget.update[('camei','fruit')]
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
fruit_or_veget.update[('camei','fruit')]
TypeError: 'builtin_function_or_method' object is not subscriptable
用圆+方+圆括号成功,哪怕只加一个
fruit_or_veget.update([('camei','fruit')])
fruit_or_veget
{'apple': 'fruit', 'carrot': 'vegetable', 'banana': 'fruit', 'grape': 'fruit', 'onion': 'veget', 'camei': 'fruit'}
查找字典中的一个变量用方括号
name_grades('ed')
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
name_grades('ed')
TypeError: 'dict' object is not callable
name_grades['ed']
'90'
词典的键必须是不可变对象。词典的值可以是任何对象。