Python Constants With Advanced Examples

In this tutorial, you will learn about Python Constants. A constant is a kind of variable that has unchangeable values during execution time. In fact, constants in Python are rarely used. Typically, constants are established and assigned to a unique module or file.

Furthermore, constants have two parts, namely a name and a value. The name will give a clear idea of what the constant is, while the value will give an idea of what the constant looks like in real life.

What is a Constants in python?

In Python, Constant is a type of variable that cannot change its value, for instance constant can be thought as a simple container that holds information that cannot be changed or altered.

In addition, you can think of constants as a bag where you can put books that can’t be taken out again.

Assigning value to Python constant

In Python, assigning a value that supports constants are usually declared and assigned in a module. The module is a new file with variables and functions which is imported into the main file.

Inside the module, constants are written with all capital letters and underscores to separate the words.

For a better understanding of this kind of topic, I will give some examples below.

Also read: Const In Python In Simple Words

Example program for declaring and assigning constant

First is to create constant.py. Inside of the file declared two variables with assigning values.

For example:

HEAT = 26.2

PI = 3.14

The second thing to do is create a constant object and name it a main.py file. You must import the constant library, then call the two variables in the constant.py file.

For example:

import constant

print(constant.HEAT)
print(constant.PI)

Output:

26.2
3.14

Basic rules for naming conventions for variables and constants in Python

Names for Python constants and variables should be a mix of lowercase (a-z) or uppercase (A-Z) letters, numbers (0-9) or an underscore (_). 

For example:

phoneNumber
email_add
NAME
READ_ONLY

For naming convention, you should have to create a name which is unique and related to the data you are assigning.

For example:

Number is more readable and makes more sense than declaring only a (N).

If you want to have two or more words in a variable name, you should put an underscore between them or use a camel case naming convention.

For example:

//underscore
email_add

//camel case naming convention
phoneNumber

If ever, use capital letters to declare a constant for easy identification.

For example:

HEIGHT
WEIGHT
GENDER
NATIONALITY
RELIGION
STATUS

Don’t use special symbols which is hard to familiarize.

For example:

@, !, %, *, #, $, etc.

Finally, don’t make a variable name that starts with a number.

For example:

123number

Global constants in Python

In Python, a global variable is often set at the beginning of the program. In other words, Python variables that are declared outside of a function are called “global variables.”

In addition, a global constant is a literal value that has a name given to it. Like a global variable, you can get the value of a global constant from any script or 4GL procedure in the application to get the object attribute.

For example:

g = 0 # global variable

def add():
    global g
    g = g + 5 # increment by 2
    print("Inside add():", g)

add()
print("In main:", g)

Output:

Inside add(): 5
In main: 5

Deleting variables

In Python, deleting a variable name is easy to get rid of a variable that is not used. To free up space, we can delete any specific variable by typing del “variable name.”

For example:

g = 26
print (g)

del g
print (g)

Concatenating variables

Concatenating variables in Python programming language with different data types is easy. Like a number variable and a Python String variable, we will have to declare the number variable as a string.

Then Python will throw a TypeError if the number variable is not declared as a string variable before it is added to a string variable.

For example:

g = ‘Glenn’
l = 26
print g+l

Another example below will throw a TypeError due to variable g being a string type while variable l is an int type.

To remove this kind of error message, we need to declare the int variable as a string.

For example:

g = ‘Glenn’
l = 26
print(g + str(l))

Output:

Glenn26

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.

Leave a Comment