Python

How to define constant in Python

April 14, 2022

How to define constant in Python

In this article you will learn how to define a constant in Python, also what constants are for in Python and how we should use them

define constant in Python thumb

Hey you programmer, ok? Let’s learn more about constants in the Python language!

In fact, it is still not possible to create a constant in Python as in other languages, where its value is not changed.

At least not in the year I am writing this article.

However, in the documentation, if you choose to create a variable with constant syntax, there are some instructions to follow

All letters of the variable that will serve as constant must be uppercase.

And also if there is more than one word they must be separated by an underscore

Let’s see two examples:

CONSTANT = 1
CONSTANTE_TWO = 2

This is the recommended pattern in the Python language documentation

You can leave an inline comment explaining that the variable is actually a constant

Note: Maybe in the future they will add the constant statement in Python, so I will update this article as the documentation provides this information

Conclusion

In this article we learned how to define a constant in Python – which actually doesn’t have its own syntax

And yes to delimit the variable with a syntax that resembles the constants of other languages, as indicated in the documentation

Do you want to learn more about Python? Click here!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x