# 案例:定义一个变量保存一个班级的信息
class1 ={'name':'python2301','location':'21教','lecturer':{'name':'余婷','age':18,'gender':'女','tel':'13678192302'},'headTeacher':{'name':'萍姐','age':18,'tel':'110'},'all_student':[{'name':'stu1','age':17,'tel':'119','专业':'电子商务','score':78,'linkman':{'name':'张三','tel':'82223111'}},{'name':'stu2','age':23,'tel':'8293','专业':'计算机软件','score':100,'linkman':{'name':'李四','tel':'829933'}},{'name':'stu3','age':30,'tel':'918111','专业':'数学','score':96,'linkman':{'name':'小明','tel':'11929203'}},{'name':'stu4','age':16,'tel':'827222','专业':'数学','score':99,'linkman':{'name':'王五','tel':'8628101'}}]}print('---------------------------------------华丽的分割线------------------------------------')# 1) 获取班级名字print(class1['name'])# 2)获取班级的位置print(class1['location'])# 3)获取讲师的名字# print(class1['lecturer']['name'])
lecturer = class1['lecturer']print(lecturer['name'])# 4)获取班主任的电话print(class1['headTeacher']['tel'])# 5)获取第一个学生的专业print(class1['all_student'][0]['专业'])print('---------------------------------------华丽的分割线------------------------------------')# 6)获取所有学生的名字
all_student = class1['all_student']for x in all_student:print(x['name'])print('---------------------------------------华丽的分割线------------------------------------')# 7)获取所有学生的联系人名字for x in all_student:print(x['linkman']['name'])print('---------------------------------------华丽的分割线------------------------------------')# 8)获取未成年学生的联系人电话for x in all_student:if x['age']<18:print(x['linkman']['tel'])print('---------------------------------------华丽的分割线------------------------------------')
# 2. 遍历(了解)"""
for 变量 in 字典:
循环体(变量依次获取到的是字典的键)
"""
dog ={'name':'财财','age':3,'color':'黄色','breed':'土狗'}for key in dog:print('key:', key,'value:', dog[key])