字典类型
字典类型:大括号内以逗号分割键值对(key(描述意义):value)的形式存储数据
dic1 = {'name':'bob','age':16,'height':140,'weight':180,'hobby_list':['debugging','sing','baleiwu']}
dic2 = dict()print(dic2) # {}
取值
print(dic1['name']) # bobprint(dic1['hobby_list'][2]) # baleiwuprint(dic1.get('name1')) # None # 如果没有key,则取None,可以自定义没有值能取到的值print(dic1.get('hobby_list')[2]) #baleiwu
常用方法
# 输出keyfor i in dic1: print(i)# 输出值for i in dic1.values(): print(i)# 输出键值对for i in dic1.items(): print(i)# 删除值dic1.pop('name')print(dic1) # 添加值dic1.setdefault('name','bob')print(dic1)