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({"姓名": "张三", "年龄": "", "性别": "男"})
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', ''])
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
最新文章
- JSON.stringify()和JOSN.parse()
- HRBUST 1867 差分+BIT
- 最少步数(bfs)
- Android:What is ART?
- Unity该插件NGUI得知(9)—— Tween并转换成世界坐标系的大小NGUI尺寸
- ubuntu下mysql 开启远程连接
- css浮动(float,clear)
- 这年头做开源项目,被冷嘲热讽,FreeSql 0.0.4
- windows10 php7安装mongodb 扩展
- C++标准库第二版笔记 3 和异常的理解 1
- cdnbest设置301跳转
- 基于SaaS的企业数据隐私保护平台
- WebLogic Server Components:XA Transactions
- Tomcat启动程序端口冲突、确认相应进程及杀死冲突进程的解决方案
- android笔记--与服务器交互更改简历状态
- node知识积累
- WebService有什么用?
- win10 右键发送到 目录
- linux 段页式内存管理
- 游戏编程入门之测试Xbox360控制输入
热门文章
- python 3 输入和输出
- sort、sorted、heapq、bisect排序
- HTTP协议图--HTTP 响应状态码(重点分析)
- Java基础知识强化之集合框架笔记76:ConcurrentHashMap之 ConcurrentHashMap简介
- linux mint gcc 编译第一个c程序
- iOS开发中的Markdown渲染
- BZOJ2223/3524:[POI2014] Couriers(主席树)
- LVS (Linux Virtual Server) 思维导图笔记
- ethereumjs/ethereumjs-blockchain-2-test
- mavenWeb工程建立步骤