How do I declare a constant variable?

QuestionsQuestions8 SkillsProYour First Python LabNov, 29 2025
077

In Python, there is no built-in way to declare constants like in some other programming languages. However, a common convention is to use uppercase letters for variable names to indicate that they should be treated as constants.

Example of Declaring a Constant Variable:

PI = 3.14159
MAX_USERS = 100

Key Points:

  1. Naming Convention: Use uppercase letters with underscores to separate words (e.g., MAX_VALUE).

  2. No Enforcement: Python does not enforce immutability, so you can technically change the value of a "constant" variable, but it's discouraged.

    PI = 3.14  # This is allowed, but not recommended
  3. Using Constants: You can use these constants throughout your code, and it’s a good practice to avoid changing their values.

Example Usage:

def calculate_area(radius):
    return PI * (radius ** 2)

print(calculate_area(5))  # Outputs: 78.53975

If you have further questions about constants or any other topic, feel free to ask!

0 Comments

no data
Be the first to share your comment!