I'm learning Python and I am confused with the constants and literal constants. What are they? For what kind of purpose do we use them? What is the difference from the normal variable?
I'm a true beginner. As beginner, I can say I know nothing about the programming world. For example, I don't know what an expression is and vice versa.
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. Literal Constants
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!" .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.
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? Another vague point is that the number 2 is a constant because its value cannot be changed. How is it possible? We can change it, like:
stack = 2
stack = 3
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.
Best Answer
Answer after OP's edit
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
andhello
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: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 namestack
is pointing to the value2
. Then, by sayingstack = 3
, you are not changing the value2
; you are now making the namestack
to point to another value,3
.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 saystack = 2
but then you cannot ever change whatstack
is pointing to or you'll get an error. In Python, this concept does not exist.Original Answer:
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:
As for "constants", you cannot declare a variables as "true constants" in Python. There are a Built-in Constants like
True
andFalse
andNone
in Python but even they are not"true constants" in Python 2.X as they can be assigned to point to another value:I hope this helps. If not, please edit your questions and give an example of what you mean exactly by Constants and Literal Constants.
Note:
True
andFalse
and the like are keywords in Python 3.x, so if you sayTrue = False
, the interpreter will raiseSyntaxError: assignment to keyword
.