"""
一、定义一个学生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

最新文章

  1. JSON.stringify()和JOSN.parse()
  2. HRBUST 1867 差分+BIT
  3. 最少步数(bfs)
  4. Android:What is ART?
  5. Unity该插件NGUI得知(9)—— Tween并转换成世界坐标系的大小NGUI尺寸
  6. ubuntu下mysql 开启远程连接
  7. css浮动(float,clear)
  8. 这年头做开源项目,被冷嘲热讽,FreeSql 0.0.4
  9. windows10 php7安装mongodb 扩展
  10. C++标准库第二版笔记 3 和异常的理解 1
  11. cdnbest设置301跳转
  12. 基于SaaS的企业数据隐私保护平台
  13. WebLogic Server Components:XA Transactions
  14. Tomcat启动程序端口冲突、确认相应进程及杀死冲突进程的解决方案
  15. android笔记--与服务器交互更改简历状态
  16. node知识积累
  17. WebService有什么用?
  18. win10 右键发送到 目录
  19. linux 段页式内存管理
  20. 游戏编程入门之测试Xbox360控制输入

热门文章

  1. python 3 输入和输出
  2. sort、sorted、heapq、bisect排序
  3. HTTP协议图--HTTP 响应状态码(重点分析)
  4. Java基础知识强化之集合框架笔记76:ConcurrentHashMap之 ConcurrentHashMap简介
  5. linux mint gcc 编译第一个c程序
  6. iOS开发中的Markdown渲染
  7. BZOJ2223/3524:[POI2014] Couriers(主席树)
  8. LVS (Linux Virtual Server) 思维导图笔记
  9. ethereumjs/ethereumjs-blockchain-2-test
  10. mavenWeb工程建立步骤