How to create constant in Python Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Python lacks built-in constant support, but constants are typically represented using all-uppercase names by convention. Although they can technically be reassigned, it’s best practice to avoid modifying their values. This convention helps indicate that such variables should remain unchanged throughout the code, promoting clarity and consistency in programming.Here's an example of how we can define the constant in Python: Python pi = 3.14159 gravity = 9.8 speed = 299792458 There are also multiple ways to create constants in Python:Table of ContentUsing typing.FinalUsing namedtupleUsing typing.FinalStarting with Python 3.8, you can annotate a constant using Final from the typing module, which helps tools like mypy catch reassignment issues: Python from typing import Final pi = 3.14159 gravity= 9.8 # Attempting to reassign these constants will raise an error in static type checkers # pi = 3.14 # mypy will raise an error Using namedtupleWe can use Python's namedtuple to create immutable constants. A namedtuple provides a convenient way to define constants that are grouped logically and cannot be easily modified. Python from collections import namedtuple # Define a namedtuple for constants c = namedtuple('Constants', ['pi', 'gravity', 'speed']) # Create an instance of the namedtuple with constant values c = Constants(pi=3.14159, gravity=9.8, speed=299792458) # Access constants print(f"PI: {constants.pi}") print(f"Gravity: {constants.gravity}") print(f"Speed of Light: {constants.speed}") # Attempting to modify a value will raise an AttributeError # constants.PI = 3.14 OutputPI: 3.14159 Gravity: 9.8 Speed of Light: 299792458 Comment More infoAdvertise with us Next Article How to create constant in Python H harshitwn5p Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to create a constant matrix in Python with NumPy? A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th 4 min read How to create modules in Python 3 ? Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t 4 min read How To Convert Data Types in Python 3? Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of 4 min read How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read Constants of Maths in Python Math module is a standard in-built module in Python that can be used for performing the mathematical tasks. The math module has a set of methods and constants. Note: For more information, refer to Math-library-functions 1. Python math.e constant: The math.e constant returns the Euler's number: 2.718 2 min read Like