class Student(object):
def __init__(self, name, age, scores):
self.name = name
self.age = age
self.scores = scores
def get_name(self):
return self.name
def get_age(self):
return self.age
def get_course(self):
return max(self.scores)
zz = Student("aa", 18, [80, 100, 90])
print(zz.get_name())
print(zz.get_age())
print(zz.get_course())
aa
18
100
class DictClass(object):
def __init__(self, dict):
self.dict = dict
def del_dict(self, key):
# 判断key是否在字典里
if key not in self.dict.keys():
return "key不在字典里"
else:
self.dict.pop(key)
return "删除成功"
def get_dict(self, key):
if key not in self.dict.keys():
return "not found"
else:
return self.dict[key]
def get_key(self):
return self.dict.keys()
def updata_dict(self, dict2):
self.dict = dict(self.dict, **dict2)
return self.dict.values()
d = DictClass({"a":1, "b":2})
print(d.get_key())
print(d.updata_dict({"c":3}))
print(d.del_dict("a"))
print(d.get_dict("b"))
dict_keys(['a', 'b'])
dict_values([1, 2, 3])
删除成功
2
class ListInfo(object):
def __init__(self, list_val):
self.list = list_val
def add_key(self, key_name):
# 添加的key必须是数字或者字符串
if isinstance(key_name, (str, int)):
self.list.append(key_name)
print(self.list)
return "OK"
else:
return "我要字符串或者数字"
def get_key(self, index):
# 判断传入的索引是否超出了列表
if index >= 0 and index < len(self.list):
return self.list[index]
return "你输入的太多了!"
def updata_list(self, new_list):
self.list.extend(new_list)
return self.list
def del_key(self):
# 首先要判断我们的列表里面是否还有元素
if len(self.list) >= 0:
return self.list.pop(-1)
else:
return "列表是空的"
list_info = ListInfo([1, 2, 3, 4, 5])
print(list_info.add_key([1,2]))
print(list_info.get_key(999))
print(list_info.get_key(3))
print(list_info.updata_list([8, 9, 10]))
print(list_info.del_key())
我要字符串或者数字
你输入的太多了!
4
[1, 2, 3, 4, 5, 8, 9, 10]
10
class SetInfo(object):
def __init__(self, my_set):
self.sett = my_set
def add_setinfo(self, keyname):
self.sett.add(keyname)
return self.sett
def get_intersection(self, unioninfo):
if isinstance(unioninfo, set):
return self.sett & unioninfo
else:
return "你传入的不是set"
def get_union(self, unioninfo):
if isinstance(unioninfo, set):
return self.sett | unioninfo
return "你传入的不是set"
def del_difference(self, unioninfo):
if isinstance(unioninfo, set):
return self.sett - unioninfo
return "你传入的不是set"
A = set([1, 2, 3, 4, 5])
B = set([5, 6, 3, 8])
my_set = SetInfo(A)
print(my_set.add_setinfo(6))
print(my_set.get_intersection(B))
print(my_set.get_union(B))
print(my_set.del_difference(B))
print(my_set.get_union("123"))
{1, 2, 3, 4, 5, 6}
{3, 5, 6}
{1, 2, 3, 4, 5, 6, 8}
{1, 2, 4}
你传入的不是set
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。