变量
python中的变量是不需要写出具体类型的,直接根据数据类型赋予变量,形如:
money = 50
数据类型的转换
使用形如int(x),float(x),str(x)的形式来将数据类型转换:
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-1.png)
运算符
特别注意,取整除是//,求指数是**
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-3.png)
字符串的引号嵌套
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-4.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-5.png)
字符串格式化
占位
字符串可以用%s的格式进行占位拼接
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-6.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-7.png)
快速写法
快速格式化适合:1.不理会类型 2.不做精度控制,所以适合没有精度要求时快速使用
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-8.png)
if语句
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-9.png)
此外还有elif和else
for循环
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-10.png)
函数
函数的定义:
def function():
函数体
return 返回值
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-11.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-12.png)
容器
列表list
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-13.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-14.png)
元组Tuple
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-15.png)
字符串
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-16.png)
切片
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-17.png)
集合
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-18.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-19.png)
字典
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-20.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-21.png)
总结
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-22.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-23.png)
函数的多返回值
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-25.png)
函数的多种传参方式
关键字参数
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-26.png)
缺省参数
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-27-1024x387.png)
不定长参数
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-28.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-29-1024x461.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-30.png)
匿名函数
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-31.png)
例如:
def test(compute):
result = compute(1, 2)
print(f"结果是:{result}")
test(lambda x, y : x + y)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-32.png)
文件操作
读
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-33-1024x345.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-34.png)
写
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-35.png)
追加
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-36.png)
异常的捕获方法
捕获常规异常
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-37-1024x388.png)
捕获指定异常
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-38-1024x313.png)
捕获多个异常
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-39-1024x431.png)
捕获异常并输出描述信息
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-40.png)
捕获所有异常
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-41.png)
异常else
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-42.png)
异常finally
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-43.png)
异常的传递性
当函数func01中发生异常, 并且没有捕获处理这个异常的时候, 异常会传递到函数func02, 当func02也没有捕获处理这个异常的时候main函数会捕获这个异常, 这就是异常的传递性.
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-44-1024x410.png)
Python模块
模块的导入
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-45.png)
自定义模块
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-47.png)
类和对象
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-75.png)
构造方法
class student:
name = None
age = None
# 构造方法
def __init__(self, name, age):
self.name = name
self.age = age
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-76.png)
魔术方法
str方法
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-80.png)
lt方法
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-81-1024x289.png)
le方法
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-83.png)
eq方法
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-84.png)
封装、继承和多态
由于Python是基于C语言开发的面向对象的语言,其封装、继承和多态与C语言有许多相似之处。
封装
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-86.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-85.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-87.png)
继承
继承的基础语法
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-91.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-89.png)
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-92-1024x473.png)
复写和使用父类成员
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-93.png)
类型注解
变量的类型注解
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-94.png)
函数(方法)的类型注解
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-95.png)
Union类型
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-96.png)
多态
![](https://www.krooskun.cn/wp-content/uploads/2023/07/image-97.png)