Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What Are Python Literals?
#1
What Are Python Literals?

I have been doing a lot of research and keep coming across vague and conflicting definitions of what literals are in Python.

The most common vague description is a literal is literally the value you are wanting to assign to a variable name. For example: in num = 5, the 5 is literally what it says it is. That just doesn't say anything at all to me. Couldn't I also say that a file is also literally what it says it is? If I make a computer class, isn't that literally what I say it is?

Another common description is that a literal is a shortcut constructor to common data types such as integers, strings, and Booleans. For example:
instead of writing: num = int(5)
I just write out: num = 5

Now I know that int() method is commonly used to change data types (such as int(5.0). But is it not also the constructor for the int class?

Thanks!
Julie
Reply
#2
There are probably different (inconsistent) ways of using the term.

But the most common thing is that it's just a way to define a particular constant object. The main types you see this with in core python are strings and numbers.

# string literals:
x = "a string" # string literal
# number literals:
y = 5 # numeric literal (an int)
z = 3.5 # float literal
zz = 7 + 6j # complex literal
Quote: Couldn't I also say that a file is also literally what it says it is?
You could, but we don't define files in python. So there is no literal constructor for a file.

Quote:If I make a computer class, isn't that literally what I say it is?
It is, but as that's code rather than a runtime assignment, I wouldn't call that a python literal. I think the idea is to distinguish it from other constructors.
Reply
#3
Ive always understood literals as data in a string or some form of numerical data (int/float/etc,)
Recommended Tutorials:
Reply
#4
https://en.wikipedia.org/wiki/Literal_(c...ogramming)

PEP586 Literal Types may also be useful
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Sep-22-2020, 06:28 PM)bowlofred Wrote: But the most common thing is that it's just a way to define a particular constant object. The main types you see this with in core python are strings and numbers.

Thanks. Since Python doesn't really have a constant data type, is anything constant in Python? Since all variables names can be changed to point to a different object at any time.
Reply
#6
Values like integers, floats, strings and others are immutable and can't be changed, but you can change what a variable refers to.
Reply
#7
(Sep-22-2020, 07:55 PM)ndc85430 Wrote: Values like integers, floats, strings and others are immutable and can't be changed, but you can change what a variable refers to.

Awesome. Another question to help clarify things. When I hear immutable vs. mutable, I think of the list data type (mutable) vs. tuple data type (immutable). But, I also see list and dictionary data type included as literals...even though they are both mutable. Are these exceptions to the rule for literals?

Also, would you define a literal as the value as you see it? What exactly does that mean? Can you compare the differences between a literal and non-literal? What makes one a literal and the other not a literal?
Reply
#8
Literal does not have to do with the type of thing, it has to do with the representation of the thing. This is a literal list:
[1, 2, 3, 4]
If I type:
x = [1, 2, 3, 4]
I have a list which is stored in x, but that list is not the literal [1, 2, 3, 4]. Instead it is a list that was constructed from the literal list. I don't think the literal list exists at all. The literal list is only code that tells Python to create a list. If there was such a thing as a "literal list" python object, the list in x should be the same as [1, 2, 3, 4], but you can see by looking at the Python object ID that they are not.
x = [1, 2, 3, 4]
print(x, id(x))
print([1, 2, 3, 4], id([1, 2, 3, 4]))
Output:
[1, 2, 3, 4] 1635969131264 [1, 2, 3, 4] 1635938109504
Notice that the list referenced by "x" is not the same list that is created by the literal list. It is quite possible (likely) that the two literal lists in the second print command created two different Python objects.
Reply
#9
(Sep-22-2020, 08:57 PM)Julie Wrote: Another question to help clarify things. When I hear immutable vs. mutable, I think of the list data type (mutable) vs. tuple data type (immutable). But, I also see list and dictionary data type included as literals...even though they are both mutable. Are these exceptions to the rule for literals?

Probably depends on exactly what definition you're using.

I might (loosely) call this a literal dict constructor, or a literal dict assignment:
d = {'a': 1}
But if you go through the official python documentation, it doesn't use that term. The documentation only uses "literal" for strings (and bytes) and numbers, all of which are immutable. So I'm not sure that nitpicking over the specifics helps much here. You know the difference between mutable and immutable types, and you know how to create both. Some guides might use the term beyond that used in the documentation.

Quote:Also, would you define a literal as the value as you see it? What exactly does that mean? Can you compare the differences between a literal and non-literal? What makes one a literal and the other not a literal?

In my mind I think of the literal as a specific assignment, where the compiler is parsing the source to create (or create a reference to) a specific object. Nothing has to be constructed at runtime. I don't know that such a thought is particularly helpful or necessary to understand how the code works, thought. Outside of your question making me think about this, I've never considered it much before.
Reply
#10
(Sep-22-2020, 11:58 PM)bowlofred Wrote:
(Sep-22-2020, 08:57 PM)Julie Wrote: Another question to help clarify things. When I hear immutable vs. mutable, I think of the list data type (mutable) vs. tuple data type (immutable). But, I also see list and dictionary data type included as literals...even though they are both mutable. Are these exceptions to the rule for literals?

Probably depends on exactly what definition you're using.

I might (loosely) call this a literal dict constructor, or a literal dict assignment:
d = {'a': 1}
But if you go through the official python documentation, it doesn't use that term. The documentation only uses "literal" for strings (and bytes) and numbers, all of which are immutable. So I'm not sure that nitpicking over the specifics helps much here. You know the difference between mutable and immutable types, and you know how to create both. Some guides might use the term beyond that used in the documentation.

Quote:Also, would you define a literal as the value as you see it? What exactly does that mean? Can you compare the differences between a literal and non-literal? What makes one a literal and the other not a literal?

In my mind I think of the literal as a specific assignment, where the compiler is parsing the source to create (or create a reference to) a specific object. Nothing has to be constructed at runtime. I don't know that such a thought is particularly helpful or necessary to understand how the code works, thought. Outside of your question making me think about this, I've never considered it much before.

Thank you for taking the time to answer. Smile

(Sep-22-2020, 11:51 PM)deanhystad Wrote: Literal does not have to do with the type of thing, it has to do with the representation of the thing. This is a literal list:
[1, 2, 3, 4]
If I type:
x = [1, 2, 3, 4]
I have a list which is stored in x, but that list is not the literal [1, 2, 3, 4]. Instead it is a list that was constructed from the literal list. I don't think the literal list exists at all. The literal list is only code that tells Python to create a list. If there was such a thing as a "literal list" python object, the list in x should be the same as [1, 2, 3, 4], but you can see by looking at the Python object ID that they are not.
x = [1, 2, 3, 4]
print(x, id(x))
print([1, 2, 3, 4], id([1, 2, 3, 4]))
Output:
[1, 2, 3, 4] 1635969131264 [1, 2, 3, 4] 1635938109504
Notice that the list referenced by "x" is not the same list that is created by the literal list. It is quite possible (likely) that the two literal lists in the second print command created two different Python objects.


Thank you. Very helpful. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace in code vs literals Skaperen 3 2,442 Mar-30-2021, 12:37 AM
Last Post: Skaperen
  string literals in a list. Not what I expected. tycarac 3 2,680 Dec-28-2019, 05:31 AM
Last Post: tycarac
  parenthesis around a tuple of literals in a for Skaperen 2 2,186 Aug-25-2019, 03:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020