```python

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }
print(dict)
print(dict1)
print(dict2)
{'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
{'abc': 456}
{'abc': 123, 98.6: 37}
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
dict['Name']: Runoob
dict['Age']: 7
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

print ("dict['Alice']: ", dict['Alice'])
---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

<ipython-input-4-726b75248a57> in <module>
      1 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
      2 
----> 3 print ("dict['Alice']: ", dict['Alice'])


KeyError: 'Alice'
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

dict['Age'] = 8 # 更新 Age
dict['School'] = "菜鸟教程" # 添加信息


print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
dict['Age']: 8
dict['School']: 菜鸟教程
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

del dict['Name'] # 删除键 'Name'
dict.clear() # 清空字典
del dict # 删除字典

print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-6-acbbe1348922> in <module>
      5 del dict # 删除字典
      6 
----> 7 print ("dict['Age']: ", dict['Age'])
      8 print ("dict['School']: ", dict['School'])


TypeError: 'type' object is not subscriptable
dict = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鸟'}

print ("dict['Name']: ", dict['Name'])
dict['Name']: 小菜鸟
dict = {['Name']: 'Runoob', 'Age': 7}

print ("dict['Name']: ", dict['Name'])
---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-8-c4866a957fa9> in <module>
----> 1 dict = {['Name']: 'Runoob', 'Age': 7}
      2 
      3 print ("dict['Name']: ", dict['Name'])


TypeError: unhashable type: 'list'
{'pear', 'banana', 'orange', 'apple'}





{'b', 'd', 'l', 'm', 'r', 'z'}

posted on 2019-09-15 06:29  柳条儿  阅读(241)  评论(0编辑  收藏  举报

导航