Basics

Constants

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

All-caps names communicate design intent: this value is configuration that callers should treat as fixed.

Source

MAX_RETRIES = 3

for attempt in range(1, MAX_RETRIES + 1):
    print(f"attempt {attempt} of {MAX_RETRIES}")

Output

attempt 1 of 3
attempt 2 of 3
attempt 3 of 3
xINT42
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

attempt 1 of 3
attempt 2 of 3
attempt 3 of 3
2026-05
5

Execution time appears here after you run the example.