Literal Constants in Python
A literal constant is a value written directly in code that cannot be changed.
Values Written Directly
When a value appears directly in Python code, that value is a literal constant. For example, the number 5 and the text 'This is a string' are values written directly in the code. You use each value as it appears, rather than referring to a name that represents it.
Why the Name Fits
The word literal emphasizes that you use the value exactly as it is written. When you type 5 into code, you are using 5 itself; you are not referring to something named 5. The word constant emphasizes that the value is fixed. Once 5 is written as a literal constant, it remains 5. You cannot reassign or modify that literal in the way you can change what value a variable holds.
Numbers and Strings
Literal constants can appear in different data types. The source examples include the integer 5, the floating-point number 1.23, and strings such as 'This is a string' and "It's a string!". In every case, the value is written directly in the code and cannot be changed as a literal constant.
| Written value | Type described in the source |
|---|---|
| 5 | Integer number |
| 1.23 | Floating-point number |
| 'This is a string' | String |
| "It's a string!" | String |
Examples of literal constants provided in the source
Variable Reassignment
Following x from 5 to 10
Distinguish the literal constants from the variable in the sequence x = 5 followed later by x = 10.
First assignment: In x = 5, x is the variable name and 5 is a literal constant. The variable x holds the value 5.
Second assignment: In x = 10, x is still the variable name, but it now holds a different literal constant: 10.
What changed: The variable's held value changed from 5 to 10. The literal constants 5 and 10 did not change; each remained the value written in the code.
A variable can be reassigned to a different literal constant, while the literal constants themselves remain fixed.
Common Naming Confusions
Treating the variable name and its value as the same thing.
The source distinguishes x as the name that holds a value from 5 as the literal constant itself.
Fix:
Identify x as the variable and 5 as the literal constant.Saying that a literal constant changes when a variable is reassigned.
Only the value held by x changes. The literal constants 5 and 10 remain fixed.
Fix:
Say that x was reassigned from holding 5 to holding 10.Forgetting what literal means.
Literal emphasizes that the value is used directly as it appears in the code.
Fix:
Read 5 as a value written directly in the code, not as a name that refers to another value.
Check Your Understanding
Consider the sequence x = 5 followed later by x = 10. Identify every literal constant and identify the variable. Then explain which item changes and which items remain fixed.
Hints
- Look for values written directly in the assignments.
- Look for the name that holds a value.
- Compare what happens to x with what happens to the literal constants 5 and 10.
What do you think happens?
In x = 5 followed by x = 10, which statement is accurate?
Reveal answer
Answer: The variable x is reassigned to a different literal constant.
The source identifies x as a variable that can hold different literal constants. The literal constants 5 and 10 themselves remain fixed.
Key Takeaways
- A literal constant is a value written directly in code.
- Literal means the value is used exactly as written; constant means its value is fixed.
- Numbers such as 5 and 1.23 and strings such as 'This is a string' are literal constants.
- A variable is a name that holds a value and can be reassigned to a different literal constant.
- Reassigning a variable does not change the literal constants involved.
Key Takeaways
- A literal constant is a value written directly in Python code that cannot be changed.
- The terms literal and constant describe direct use and fixed value, respectively.
- Numbers and strings are examples of literal constants.
- A variable can be reassigned, but the literal constants it holds remain fixed.