教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python中变量的命名及详解

python中变量的命名及详解

发布时间:2021-12-28   编辑:jiaochengji.com
教程集为您提供python中变量的命名及详解等资源,欢迎您收藏本站,我们将为您提供最新的python中变量的命名及详解资源

变量

一个变量存储一个值。

示例

<pre class="brush:html;toolbar:false">message = "Hello Python world!" print(message)</pre>

一个变量存储一个值。你可以在任何时候改变这个值。

<span style="color: rgb(85, 85, 85); font-family: "Classic Grotesque W01", "Avenir Next", "Segoe UI", "Helvetica Neue", Arial, "Hiragino Sans GB", "PingFang SC", "Heiti SC", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans", sans-serif; font-size: 16px; white-space: normal; background-color: rgb(255, 255, 255);"><pre class="brush:html;toolbar:false">message = "Hello Python world!" print(message) message = "Python is my favorite language!" print(message)</pre></span>

命名规则

变量名只能包含字母,数字,下划线。且只能以字母或下划线开头。

空格不允许出现在变量名中。

不能用 Python 关键字作为变量名。

变量名应当是有意义的。不能过短或过长。例如:mc_wheels 就比 wheels 和 number_of_wheels_on_a_motorycle 好。

小心使用小写的 l 和大写的 O 。它们容易和1和0混淆。

NameError

NameError 是一种常见的变量错误。仔细阅读下述代码,找出哪里出错了:

<pre class="brush:html;toolbar:false">message = "Thank you for sharing Python with the world, Guido!" print(mesage)</pre>

这个错误是由于变量前后不一致导致的。我们只要保证变量名前后一致就能避免这个错误。如下所示:

<pre class="brush:html;toolbar:false">message = "Thank you for sharing Python with the world, Guido!" print(message)</pre>

<span style="color: rgb(85, 85, 85); font-family: "Classic Grotesque W01", "Avenir Next", "Segoe UI", "Helvetica Neue", Arial, "Hiragino Sans GB", "PingFang SC", "Heiti SC", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans", sans-serif; font-size: 16px; white-space: normal; background-color: rgb(255, 255, 255);">
</span>

您可能感兴趣的文章:
python中变量的命名及详解
Python你知道多少?教你玩转Python变量与常量!
python命名空间是什么
anaconda和python区别
详解Python标识符命名规范
python语言变量命名规则
Python超级详细的变量命名规则
如何将python定义一个变量并使用?
php变量命名规则
python os模块是什么

[关闭]
~ ~