Table of contents
1.
Introduction
2.
Declaring Constants in Python
3.
Defining Constants in Python
4.
Constants vs Variables
5.
Examples of Constants in Python
5.1.
Example 1: Mathematical Constants
5.2.
Example 2: Physical Constants
6.
Benefits of Using Constants
7.
Limitations of Constants
8.
Frequently Asked Questions 
8.1.
How do you declare constants in Python?
8.2.
Can constants be modified in Python? 
8.3.
What are some examples of common Python constants?
9.
Conclusion
Last Updated: Aug 12, 2025
Easy

Python Constants

Author Sinki Kumari
0 upvote

Introduction

Python constants are values that remain unchanged throughout the execution of a program. 

Python Constants

They are used to store fixed values such as mathematical constants, physical constants, or configuration settings that are not expected to change.

Declaring Constants in Python

In Python, constants are typically declared using variables and naming conventions to indicate their immutability. Although Python doesn't have a built-in constant type, conventions are followed to treat certain variables as constants.

Defining Constants in Python

Constants can be defined using regular variables with uppercase names to signify their role as constants. Python does not enforce constant immutability, but naming conventions and programming practices maintain their intended behavior.

Constants vs Variables

Constants differ from variables primarily in their immutability and intended usage. Variables are meant to store changing data, while constants hold values that remain consistent throughout program execution.

Examples of Constants in Python

Example 1: Mathematical Constants

python

PI = 3.14159
EULER = 2.71828

Example 2: Physical Constants

python

SPEED_OF_LIGHT = 299792458  # meters per second
GRAVITATIONAL_CONSTANT = 6.67430e-11  # gravitational constant in m^3 kg^-1 s^-2

Benefits of Using Constants

Constants offer several advantages:

  • Readability: Constants improve code readability by providing meaningful names to fixed values.
     
  • Maintainability: They facilitate easy updates and changes by centralizing fixed values.
     
  • Avoiding Magic Numbers: Constants eliminate the use of magic numbers, improving code maintainability.

Limitations of Constants

While useful, constants in Python have limitations:

  • Immutability Enforcement: Python does not enforce constant immutability, relying on naming conventions and developer discipline.
     
  • Scope: Constants defined at the module level are accessible globally, potentially affecting program behavior.

Frequently Asked Questions 

How do you declare constants in Python?

Constants are declared using regular variables with uppercase names, following naming conventions.

Can constants be modified in Python? 

Python does not restrict modification of constants, but conventions are followed to treat them as immutable.

What are some examples of common Python constants?

Examples include mathematical constants like pi (PI = 3.14159) and physical constants like the speed of light (SPEED_OF_LIGHT = 299792458).

Conclusion

Python constants play a vital role in maintaining code clarity and facilitating efficient programming practices. By adhering to naming conventions and leveraging their immutability, developers can enhance code readability and maintainability across various projects.
 

Live masterclass