Python Constant Last Updated : 30 Dec, 2024 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 infoAdvertise with us Next Article Python Constant 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 Naming Conventions Python, known for its simplicity and readability, places a strong emphasis on writing clean and maintainable code. One of the key aspects contributing to this readability is adhering to Python Naming Conventions. In this article, we'll delve into the specifics of Python Naming Conventions, covering 4 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 Like