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 小时前
Rust 迭代器完全通俗易懂指南(零基础全覆盖)
java·开发语言·rust
MY_TEUCK8 小时前
【Java 后端】SpringBoot 登录认证与会话跟踪实战(JWT + Filter/Interceptor)
java·开发语言·spring boot
QQ2422199798 小时前
基于python+微信小程序的家教管理系统_mh3j9
开发语言·python·微信小程序
沐知全栈开发9 小时前
JavaScript 条件语句
开发语言
RSTJ_16259 小时前
PYTHON+AI LLM DAY THREETY-SEVEN
开发语言·人工智能·python
郝学胜-神的一滴9 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Aision_9 小时前
Agent 为什么需要 Checkpoint?
人工智能·python·gpt·langchain·prompt·aigc·agi
清水白石0089 小时前
《Python性能深潜:从对象分配开销到“小对象风暴”的破解之道(含实战与最佳实践)》
开发语言·python
Je1lyfish9 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法
Brilliantwxx9 小时前
【C++】 vector(代码实现+坑点讲解)
开发语言·c++·笔记·算法