python目录详解
数据存储与运算
字面量与变量
非法的标识符红色
思路是加一个中间变量常见数据类型
type显示类型的
输入与输出
运算符
数据的逻辑处理
条件判断
只有if内部才会执行,外部是并列
模式匹配
循环
数据存储容器
列表
切片只包含开始索引,不包含结 束索引,左闭右开
字符串
和列表一样
元组
集合
字典
函数
函数基础
函数进阶
类型注解
模块
也就是说,当主函数写了这个__name的时候,只有这个文件会执行下边的函数,外边文件哪怕导入了主文件模块也不会执行__name下边的部分
面向对象基础
pythonclass Car: def __init__(self,c_color,c_brand,c_name,c_price): self.color = c_color self.brand = c_brand self.name = c_name self.price = c_price print("Car类型的对象初始化完毕,对象属性已经添加完毕") #定义实例方法 def running(self): print(f"{self.brand} {self.name} 正在高速行驶中。。。。") def total_cost(self,discount,rate=0.3): total_cost = self.price * discount+rate * self.price return total_cost #魔法方法 def __str__(self): #输出自定义的方法,返回字符串 return f"{self.color} {self.brand} {self.name} {self.price}" def __eq__(self,other): return self.color == other.color and self.brand == other.brand def __lt__(self, other): return self.price < other.price #测试 c1 = Car("红色","BWM","K1",999999) print(c1) c2 = Car("红色","BWM","K1",999999) print(c2) print(c1 == c2) print(c1 < c2)

pythonclass Car: #类属性(所有实例对象共享) wheel = 4# 轮胎数量 tax_rate = 0.1 #购置税税率 def __init__(self,c_color,c_brand,c_name,c_price): #实例属性 self.color = c_color self.brand = c_brand self.name = c_name self.price = c_price self.wheel = 2 def running(self): print(f"{self.brand} {self.name} 正在高速行驶中。。。。") def total_cost(self,discount,rate=0.3): total_cost = self.price * discount+rate * self.price return total_cost #测试 c1 = Car("红色","BWM","K1",999999) print(c1.brand) print(c1.wheel) #通过实例对象,查找属性时,会先查找实例属性,实例属性不存在时,再查找类属性 #通过类名访问类属性 print(Car.wheel)
异常
抛出的异常和捕获的异常一定要一样才能捕获到
这样也行
没有捕获也可以释放资源









































































































