类的操作练习

"""
设计:Python程序设计
作者:初学者
日期:2022年 03月 19日
"""![在这里插入图片描述](https://img-blog.csdnimg.cn/1e59f40885a04234a465ea8ed5f0fbf2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rSq6I2S5a6H5a6ZcHk=,size_12,color_FFFFFF,t_70,g_se,x_16#pic_center)


class Student:
    def __init__(self, name, age, score, sex):
        self.name = name
        self.age = age
        self.score = score
        self.sex = sex

    def print_self_info(self):
        print('%s的年龄是%d,成绩是%d,性别是%s' % self.age, self.score, self.age)


lists = [
    Student('张三', 20, 90, '男'),
    Student('李四', 28, 80, '男'),
    Student('王伟', 20, 70, '男'),
    Student('李好', 24, 100, '女'),
    Student('张帅', 30, 95, '男'),
    Student('杜丽', 40, 99, '女'),
    Student('德卡没想', 45, 60, '男')
]


# 查找王伟
def find():
    for item in lists:
        # print(item)
        if item.name == '王伟':
            return item


stu = find()
print(stu.name, stu.age, stu.score)


# 查找女同学
def find01():
    lists1 = []
    for items in lists:
        if items.sex == '女':
            lists1.append(items)
    return lists1

re = find01()
for item in re:
    print(item.name, item.age)


# 查找年龄>=30的学生和数量
def find02():
    count = 0

    for i in lists:
        if i.age >= 30:
            count += 1
    return count


print(find02())


你可能感兴趣的:(练习,python,list,数据结构)