PYTHON Tutorial



CONSTANTS IN PYTHON


πŸ”’ Constants in Python

Python does not have built-in constant types like some other languages. However, by convention, constants are defined using all uppercase letters, and they should not be changed once defined.

βœ… Declaring Constants

PI = 3.14159
MAX_USERS = 100
APP_NAME = "Technorank"
  

These values should not be modified in your program.

πŸ“¦ Constants in a Module

You can store constants in a separate file (e.g., constants.py) and import them:

# constants.py
API_KEY = "123ABC"
TIMEOUT = 30

# main.py
import constants

print(constants.API_KEY)
  

⚠️ Python Doesn’t Enforce Constants

Python allows you to reassign a constant (but it's discouraged):

PI = 3.14
PI = 4   # ❌ Not recommended!
  
πŸ’‘ Best Practice: Use constants to store values that should remain unchanged throughout your code. Even though Python won’t stop you from modifying them, avoid doing so.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review