什么是常量和文字常量? [英] What are Constants and Literal constants?
问题描述
我正在学习Python,只是对Constants和Literal常量感到困惑,它们是什么?我们出于什么目的使用它们?与普通变量有什么区别?
I'm learning Python and i am just confused with the Constants and Literal constants.What are they?For what kind of purpose do we use them ? What is their difference from the normal variable?
非常感谢您.
我是一个真正的初学者.就像我可以说的初学者一样,我对编程世界一无所知.就像我不知道什么是表达式,反之亦然.
I'm a true beginner.As beginner as i can say i know nothing about the programming world.Like i don't know what an expression is and vice versa.
我一直在使用"A byte of python"这本书来学习python语言,在书中的某个地方,我遇到了一个讨论字面量和常量的部分.我在那儿分享该部分:
I have been learning the python language using the " A byte of python " book and somewhere in the book i came across a section which talks about literals and constants.I share the section there:
5.2.文字常量
5.2. Literal Constants
文字常量的一个示例是数字,例如5,1.23或a 像这是一个字符串"或这是一个字符串!"之类的字符串
An example of a literal constant is a number like 5 , 1.23 , or a string like 'This is a string' or "It's a string!" .
之所以称其为文字,是因为它是文字-您可以使用其值 字面上地.数字2总是代表自己,别无其他-它 是一个常量,因为它的值无法更改.因此,所有这些 被称为文字常量.
It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.
上面写着因为它是字面上的,所以它被称为字面意思,因为您按字面意义使用它的值",我只是不明白这一点.这本书试图说我们按字面意义使用值是什么?另一本书不清楚的是数字2是一个常量,因为它的值无法更改.怎么可能?我们可以更改它,例如:
Where it says,"it is called literal because it is literal-you use the it's value literally",i just don't get this part.What is the book trying to say that we use the value literally?the another vague point is that the number 2 is a constant because it's value cannot be changed.How is it possible?we can change it,like:
stack = 2
stack = 3
首先,我将数字2分配给堆栈,然后更改了堆栈的值(书中声称该数字2是一个常数,因此无法更改),并为其分配了数字3.所以我很容易地更改了数字2的值.我真的很困惑,如果您不明白我的意思,请告诉我,以便我举更多的例子.谢谢您的陪伴.
First, I assigned the number 2 to the Stack then I changed the value of the Stack(Which is that number 2 that the book is claiming it is a constant so it cannot be changed)and assigned the number 3 to it.So i easily changed the value of the number 2.I am really confused,if you didn't get my point,please tell me so i can give more examples.Thank you for giving your time guys.
推荐答案
OP编辑后的答案
文字常量是实际的文字值;我知道字面量这个词会让您感到困惑,但是举个例子可能会使它更清楚.如果您在REPL中输入以下内容:
A literal constant is an actual literal value; I know the word literal confuses you but an example might make it clearer. If you type the following in the REPL:
>>> 2
2
>>> 'hello'
'hello'
2
和hello
是实际的文字常量,与您所认为的相反,您无法更改它们的值(嗯,作为初学者,最好不知道这一点).如果有:
2
and hello
are actual literal constants and contrary to what you think, you can't change their value (well, you can, as a beginner, it's best to not know about that). When you have:
stack = 2
stack = 3
您首先要为stack
分配常量文字(尽管,老实说,不用担心它叫什么,它是数字2).因此,名称stack
指向值2
.然后,说出stack = 3
,表示您正在 不是 更改值2
; 您现在将名称stack
指向另一个值3
.
You are first assigning the constant literal (though, honestly, don't worry about what it's called, it's the number 2) to stack
. So, the name stack
is pointing to the value 2
. Then, by saying stack = 3
, you are not changing the value 2
; you are now making the name stack
to point to another value, 3
.
就其价值而言,常量字面量"听起来很复杂;只需将2
或'John'
等值视为实际值即可.关于实际常量(在编程常量中,常量指的是赋值后不能更改的变量),该概念实际上在Python中并不存在.例如,常量是指您说stack = 2
的时间,但随后您将永远无法更改stack
指向的内容,否则会出现错误.在Python中,这个概念不存在.
For what it's worth, "constant literal" sounds complicated; just think of values like 2
or 'John'
etc. as what they are. And with regards to actual constants (in programming constants are referred to variables that cannot be changed after assignment), that concept doesn't really exist in Python. A constant is when, for instance, you say stack = 2
but then you cannot ever change what stack
is pointing to or you'll get an error. In Python, this concept does not exist.
原始答案:
对于初学者,我建议您阅读:
For starters, I recommend you read The story of None, True and False (and an explanation of literals, keywords and builtins thrown in) by Guido:
另一方面,文字是描述常量值的表达式的元素.文字的示例是数字(例如42、3.14或1.6e-10)和字符串(例如"Hello,world").解析器可以识别文字,并且解析文字的确切规则通常非常微妙.
A literal, on the other hand, is an element of an expression that describes a constant value. Examples of literals are numbers (e.g. 42, 3.14, or 1.6e-10) and strings (e.g. "Hello, world"). Literals are recognized by the parser, and the exact rules for how literals are parsed are often quite subtle.
对于常量",您不能在Python中将变量声明为真常量".有内置常量,如True
和False
和None
在Python中,但即使在Python 2.X中它们也不是真常量",因为可以将它们分配为指向另一个值:
As for "constants", you cannot declare a variables as "true constants" in Python. There are a Built-in Constants like True
and False
and None
in Python but even they are not"true constants" in Python 2.X as they can be assigned to point to another value:
True = False
if True:
print 'Hey'
else:
print 'WAAAT!'
我希望这会有所帮助.如果不是,请编辑您的问题,并举例说明常量和文字常量的确切含义.
I hope this helps. If not, please edit your questions and give an example of what you mean exactly by Constants and Literal Constants.
注意:True
和False
等是Python 3.x中的关键字,因此,如果您说True = False
,则解释器将引发SyntaxError: assignment to keyword
.
Note: True
and False
and the like are keywords in Python 3.x, so if you say True = False
, the interpreter will raise SyntaxError: assignment to keyword
.
这篇关于什么是常量和文字常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!