Basics

Constants

Python uses naming conventions and optional types for values that should not change.

Python has no const keyword for ordinary names. Modules use all-caps names such as MAX_RETRIES to say “treat this as fixed configuration, not changing state.”

The interpreter will still let code rebind the name. That is why constants are primarily an API and readability convention. If a project also uses static typing, Final can make the convention machine-checkable.

Named constants remove magic values from code and give repeated literals one place to change.

All-caps names communicate design intent: this value is configuration that callers should treat as fixed. Comparing against MAX_RETRIES instead of a bare 3 keeps the meaning beside the check, and gives the limit one place to change.

Source

MAX_RETRIES = 3

attempts = 2
print(attempts < MAX_RETRIES)
attempts = 3
print(attempts < MAX_RETRIES)

Output

True
False
UPPER_CASE is a naming convention, not a language constraint; the binding behaves like any other variable.

Constants are useful when a repeated literal deserves a name at the domain boundary.

Source

API_VERSION = "2026-05"
print(API_VERSION)

Output

2026-05

Final lets type checkers reject reassignment, but Python still runs ordinary rebinding at runtime.

Source

from typing import Final

MAX_RETRIES: Final = 3
MAX_RETRIES = 5
print(MAX_RETRIES)

Output

5

Notes

See also

Run the complete example

Example code

Expected output

True
False
2026-05
5

Execution time appears here after you run the example.