zoukankan      html  css  js  c++  java
  • python3 class类 练习题

    """
    一、定义一个学生Student类。有下面的类属性:
    1 姓名 name
    2 年龄 age
    3 成绩 score(语文,数学,英语) [每课成绩的类型为整数]


    类方法:
    1 获取学生的姓名:get_name() 返回类型:str
    2 获取学生的年龄:get_age() 返回类型:int
    3 返回3门科目中最高的分数。get_course() 返回类型:int


    写好类以后,可以定义2个同学测试下:
    zm = Student('zhangming',20,[69,88,100])
    返回结果:
    zhangming
    20
    100
    """

    class Student():
        # 构造函数
        # 对当前对象的实例的初始化
        def __init__(self, name, age, score):
            self.name = name
            self.age = age
            self.score = score
    
        #   isinstance函数判断一个对象是否是一个已知的类型,类似type
        def get_name(self):
            if isinstance(self.name, str):
                return self.name
    
        def get_age(self):
            if isinstance(self.age, int):
                return self.age
    
        def get_course(self):
            a = max(self.score)
            if isinstance(a, int):
                return a
    
    
    zm = Student('zhangming', 20, [69, 88, 100])
    print(zm.get_name())
    print(zm.get_age())
    print(zm.get_course())

    """
    二、定义一个字典类:dictclass。完成下面的功能:

    dict = dictclass({你需要操作的字典对象})

    1 删除某个key

    del_dict(key)


    2 判断某个键是否在字典里,如果在返回键对应的值,不存在则返回"not found"

    get_dict(key)

    3 返回键组成的列表:返回类型;(list)

    get_key()

    4 合并字典,并且返回合并后字典的values组成的列表。返回类型:(list)

    update_dict({要合并的字典})
    """

    class Dictclass():
    
        # 构造函数
        # 对当前对象的实例的初始化
        def __init__(self, class1):
            self.classs = class1
    
        def del_dict(self, key):
            if key in self.classs.keys():
                del self.classs[key]
                return self.classs
            return "不存在这个值,无需删除"
    
        def get_dict(self, key):
            if key in self.classs.keys():
                return self.classs[key]
            return "not found"
    
        def get_key(self):
            return list(self.classs.keys())
    
        def update_dict(self, dict1):
            # 方法1
            # self.classs.update(dict1)
            # 方法2,对于重复的key,B会覆盖A
            a = dict(self.classs, **dict1)
            return a
    
    
    a = Dictclass({"姓名": "张三", "年龄": "18", "性别": ""})
    print(a.del_dict("年龄"))
    print(a.get_dict("姓名"))
    print(a.get_key())
    print(a.update_dict({"年薪": 0}))

    """
    三、定义一个列表的操作类:Listinfo

    包括的方法:

    1 列表元素添加: add_key(keyname) [keyname:字符串或者整数类型]
    2 列表元素取值:get_key(num) [num:整数类型]
    3 列表合并:update_list(list) [list:列表类型]
    4 删除并且返回最后一个元素:del_key()

    a = Listinfo([44,222,111,333,454,'sss','333'])
    """

    class Listinfo():
    
        def __init__(self, my_list):
            self.listt = my_list
    
        def add_key(self, keyname):
            if isinstance(keyname, (str, int)):
                self.listt.append(keyname)
                return self.listt
            return "error"
    
        def get_key(self, num):
            if num >= 0 and num < len(self.listt):
                a = self.listt[num]
                return a
            return "超出取值范围"
    
        def update_list(self, list1):
            if isinstance(list1, list):
                self.listt.extend(list1)
                return self.listt
            return "类型错误"
    
        def del_key(self):
            a = self.listt.pop(-1)
            return a
    
    
    a = Listinfo([44, 222, 111, 333, 454, 'sss', '333'])
    print(a.add_key(1))
    print(a.get_key(1))
    print(a.update_list([1, 2, 3]))
    print(a.del_key())

    """
    定义一个集合的操作类:Setinfo

    包括的方法:

    1 集合元素添加: add_setinfo(keyname) [keyname:字符串或者整数类型]
    2 集合的交集:get_intersection(unioninfo) [unioninfo :集合类型]
    3 集合的并集: get_union(unioninfo)[unioninfo :集合类型]
    4 集合的差集:del_difference(unioninfo) [unioninfo :集合类型]
    set_info = Setinfo(你要操作的集合)
    """

    class Setinfo():
    
        def __init__(self, my_set):
            self.sett = my_set
    
        def add_setinfo(self, keyname):
            if isinstance(keyname, (str, int)):
                self.sett.add(keyname)
                return self.sett
    
        def get_intersection(self, unioninfo):
            if isinstance(unioninfo, set):
                a = self.sett & (unioninfo)
                return a
    
        def get_union(self, unioninfo):
            if isinstance(unioninfo, set):
                a = self.sett | (unioninfo)
                return a
    
        def del_difference(self, unioninfo):
            if isinstance(unioninfo, set):
                a = self.sett - (unioninfo)
                return a
    
    
    a = Setinfo({1, "a", 2, "b", 3, "c"})
    print(a.add_setinfo(4))
    print(a.get_intersection({1, 2, "a"}))
    print(a.get_union({2, 3, 4, "c", "d"}))
    print(a.del_difference({1, 2, 3, 4}))


    原文:https://blog.csdn.net/bullpride/article/details/52022701

  • 相关阅读:
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)K Co-prime Permutation
    2020 CCPC绵阳站赛后回忆+总结
    CF 1473D Program
    Educational Codeforces Round 100 (Rated for Div. 2) 补题情况
    Codeforces Round #690 (Div. 3) (补题情况)
    这个博客停用,新博客地址:www.baccano.fun
    炫酷路径(牛客)
    洛谷 P1123 取数游戏
    洛谷P2802 回家
    cf上分的失落->高兴->更失落
  • 原文地址:https://www.cnblogs.com/vaster/p/9874984.html
Copyright © 2011-2022 走看看