字典类型
字典类型:大括号内以逗号分割键值对(key(描述意义):value)的形式存储数据
dic1 = {'name':'bob','age':16,'height':140,'weight':180,'hobby_list':['debugging','sing','baleiwu']}
dic2 = dict()
print(dic2) # {}
取值
print(dic1['name']) # bob
print(dic1['hobby_list'][2]) # baleiwu
print(dic1.get('name1')) # None
# 如果没有key,则取None,可以自定义没有值能取到的值
print(dic1.get('hobby_list')[2]) #baleiwu
常用方法
# 输出key
for 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)