In programming, a constant is a value that is fixed and cannot be altered during the execution of the program. Constants play a key role in making our programs more robust and reliable because they help us avoid errors caused by accidental modifications of important values. In this tutorial, we will explore how to define and use constants in Python programming. We will also discuss the best practices in utilizing constants in Python programs. By the end of this tutorial, you will have a clear understanding of constants and their importance in writing effective Python code.

Table of Contents :

  • What are constants
  • Constants in Python
  • Naming convention for constants in Python

What are constants

  • A constant is a special type of variable whose value never changes. 
  • It is a good practice to use constants for values that will never change, such as the maximum and minimum number of connections. 
  •  For example :


  • Many programming languages have dedicated syntax and keywords for defining constants.

Constants in Python

  • Python do not have a strict concept of constants as such.
  • In Python we cannot forcefully declare a variable whose value will never change.
  • The only way to define a constant in Python is by indicating to other coders that this variable should not be reassigned.
  • Python community has agreed on using naming convention to indicate that a variable is declared as constant and should not be changed in future.
  • In other words Naming convention distinguishes between variables and constants in Python.
  • In Python constants are named similar to variables but the difference that distinguishes them from variables is that constants cannot have lower case letters.

Naming convention for constants in Python : 

  • A Constant can be a combination of 
    • uppercase letters (A-Z), 
    • digits (0-9) and 
    • underscores (_). 
    • The first character of a constant name must be a letter or an underscore.
  • In Python, constants are usually declared and assigned on a module level.
  • Constants can be any data type, including integers,  floating point numbers, strings, tuples etc.

Prev. Tutorial : Variables

Next Tutorial : Literals