删除变量
回忆上次内容
- 上次研究了变量的死
- 有生就有死
- 原本的死是在程序退出的时候自动执行的
- 也可以手动给变量执行死刑
- del
- del(a)之后
- dir()就无法在当前作用域(scope)内观察到这个变量了
- 也就是说a死了😱
- 当然我们还可以让他重生
- 感觉自己成了造物主了
- 这次主要就是研究变量之死
- 变量里面还有什么需要注意的么?
- 我们来总结一下
变量的定义、声明和赋值
- 什么是变量?
- 能变的量
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值](https://file.cfanz.cn/uploads/png/2023/08/02/10/32JdP131S6.png)
- 变量 只有声明和赋值了之后 才能用
- 否则 就会出现 名字错误
- NameError
变量的重新赋值
- 变量重新赋值的时候
- 会重新申请一个
- 该类型变量的空间
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_02](https://file.cfanz.cn/uploads/png/2023/04/20/11/6JR7X2O84D.png)
- 然后
- 让变量 指向新空间
接收输入
- 变量 可以接受
- 键盘的输入
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_03](https://file.cfanz.cn/uploads/png/2023/04/22/2/2S683APNI1.png)
- 输入的函数
- 是input()
运算
- 不同的变量类型
- 对应着不同的 加法运算
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_04](https://file.cfanz.cn/uploads/png/2023/08/02/10/eIGG1b8940.png)
- 有时候不能相加
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_05](https://file.cfanz.cn/uploads/png/2023/08/02/10/T288dYS4e9.png)
- 相加类型不合适的时候
- 会发生类型错误
- TypeError
- 如果不确定数据类型
- 又想相加
- 怎么办?
转化
- str型和int型
- 无法直接相加
- 这就需要
- 类型转化
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_06](https://file.cfanz.cn/uploads/png/2023/08/02/10/3334343ZV4.png)
- 转化之后
- 就可以相加了
- 程序运行过程中
- 出现的各种问题
- 如何处理?
处理异常
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_07](https://file.cfanz.cn/uploads/png/2023/08/02/10/5c6N1aL36a.png)
- 错误不应被悄悄传递
- 除非你确定需要这样做
- 精准地捕获异常
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_08](https://file.cfanz.cn/uploads/png/2023/08/02/10/4U5f0Yd01e.png)
- 发现了错误
- 一定要处理
- 一般不能 在except模块中 直接pass
- 也就是不写 except:pass 风格的代码
拆分程序
- vim窗口拆分的方式
- :split
- ctrl + w + n
- vim窗口合一的方式
- :only
- ctrl + w + o
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_09](https://file.cfanz.cn/uploads/png/2023/04/28/13/RfR8eYfAf2.png)
- 把程序拆分成 输入输出 两个部分
- 分别放在 两个文件里面
- 可以 更加细分么?
主控程序
- 可以把 程序
- 更加 细分
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值_10](https://file.cfanz.cn/uploads/png/2023/08/02/10/0Z0204XT7a.png)
- 其中 输入部分
- 包括
- try
- except
- try 语句块的完整形式
- 是什么呢?
try 完整形式
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_11](https://file.cfanz.cn/uploads/png/2023/04/30/5/9SGA328E36.png)
- 完善后的代码如何进行版本控制呢?
git版本控制
- git commit提交版本
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值_12](https://file.cfanz.cn/uploads/png/2023/08/02/10/99dM84H2N6.png)
- git log 查看版本
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值_13](https://file.cfanz.cn/uploads/png/2023/08/02/10/29a26M899K.png)
- git checkout 回滚到版本
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_14](https://file.cfanz.cn/uploads/png/2023/08/02/10/4V524OH7YL.png)
- 可以为代码添加什么注释么?
注释
- 注释有两种
- 第一种:单行注释
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_15](https://file.cfanz.cn/uploads/png/2023/08/02/10/F8BKc27e07.png)
- 第二种:多行注释
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_16](https://file.cfanz.cn/uploads/png/2023/08/02/10/5RPI4MNED2.png)
- 多行注释可以怎么用么?
多行注释生成帮助网页
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_17](https://file.cfanz.cn/uploads/png/2023/08/02/10/ddJF44cQUd.png)
- python3 -m pydoc -w main
- 对于 main.py 生成帮助网页
- 帮助文件叫做 main.html
- 帮助文件 就生成在当前的 apple 文件夹
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_18](https://file.cfanz.cn/uploads/png/2023/08/02/10/62cWdWZH4V.png)
- 有的时候
- 帮助注释也不能全写清楚
- 怎么办?
动态类型
- 变量名明确类型
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_19](https://file.cfanz.cn/uploads/png/2023/05/09/14/61L502PER3.png)
- 蛇形命名(snake_case)包括了
- 变量类型
- 变量含义
- 其实变量类型主要有两种
- 字符串
- 数字
字符串
- 字符串就是字符的串
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_20](https://file.cfanz.cn/uploads/png/2023/07/13/17/4Ea9HW0H24.png)
- 字符串是一个个字符组成的串
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_21](https://file.cfanz.cn/uploads/png/2023/07/13/17/6U6e1a35I1.png)
- 变量类型是
- 字符串
- str
- 另一种常见类型是什么呢?
整数
-另一种常见类型是
- 整型数字
- int
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_字符串_22](https://file.cfanz.cn/uploads/png/2023/08/02/10/DH069dBB34.png)
- 根据 进制类型
- 可以有不同的 表示方法
进制转化
- 一个整数 可以有各种进制的显示形式
- 数字大小 与 使用进制形式 无关
- 苹果数量 与 数苹果的人有几根手指 无关
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值_23](https://file.cfanz.cn/uploads/png/2023/08/02/10/U61033Z6LA.png)
- 这些就是
- 变量的定义、赋值
- 变量是如何消失的呢?
删除变量
- 变量a的一生
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_变量类型_24](https://file.cfanz.cn/uploads/png/2023/08/02/10/AI9G9Tbc89.png)
- 删了就没了
![图片描述 [oeasy]python0078_变量部分总结_variable_summary_赋值_25](https://file.cfanz.cn/uploads/png/2023/08/02/10/316240M223.png)
- 没了就不能再删了
- 这章
- 我们总结完了
- 总结完了
- 就不能再总结了😄
总结
- 这次我们研究的比较杂
- 类型转化
- 进制转化
- 捕获异常
- 版本控制
- 生成帮助文档
- 变量的常用类型
- 变量的生命周期控制
- 数据类型主要研究了两个
- 字符串
- str
- 整型数字
- int
- 字符串型 和 整型数字型变量
- 是可以相互转化的
- 加法运算逻辑 会根据操作变量的不同 而不同
- 整型变量的加法 是真的加
- 字符串的加法 是字符串拼接
- 字符串中有一些转义字符
- \n
- \r
- \t
- 都是什么意思来着??
- 还有什么没见过的转字符吗??🤔
- 下次再说👋








