Python Constant Last Updated : 30 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 infoAdvertise with us Next Article Python Docstrings H harshitwn5p Follow Improve Article Tags : Python python-basics python Practice Tags : pythonpython Similar Reads Python Docstrings When it comes to writing clean, well-documented code, Python developers have a secret weapon at their disposal â docstrings. Docstrings, short for documentation strings, are vital in conveying the purpose and functionality of Python functions, modules, and classes.What are the docstrings in Python?P 10 min read Learn Python Basics âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in 9 min read Python Crash Course If you are aware of programming languages and ready to unlock the power of Python, enter the world of programming with this free Python crash course. This crash course on Python is designed for beginners to master Python's fundamentals in record time! Experienced Python developers developed this fre 7 min read Python vs Cpython Python is a high-level, interpreted programming language favored for its readability and versatility. It's widely used in web development, data science, machine learning, scripting, and more. However, Cpython is the default and most widely used implementation of the Python language. It's written in 4 min read Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c 7 min read Python 3 basics Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the 10 min read Like