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)

异常

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

这样也行

没有捕获也可以释放资源

相关推荐
维克兜率天1 分钟前
4.3.2.1 日常监控:上线只是开始
服务器·数据库·python·区块链·php·量化
李迟2 分钟前
Golang实践录:工程框架实践:搭建工程模板
开发语言·后端·golang
纪伊路上盛名在3 分钟前
Kabsch算法的Julia实现
开发语言·算法·julia·序列分析·蛋白质·rmsd
玖玥拾7 分钟前
Lua 基础语法(三) 元表 metatable、元方法、模拟面向对象
开发语言·unity·lua
the局外人8 分钟前
学习 FastAPI 的 Day 2:用异步 ORM 完成增删改查
后端·python·fastapi
智搜广告11 分钟前
GEO优化公司怎么选?智搜广告从三个维度帮你判断
大数据·人工智能·python·elasticsearch·microsoft·geo
维天说13 分钟前
Agent会听人话,反而更难管
java·开发语言·人工智能
wanglei20070821 分钟前
软件架构设计中,各个组件之间的通信方式有哪些?
python
会飞的拖把27 分钟前
Python 面向对象编程详解:从类与对象到动态属性方法
开发语言·python
微小冷33 分钟前
Python CasADi初步教程
python·控制·模型预测·casadi·符号表达式·sx