Python Constants

Summary: in this tutorial, you’ll learn how to define Python constants.

Sometimes, you may want to store values in variables. But you don’t want to change these values throughout the execution of the program.

To do it in other programming languages, you can use constants. The constants are like variables but their values don’t change during the program execution.

The bad news is that Python doesn’t support constants.

To work around this, you use all capital letters to name a variable to indicate that the variable should be treated as a constant. For example:

FILE_SIZE_LIMIT = 2000Code language: Python (python)

When encountering variables like these, you should not change their values. These variables are constant by convention, not by rules.

Summary

  • Python doesn’t have built-in constant types.
  • By convention, Python uses a variable whose name contains all capital letters to define a constant.
Did you find this tutorial helpful ?