# 集合 set 无序不重复的类型
# 字符串 '' 数字 12 33.3 列表 [] 元组 () 字典 {:} 集合 {111}
from random import randint
ls = [randint(10, 13) for _ in range(10)]
print(ls)
s1 = set(ls)
print(s1)
print(type(s1))
# 声明一个集合
s2 = set() # 空集合
d = {} # 空字典
print(type(s2))
print(type(d))
s3 = {111, 222.22, '哈哈'}
print(s3)
# 不支持下标访问
# s3[0] TypeError: 'set' object is not subscriptable
# 添加
s3.add('嘻嘻')
s3.add('嘻嘻')
s3.add('嘻嘻')
print(s3)
#删除
v = s3.pop()
print(v)
print(s3)
s3.remove(111)
print(s3)
s3.clear()
print(s3)
面向对象
## 面向对象
# 区别面向过程的
# 通过类创建对象 去完成我们要做的事情
# 类是对象的模板, 对象是类的具体实例
# 类的定义格式
# class 类名(object):
# # 方法
# pass
# 类中拥有属性(事务的特性)和方法(事务的行为)
class Student(object):
def __init__(self):
"""__方法名__都是魔法方法, 具有特定的用途
比如 __init__ 方法就是专门定义对象属性的,
它会在类创建对象的过程中自动被调用
"""
print("__init__ 自动被调用")
# 定义属性
self.name = "张三"
self.age = 18
def study(self):
print(f"{self.name}疯狂的学习中.....")
def eat(self):
print(f"{self.name}大口的吃着...")
# 使用类
# 对象名 = 类名()
s1 = Student()
s2 = Student()
# 对象名.方法名()
s1.study()
s1.eat()
# 属性赋值(修改)
s2.name = "李四"
s2.study()
self 是什么
class Student(object):
def __init__(self):
self.name = "张三"
self.age = 18
def study(self):
print(f'self的地址是{id(self)}')
print(f"{self.name}疯狂的学习中.....")
# self 就是当哪个对象被调用 self 就是谁
s1 = Student()
s2 = Student()
print(s1) # 对象在内存中的地址
print(id(s1)) # 对象在内存中的地址
print(id(s2))
s1.study()
s2.study()
比较完整的类
class Student(object):
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
def study(self):
print(f"{self.name}疯狂的学习中.....")
print(f"分数是{self.score}")
# self 就是当哪个对象被调用 self 就是谁
s1 = Student("张三", 20, 89)
s2 = Student('李四', 17)
s1.study()
s2.study()
定义一个比赛类
# match 类
# 属性 赛事名 str , 比分: str 参赛选手 []
# 方法: 显示上半场比分, 显示下半场比分
# 创建两个比赛对象, 并且进行数据模拟
class Match(object):
def __init__(self, name, score):
self.name = name
self.score = score
self.player = []
def first(self):
print(f"{self.name}")
print(f"{self.player}参与本场比赛")
print(f"分数是{self.score}")
def second(self):
print(f"{self.name}")
print(f"{self.player}参与本场比赛")
print(f"分数是{self.score}")
s1 = Match("英超联赛", 3)
s2 = Match('中超联赛', 17)
s1.player = ['peter', 'c罗', '梅西']
s1.first()
s2.second()
继承
# 继承: 子类继承父类所有属性和方法
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print("喵喵的吃")
def show(self):
print(f"姓名: {self.name} 年龄: {self.age}")
class Student(Person):
def __init__(self, name, age, score):
# 主动调用父类的__init___方法
super().__init__(name, age)
self.score = score
def show(self):
print(f"姓名: {self.name} 年龄: {self.age} 分数 {self.score}")
class Teacher(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.salary = salary
def show(self):
print(f"姓名: {self.name} 年龄: {self.age} 工资 {self.salary}")
s1 = Student('小明', 13, 100)
s1.show()
t1 = Teacher('Eric', 56, 3500)
t1.show()



