Sep-22-2020, 05:36 PM
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
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