我是Python的初学者,但我常常混淆字面值和变量。
以下是我所知道的字面值:"a"+"b"
而变量的定义方式如下:sentence="a"+"b"
我是Python的初学者,但我常常混淆字面值和变量。
以下是我所知道的字面值:"a"+"b"
而变量的定义方式如下:sentence="a"+"b"
字面值是表示固定 (const
) 值的符号表示法。
变量是与符号名称相关联的存储位置(如果你愿意的话,可以指向它)。
最好在使用中解释清楚:
foo = bar(42)
^ ^ ^
| | |--- literal, 42 is *literally* 42
| |------- function, also represents "something" in memory
|------------- variable, named "foo", and the content may vary (is variable)
而标识符是Python语句中分配给变量的名称。
1
、1.5
、'a'
、"abc"
等都是字面值。但在语句x=123
中,x是一个变量,而123是一个字面值。