Constants
In programming paradigm, Constants are the placeholders to store constant values, the values that cannot be changed during the lifetime of a program.
Python does not have any special type to hold constant values. But, there is a workaround. We can make use of the normal variables to store constant values. To differentiate Constants from the normal Variables, we may name the constants in complete uppercase.
Also, note that in Python these constants that we are “defining as constants” can be changed, but we choose not to change them, and have to take care not to change so that they remain constant.
NAME_LENGTH_LIMIT = 100
Example
In the following program, we create a variable NAME_LENGTH_LIMIT
to hold a constant value, and use it to validate the name read from user.
Python Program
NAME_LENGTH_LIMIT = 100
x = input('Enter your name : ')
if (len(x) < NAME_LENGTH_LIMIT) :
print('Good! You entered a valid name.')
else :
print('Warning! You breached the maximum length for a name.')
Output
Enter your name : Apple
Good! You entered a valid name.
Conclusion
In this Python Tutorial, we learned the concept of Constants, and how to work around in Python to define and work with constants.