python第二周day3

本文介绍了Python第二周day3的学习内容,主要包括字典的更新版作业、字典的操作和方法,如不支持的比较操作、键的检查、字典的转换等,以及集合的概念、增删查操作和数学集合运算,如并集、交集、差集和对称差集等。
摘要由CSDN通过智能技术生成

python第二周day3(9.24)

1、day7字典作业更改版

# 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)  )
students = [
    {
   'name': '晨晨', 'age':18, 'score': 78, 'tel': '123', 'gender': '男'},
    {
   'name': '陈来', 'age':20, 'score': 80, 'tel': '321', 'gender': '不明'},
    {
   'name': '陈昕', 'age':28, 'score': 98, 'tel': '653', 'gender': '女'},
    {
   'name': '小新', 'age':32, 'score': 65, 'tel': '783', 'gender': '男'},
    {
   'name': '小明', 'age':17, 'score': 24, 'tel': '988', 'gender': '女'},
    {
   'name': '小红', 'age':14, 'score': 54, 'tel': '903', 'gender': '男'}
]
# 1. 统计不及格学生的个数
count = 0
for stu in students:
    if  stu['score'] < 60:
        count += 1
print('1)不及格学生的个数:', count)  #  2
# 2.打印不及格学生的名字和对应的成绩
print('2)不及格学生的名字和对应的成绩')
for stu in students:
    score = stu['score'] #先保存成绩数据
    if score < 60:
        print(stu['name'], score) # 小明 24  小红 54
# 3.统计未成年学生的个数
# **4.打印手机尾号是8的学生的名字
# 方法一
print('4)手机尾号是8的学生的名字:')
for stu in students:
    if stu['tel'][-1] == '8':
        print(stu['name'])  # 小明
# 方法二
for stu in students:
    if int(stu['tel']) % 10 == 8:
        print(stu['name'])  # 小明
# 5)打印最高分和对应的学生的名字
print('5)最高分和对应的学生的名字:')
# 方法一:
#  **第一次循环获取最高分
max_score = students[0]['score']
for stu in students[1:]:
    score = stu['score']
    if score > max_score:
        max_score = score
# 第二次循环找到分数和最高分相等的所有学生的姓名
for stu in students:
    if stu['score'] == max_score:
        print(stu['name'], max_score) # 陈昕 98

# **方法二:
max_score = students[0]['score']
names = [students[0]['name']]
for stu in students[1:]:
    score = stu['score']
    if score == max_score:
        names.append(stu['name'])
    elif score > max_score:
        max_score = score
        names.clear()
        names.append(stu['name'])
print(names, max_score)   # ['陈昕'] 98

# 6.删除性别不明的所有学生(使用提出) 老师举例(下标遍历--倒着取)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值