变量名
回忆上次内容
- 上次讲了 什么是
变量
- 变量变量
- 各种系统、游戏就是由变量所组成的
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第1张图片](http://img.e-com-net.com/image/info8/b22418dc2d9d4a2f8dd0318bd5bf0606.jpg)
- 声明了变量
- 声明就是 declaration
- 把标识符 和 具体值 联系起来
- 标识符就是 变量的标记符
- 具体值 就是 赋给变量的值
- 过程就是 赋值
- 哪些字符串可以用作变量名呢?
变量名
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第2张图片](http://img.e-com-net.com/image/info8/d038f669ef4449fa9785df1601c1b8e9.jpg)
- 1a 不可以 作为变量名
- 为什么 不能是 数字开头 呢?
数字
- 数字开头 一般都表示 数字常量
- 从 c 语言的时候就是这样
- 这样 词法分析器 便于区分
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第3张图片](http://img.e-com-net.com/image/info8/eb0da31ea68349b39242e438a9fa0ab4.jpg)
命名要求
-
变量名、函数名、模块名
-
c语言对于标识符的要求是这样的
- 第一个字符应该是字母或下划线
- The first letter of an identifier should be either a letter or an underscore
- 合法的标识符可以包括大小写字母、数字、下划线
- A valid identifier can have letters(both uppercase and lowercase letters),digits and underscores.
-
python具体什么要求呢?
查看文档
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第4张图片](http://img.e-com-net.com/image/info8/70ce5d7c52c449afb1bdb32e660bdc0e.jpg)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v7hW2yO0-1681783529272)(https://doc.shiyanlou.com/courses/uid1190679-20230215-1676466057390)]
标识符
- 基本规则和c语言类似
- 第一个字符应该是字母或下划线
- 后面跟 字母、数字、下划线
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第5张图片](http://img.e-com-net.com/image/info8/56972249a39e49768c0e0f17c3547f84.jpg)
中文标识符
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第6张图片](http://img.e-com-net.com/image/info8/acb85dae382a4dac96b0d889343b15f7.jpg)
PEP3131
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第7张图片](http://img.e-com-net.com/image/info8/d3a12c7a07a14013911a423aa522e657.jpg)
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第8张图片](http://img.e-com-net.com/image/info8/950ede6b936242dba6f6ca2d18a743ac.jpg)
identifier
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第9张图片](http://img.e-com-net.com/image/info8/4233e49b539340968b9b8e3a40574504.jpg)
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第10张图片](http://img.e-com-net.com/image/info8/5df2a764eeeb49cbb506b172318ca269.jpg)
- 如果非要数字开头的话
- 标点也属于unicode字符
标点
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第11张图片](http://img.e-com-net.com/image/info8/6a7ea18f02564dd2bb86051fb9b09093.jpg)
分别赋值
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第12张图片](http://img.e-com-net.com/image/info8/9517c06afb364182bdc112a4b9dd137e.jpg)
练习
-
以下哪些变量名是合法的?
- oeasy
- o2z
- o?z
- o,z
- _orz
- 0rz
- __0rz
-
使用函数验证一下推论
失灵
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第13张图片](http://img.e-com-net.com/image/info8/9668e4887ead4d658a4b92ac14453e9c.jpg)
总结
- 变量 就是 能变的量
- 这次研究了 变量标识符的 规则
- 第一个字符 应该是 字母或下划线
- 合法的标识符可以包括
- 还研究了字符串(str)的函数
- 最后发现
![[oeasy]python0133_变量名_标识符_identifier_id_locals_第14张图片](http://img.e-com-net.com/image/info8/9668e4887ead4d658a4b92ac14453e9c.jpg)
-
这怎么理解呢??
-
我们下次再说!
-
蓝桥->https://www.lanqiao.cn/courses/3584
-
github->https://github.com/overmind1980/oeasy-python-tutorial
-
gitee->https://gitee.com/overmind1980/oeasypython