字典

  1. # 字典
  2. # 字典由键和值组成
  3. dict1 = {'name':'Python', 'Date':2001}
  4. dict2 = {12: 15, 3: '5'}
  5. print("dict1['name']:",dict1['name'])
  6. dict = {'Name': 'Phil', 'Age': 7, 'Class': 'First'}
  7. print("dict['Name']: ", dict['Name'])
  8. print("dict['Age']: ", dict['Age'])
  9. dict['Age'] = 8 # 更新 Age
  10. dict['School'] = "CCU" # 添加信息
  11. print("dict['Age']: ", dict['Age'])
  12. print("dict['School']: ", dict['School'])

字典需要键和值,键不可以重复,否则按照后来的定义取值

字典可以被更新和添加,也有函数可用