字典类型
dict{}---字典类型
优点:能存储多个数据,能以键值对的方式存储数据
# 字典类型,定义方式,{}内以都好隔开 键值对,key(描述意义,一般使用字符串类型,不能使用列表):value(值,任意数据)
# 字典类型的优点就是能以键值对的方式去定义字典中的元素
info_dict = {'name':'tom',
'age':'18',
'sex':'man',
'height':'180',
'hobby_list':['music','run']}
# 输出info_dict字典中name的值,输出info_dict字典中hobby_list的值,输出info_dict字典中height的值
print(info_dict['name'],info_dict['hobby_list'],info_dict['height'])
# 输出:tom ['music', 'run'] 180
print(info_dict['hobby_list'][1])# 取出info_dict字典中hobby_list列表中的第二个元素
# 输出:run
# 字典类型的其他定义方式
dic = dict()
print(dic)
# 输出:{}
dic1 = {}
print(dic1)
# 输出:{}
# 字典的使用方法
info_dict['hp']=1000000 # 字典类型的传值,表示为info_dict字典传递一个名为hp,值为1000000的键值对
print(info_dict)
# 输出:{'name': 'tom', 'age': '18', 'sex': 'man', 'height': '180', 'hobby_list': ['music', 'run'], 'hp': 1000000}