Concepts / Literal Constants in Python

Literal Constants in Python

A literal constant is a value written directly in code that cannot be changed.

  • Programming

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.

5value written directlyxname that holds a value
How does a value appear directly in Python code, and how is that different from a name whose value can later be reassigned?

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.

holds5fixed valuexname5value held by x
What is the difference between a value written directly in an expression and a variable name that refers to a value?

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.

5integer'This is a string'string1.23floating-point number"It's a string!"string
What do literal constants look like for numbers and strings, and how do their written forms differ?
Written valueType described in the source
5Integer number
1.23Floating-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.

holdsholdsxholds 55literal constantxholds 1010literal constant
What changes when the variable x is assigned 10 after first holding 5?

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

EASY

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?

  • The literal constant 5 changes into 10.
  • The variable x is reassigned to a different literal constant.
  • Both x and the literal constants are unchanged.
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

  1. A literal constant is a value written directly in code.
  2. Literal means the value is used exactly as written; constant means its value is fixed.
  3. Numbers such as 5 and 1.23 and strings such as 'This is a string' are literal constants.
  4. A variable is a name that holds a value and can be reassigned to a different literal constant.
  5. 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.