About this question
This question explains the common practices for creating constants in Python, including naming conventions and using modules or classes to prevent unintended changes.
This question explains the common practices for creating constants in Python, including naming conventions and using modules or classes to prevent unintended changes.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Jun 23, 2025
In Python, there’s no built-in keyword like const (like in JavaScript or C++) to define a constant. However, by following a few conventions and best practices, you can still create variables that are treated as constants.
1. Use All Uppercase Naming
The most common way to define a constant is by naming the variable in all uppercase letters:
PI = 3.14159
MAX_USERS = 100 2. Define Constants in a Separate Module
You can also create a separate Python file, like constants.py, to store all your constants:
# constants.py
API_KEY = "your_api_key"
TIMEOUT = 30
# main.py
from constants import API_KEYKeeps your code cleaner and easier to maintain.
3. Use typing.Final (Python 3.8+)
If you’re using type hints, Python 3.8 introduced Final from the typing module:
from typing import Final
PI: Final = 3.14159This tells tools like linters and type checkers that PI shouldn't be reassigned.
Remember:
So, while you can't technically create a constant in Python, you can get pretty close with clear naming and good habits!
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…