Python Constant Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Python, constants are variables whose values are intended to remain unchanged throughout a program. They are typically defined using uppercase letters to signify their fixed nature, often with words separated by underscores (e.g., MAX_LIMIT). Let's understand with the help of example: Python # Mathematical constant PI = 3.14159 # Acceleration due to gravity GRAVITY = 9.8 print(PI) print(GRAVITY) Output3.14159 9.8 Table of ContentRules to be followed while declaring a ConstantExample of Constant 1. Mathematical Constant:2. Configuration settings:3. UI Color Constants:How to create immutable constants?Rules while declaring a ConstantPython constant and variable names can include:Lowercase letters (a-z)Uppercase letters (A-Z)Digits (0-9)Underscores (_)Naming rules for constants:Use UPPERCASE letters for constant names (e.g., CONSTANT = 65).Do not start a constant name with a digit.Only the underscore (_) is allowed as a special character; other characters (e.g., !, #, ^, @, $) are not permitted.Best practices for naming constants:Use meaningful and descriptive names (e.g., VALUE instead of V) to make the code clearer and easier to understand.Example of Constant There are multiple use of constants here are some of the examples:1. Mathematical Constant: Python PI = 3.14159 E = 2.71828 GRAVITY = 9.8 2. Configuration settings: Python MAX_CONNECTIONS = 1000 TIMEOUT = 15 3. UI Color Constants: Python BACKGROUND_COLOR = "#FFFFFF" TEXT_COLOR = "#000000" BUTTON_COLOR = "#FF5733" These examples are valid and align with the described naming conventions.How to create immutable constants?The example using namedtuple is correct and demonstrates how to create immutable constants. Here's why this works:A namedtuple creates a lightweight, immutable object where fields can be accessed like attributes.While you can access values using constants.PI, you cannot modify them. Python from collections import namedtuple Constants = namedtuple('Constants', ['PI', 'GRAVITY']) constants = Constants(PI=3.14159, GRAVITY=9.8) # constants.PI = 3.14 print(constants.PI) Output3.14159 Comment More info H harshitwn5p Follow Improve Article Tags : Python python-basics python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like