- Naming Convention
- Example 1: Defining a Constant
- Example 2: Using Constants in Calculations
- Example 3: Multiple Constants
- Example 4: Constants Can Still Be Modified
- Example 5: Constants Improve Readability
- Example 6: Avoiding Magic Numbers
- Example 7: Constants Containing Strings
- Example 8: Constants Used Multiple Times
- Common Mistakes
- Best Practices
- Key Points to Remember
A constant is a value that is intended to remain unchanged throughout the execution of a program.
Unlike languages such as C, C++, or Java, Python does not have built-in support for constant variables. Any variable can be modified after it is created.
However, by convention, programmers write constant names using uppercase letters to indicate that their values should not be changed.
Naming Convention #
Constants are usually written using uppercase letters with words separated by underscores.
Examples:
PI = 3.14159
MAX_SIZE = 100
GRAVITY = 9.8
COMPANY_NAME = "Edcret"
Note: This is only a naming convention. Python does not prevent these values from being modified.
Example 1: Defining a Constant #
main.py
PI = 3.14159
print(PI)
Output #
3.14159
Explanation #
PIis intended to represent a constant value.- The variable name is written in uppercase.
- This tells other programmers that the value should not be modified.
Example 2: Using Constants in Calculations #
main.py
PI = 3.14159
radius = 5
area = PI * radius * radius
print(area)
Output #
78.53975
Explanation #
PIstores the mathematical constant ฯ.radiusstores the radius of the circle.- The formula
ฯ ร rยฒis used to calculate the area. - Using a constant avoids writing the same value repeatedly.
Example 3: Multiple Constants #
main.py
MAX_USERS = 100
MIN_PASSWORD_LENGTH = 8
print(MAX_USERS)
print(MIN_PASSWORD_LENGTH)
Output #
100
8
Explanation #
- Programs often contain values that remain fixed.
- Giving these values meaningful constant names improves readability.
- If a value ever changes, it only needs to be updated in one place.
Example 4: Constants Can Still Be Modified #
main.py
PI = 3.14159
print(PI)
PI = 3.14
print(PI)
Output #
3.14159
3.14
Explanation #
- Python allows the value of
PIto be changed. - Although this is valid syntax, it is considered poor programming practice.
- Programmers are expected to treat uppercase variables as constants and avoid modifying them.
Example 5: Constants Improve Readability #
main.py
DISCOUNT_PERCENT = 10
price = 500
discount = price * DISCOUNT_PERCENT / 100
print(discount)
Output #
50.0
Explanation #
DISCOUNT_PERCENTclearly describes the purpose of the value.- Using a named constant makes the code easier to understand than writing the number
10directly. - If the discount changes, only the constant needs to be updated.
Example 6: Avoiding Magic Numbers #
main.py
PASS_MARK = 40
marks = 55
print(marks >= PASS_MARK)
Output #
True
Explanation #
- The value
40has been stored in the constantPASS_MARK. - This avoids using unexplained numbers directly in the program.
- Such unnamed values are often called magic numbers.
- Replacing magic numbers with constants makes code more meaningful.
Example 7: Constants Containing Strings #
main.py
COMPANY_NAME = "Edcret"
print("Welcome to", COMPANY_NAME)
Output #
Welcome to Edcret
Explanation #
- Constants are not limited to numeric values.
- They can also store strings, Boolean values, or other objects.
- Uppercase names indicate that the value should remain unchanged.
Example 8: Constants Used Multiple Times #
main.py
TAX_RATE = 18
price1 = 100
price2 = 250
print(price1 * TAX_RATE / 100)
print(price2 * TAX_RATE / 100)
Output #
18.0
45.0
Explanation #
- The same constant is used in multiple calculations.
- This avoids duplicating the value
18. - Updating the tax rate requires changing only one line.
Common Mistakes #
1. Writing Constants in Lowercase #
Poor Practice
pi = 3.14159
Better Practice
PI = 3.14159
Reason
Uppercase names clearly indicate that the value is intended to be constant.
2. Modifying a Constant #
Poor Practice
MAX_SIZE = 100
MAX_SIZE = 200
Reason
Although Python allows this, constants should not be modified after they are defined.
3. Using Magic Numbers #
Poor Practice
radius = 5
area = 3.14159 * radius * radius
Better Practice
PI = 3.14159
radius = 5
area = PI * radius * radius
Reason
Named constants make code easier to read and maintain.
Best Practices #
- Write constant names in uppercase.
- Use underscores to separate words in constant names.
- Avoid changing constant values after they are defined.
- Replace unexplained numeric values (magic numbers) with meaningful constants.
- Choose descriptive names that clearly indicate the purpose of the constant.
Key Points to Remember #
- A constant is a value that is intended to remain unchanged.
- Python does not provide true constant variables.
- Uppercase variable names are used by convention to represent constants.
- Constants improve code readability and maintainability.
- Constants help eliminate magic numbers from programs.
- Although constants can be modified in Python, doing so is considered poor programming practice.