What is the difference between literal and variables in Python?
22,233
Solution 1
A literal is notation for representing a fixed (const
) value.
A variable is storage location associated with a symbolic name (pointed to, if you'd like).
It's best explained in use:
foo = bar(42)
^ ^ ^
| | |--- literal, 42 is *literally* 42
| |------- function, also represents "something" in memory
|------------- variable, named "foo", and the content may vary (is variable)
Identifier on the other hand is the name assigned to a variable in a python statement.
Solution 2
In any programming language a Literal is a constant value, where as identifiers can change their values. Identifiers can store literals and process them further. Identifiers are name given to variables.
1
, 1.5
, 'a'
, "abc"
, etc. are examples for literals. But in the statement x=123
, x is a variable and 123 is a Literal.
![Wuchun Aaron](https://i.stack.imgur.com/AmXx7.jpg?s=256&g=1)
Author by
Wuchun Aaron
Updated on July 05, 2022Comments
-
Wuchun Aaron almost 2 years
I'm a beginner user for Python, but I get confused between literal and variables.
This is what I know about a literal:
"a"+"b"
And variables:
sentence="a"+"b"
-
Keith Thompson about 11 yearsAn identifier is not a variable. An identifier may be the name of a variable.