Python constants

In a programming language, when variables that hold a value and cannot be changed during the whole program execution are called constants. Constant value is only can change by the programmer, not by the program that is the characteristic of constants. This tutorial you will learn about Python constants.


Objectives

  • Create constants and use it

  • Constants declaration rules

  • Importance of constants


Create Python constants and use it

# Declare Constant variable
PI = 3.14
ACTIVE = "A"

# Access Constant variable
print(PI)


Python constants declaration rules

  • Constant names should be uppercase (A to Z) or digits (0 to 9) or an underscore (_)

  • If the Constant name have 2 or more words, use underscore to separate them.

ACTIVE_USER = "AU"
  • Variable naming convention will apply strictly.


Importance of python constants

  • Improved readability

  • Better maintainability

  • Lower risk of errors

  • Reduced debugging needs


Python constants video tutorial