Site icon TestingDocs.com

Python Constants

Overview

Python language does not have built-in support for constants. Python constants are defined as variables whose values are not expected to change throughout the execution of a program. Constants act as a container that holds information that cannot be changed later.

It’s a convention to use all uppercase capital letters to name such variables to indicate that they should not be changed and to separate them from other variables.
However, this convention does not actually prevent reassignment.

Examples

In this section, we will declare some constants. Some of the examples are as follows:

Numerical Constants

We will declare two numerical constants with floating point numbers in this example. These are used for fixed values like mathematical constants or in scientific calculations.

PI = 3.14159

GRAVITY = 9.81

String Constants

These can be used for fixed strings like file paths or error messages.

CONFIG_PATH = ‘/path/to/configuration/file’
ERROR_MESSAGE = ‘Web element not found’

Configuration Constants

These constants are used in Python automation scripts. These are used to
set parameters or settings for a program.

MAX_DB_CONNECTIONS = 100
DEBUG_MODE = False

It’s important to note that Python’s syntax and interpreter do not enforce the values of constants. Rather, they are just a convention. Although it is technically possible to change the values of these variables, it is not recommended as it goes against the convention and can cause confusion or errors in the program.

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website:

https://www.python.org

Exit mobile version