A Python constant is a value that cannot be changed once it has been set. This is different from a regular variable, which can be changed.
For example, if you have a variable called name
that stores your name, you can change the value of the variable by assigning a new value to it, like this:
name = "Samantha"
# Later on, you can change the value of the "name" variable
name = "Bob"
But if you have a constant, you cannot change its value once it has been set. For example, you might have a constant called PI
that stores the value of the mathematical constant pi:
PI = 3.14
# You cannot change the value of the "PI" constant
PI = 3.15 # This will cause an error!
In Python, constants are typically written in all uppercase letters to make them easy to identify.
Quiz
- What is a Python constant?
- A. A value that cannot be changed once it has been set.
- B. A collection of unique values.
- C. A list of values.
- D. A collection of key-value pairs
- What is the purpose of using constants in Python?
- A. To make it easier to identify important values
- B. To make it easier to change the values of variables
- C. To make it easier to understand the code
- D. To make it easier to store a collection of unique values
- How are constants typically written in Python?
- A. In lowercase letters
- B. In uppercase letters
- C. In camel case
- D. In snake case