1.聲明一個字典保存一個學(xué)生的信息扔罪,學(xué)生信息中包括: 姓名、年齡、成績(單科)矗积、電話
student = {'name': '小馮', '年齡': 23, 'score': 66, 'tel': '112'}
2.聲明一個列表敞咧,在列表中保存6個學(xué)生的信息(6個題1中的字典)
students = [{'name': '小馮', '年齡': 23, 'score': 66, 'tel': '113'},
{'name': '小花', '年齡': 22, 'score': 57, 'tel': '114'},
{'name': '小白', '年齡': 21, 'score': 68, 'tel': '128'},
{'name': '小洪', '年齡': 11, 'score': 69, 'tel': '116'},
{'name': '小馬', '年齡': 13, 'score': 39, 'tel': '187'},
{'name': '小二', '年齡': 20, 'score': 66, 'tel': '118'}
]
? a.統(tǒng)計不及格學(xué)生的個數(shù)
count_low_socre = 0
for student in students:
if student['score'] < 60:
count_low_socre += 1
print(count_low_socre)
? b.打印不及格學(xué)生的名字和對應(yīng)的成績
for student in students:
if student['score'] < 60:
print(student['name'], student['score'])
? c.統(tǒng)計未成年學(xué)生的個數(shù)
count_minor_student = 0
for student in students:
if student['年齡'] < 18:
count_minor_student += 1
print(count_minor_student)
? d.打印手機(jī)尾號是8的學(xué)生的名字
for student in students:
if student['tel'][-1] == '8':
print(student['name'])
? e.打印最高分和對應(yīng)的學(xué)生的名字
max_score = 0
max_score_name = ''
for student in students:
score = student['score']
if score > max_score:
max_score = score
print(max_score)
for student in students:
if student['score'] == max_score:
print(student['name'])
? f.將列表按學(xué)生成績從大到小排序(掙扎一下,不行就放棄)
#按照順序取出分?jǐn)?shù)
for i in range(len(students)-1):
#每個循環(huán)中比較數(shù)
for j in range(len(students)-i-1):
if students[j]['score'] < students[j + 1]['score']:
students[j]['score'], students[j + 1]['score'] = students[j + 1]['score'], students[j]['score']
print(students)
3.用三個列表表示三門學(xué)科的選課學(xué)生姓名(一個學(xué)生可以同時選多門課)
science = ['小馮', '小花', '小馬', '小張']
english = ['小馮', '如花', '小紅','小張','小龍']
chemistry = ['小馮', '小光', '小李']
set_science = set(science)
set_english = set(english)
set_chemistry = set(chemistry)
? a. 求選課學(xué)生總共有多少人
count = 0
for _ in (set_science | set_chemistry | set_english):
count += 1
print(count)
b. 求只選了第一個學(xué)科的人的數(shù)量和對應(yīng)的名字
count = 0
for _ in (set_science - set_english - set_chemistry):
count += 1
print(count, set_science - set_english - set_chemistry)
? c. 求只選了一門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
count = 0
for _ in ((set_science ^ set_english ^ set_chemistry) - (set_english & set_chemistry & set_science)):
count += 1
print(count, ((set_science ^ set_english ^ set_chemistry) - (set_english & set_chemistry & set_science)))
d. 求只選了兩門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
count = 0
for _ in (set_science & set_english - set_chemistry)|( set_english & set_chemistry\
- set_science )|(set_chemistry & set_science - set_english ):
count += 1
print(count, (set_science & set_english - set_chemistry)|( set_english & set_chemistry\
- set_science )|(set_chemistry & set_science - set_english ))
? e. 求選了三門學(xué)生的學(xué)生的數(shù)量和對應(yīng)的名字
count = 0
for _ in (set_science & set_english & set_chemistry):
count += 1
print(count, set_science & set_english & set_chemistry)