Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Constants and Literal constants?

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?

Thank you very much.

Edit :

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.

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?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

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.

like image 425
user3722727 Avatar asked Jun 09 '14 14:06

user3722727


People also ask

What are constants and literal?

A literal is a value that is expressed as itself. For example, the number 25 or the string "Hello World" are both literals. A constant is a data type that substitutes a literal. Constants are useful in situations where. a specific, unchanging value is to be used at various times during the software program.

What is literal or constant give example?

The values assigned to each constant variable are referred to as the literals. Generally, both terms, constants, and literals are used interchangeably. For example, “const int = 5;“, is a constant expression and the value 5 is referred to as a constant integer literal.

What is constant and literal in C++?

Advertisements. Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

What are literals and constants in Java?

Literal: Any constant value which can be assigned to the variable is called literal/constant. In simple words, Literals in Java is a synthetic representation of boolean, numeric, character, or string data.


1 Answers

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
2
>>> 'hello'
'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

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.

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.

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:

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.

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.

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.

like image 138
s16h Avatar answered Oct 13 '22 01:10

s16h