python基础语法

python目录详解

数据存储与运算

字面量与变量



非法的标识符红色


思路是加一个中间变量

常见数据类型

type显示类型的


输入与输出

运算符

数据的逻辑处理

条件判断

只有if内部才会执行,外部是并列

模式匹配

循环

数据存储容器

列表


切片只包含开始索引,不包含结 束索引,左闭右开


字符串

和列表一样

元组

集合

字典

函数

函数基础

函数进阶

类型注解

模块

也就是说,当主函数写了这个__name的时候,只有这个文件会执行下边的函数,外边文件哪怕导入了主文件模块也不会执行__name下边的部分

面向对象基础

python 复制代码
class 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)
python 复制代码
class 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)

异常

抛出的异常和捕获的异常一定要一样才能捕获到

这样也行

没有捕获也可以释放资源

相关推荐
Scott9999HH3 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社4 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海4 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰4 小时前
[爬虫]-Urllib
爬虫·python
萧瑟余晖5 小时前
JDK 26 新特性详解
java·开发语言
马优晨5 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
玉鸯6 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260856 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao7 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest