Skip to content
beginner

Python Constants and Naming Conventions

Good names are the cheapest debugging tool you own. A variable called a tells you nothing when a script breaks; a variable called average_score tells you…

Published 2026-06-15Updated 2026-09-126 min read
A small brown mouse eating sunflower seeds in a natural setting.
A small brown mouse eating sunflower seeds in a natural setting. Photo by Owen Sellwood on Pexels.

Good names are the cheapest debugging tool you own. A variable called a tells you nothing when a script breaks; a variable called average_score tells you exactly where to look. The catch is that a name only helps if it matches what the value actually does. That is the real lesson behind Python naming conventions: the name on the page should do the explaining before you ever read a comment.

What Makes a Value a Constant?

A constant is a value you intend to keep fixed by design: the value of pi, a maximum number of users, a default timeout. Some languages lock constants so they cannot change. Python does not. A constant in Python is still an ordinary variable, and nothing stops you from reassigning it.

So how do you signal intent? By convention, you write constants in all uppercase letters with underscores between words:

PI = 3.14159
MAX_USERS = 100
DEFAULT_TIMEOUT = 30

That uppercase style is a message to every reader: this value is meant to stay fixed. Python won't enforce it, but the convention is unmistakable. PEP 8, the official Python style guide, recommends exactly this pattern for module-level constants.

Note: These are still ordinary variables. Python will happily let you write MAX_USERS = 5 later in the code. The uppercase name is a promise you and your team keep, not a rule the interpreter enforces.

Knowledge check

Check your understanding

Answer this question before you continue.

Which name best communicates that a module-level value is intended to remain fixed by design?
Single Choice

Focus: Identify how Python communicates that a value is intended to remain fixed.

The Decision Frame: Does the Value Change?

The key question is not "is this value important?" It's "should this value change while the program runs?"

A value fixed by the program's design—the tax rate, the maximum retry count, the path to a config file—is a good constant candidate. A value that changes as the program runs is a variable, even if it's important:

MAX_USERS = 100      # fixed by design: a constant
current_users = 37   # changes as people log in: a variable

current_users is just as important as MAX_USERS, but it gets a lowercase name because it's meant to move. Mixing the two up is a common beginner trap: labeling every important value a constant, or treating a constant as if it were a normal variable.

Now watch all three roles work together in one calculation:

PI = 3.14159        # constant: fixed by math
radius = 5          # variable: changes with the circle
area = PI * radius * radius
print(area)
78.53975

The code reads like a sentence. PI is a constant because its value is fixed by design. radius is a variable because it changes with the circle you're measuring. area is the result. Each name fits its role, so you can see what the calculation does without a comment.

Knowledge check

Check your understanding

Answer this question before you continue.

A program's number of currently logged-in users changes as people log in and out. How should that value normally be named?
Misconception Check

Focus: Classify a value as a constant or variable by considering whether it changes during program execution.

A Naming Decision Guide

Flowchart beginning with identifying a value's role, then asking whether it changes. Fixed values lead to uppercase names with underscores for constants; changing values lead to lowercase snake_case names for variables; functions use lowercase snake_case, classes use CapWords, and short-lived loop counters may use short names. The final step reminds readers to choose a descriptive name and avoid collisions with keywords or built-ins.
Choose a Python naming style by identifying the role, checking whether the value changes, and selecting a clear name.

When you meet a new value, run it through one short process: identify the role, ask whether it changes, choose the style, then check for collisions.

Identify the role. Is this a fixed setting, a changing measurement, a function, or a throwaway loop counter? The role decides the style:

  • Constants: all uppercase with underscores. MAX_USERS, DEFAULT_TIMEOUT.
  • Variables and functions: lowercase with underscores (snake_case). user_name, total_score, calculate_total.
  • Classes: each word capitalized with no underscores (CapWords), like ShoppingCart or UserProfile. This article focuses on constants, variables, and functions, but knowing the class style keeps you from assuming every name is snake_case.
  • Temporary loop variables: short names are fine. i, x, or temp work inside a short loop, but not for data that matters.

Ask whether it changes. If the value is meant to stay fixed by design, uppercase it and treat it as a constant. If it moves as the program runs, lowercase it and let the name say what it does.

Choose a descriptive, not wordy, name. average_score beats a or score1, but average_score_for_all_completed_quizzes is too much. A name that forces you to stop and think is a name doing your debugging for you—in the wrong direction.

Check for collisions. Python draws a line between rules it enforces and conventions it merely recommends, and beginners often blur the two. That boundary is worth one careful look, because it produces the most confusing failure a beginner can hit.

Knowledge check

Check your understanding

Answer this question before you continue.

Which pairing follows the naming guide in the article?
Single Choice

Focus: Match common Python name styles to the roles they represent.

Enforced Rules vs. Conventions

Enforced rules: You cannot use Python keywords as variable names. for, if, while, and def are reserved by the language, and trying to use one raises a syntax error:

for = 5
  File "<stdin>", line 1
    for = 5
        ^
SyntaxError: invalid syntax

Conventions: Names like list and dict are not keywords. They're built-in names, and Python will let you assign to them. That runs without an error—until you need the real built-in and it's gone:

list = [1, 2, 3]
print(list("hello"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

You just shadowed the built-in list with your own variable, so list("hello") no longer builds a list from a string. The repair is simple: rename your variable to something descriptive like items, and the built-in comes back.

items = [1, 2, 3]
print(list("hello"))
['h', 'e', 'l', 'l', 'o']

That is the practical rule: avoid shadowing built-ins even though Python allows it, because a descriptive name prevents a confusing failure you would otherwise have to debug.

Knowledge check

Check your understanding

Answer this question before you continue.

This code raises a TypeError because the variable shadows the built-in `list`. Which replacement fixes the problem while preserving the stored values?
Debugging

Focus: Recognize and repair shadowing of a Python built-in name.

list = [1, 2, 3]
print(list("hello"))

Make Naming a Habit

Naming conventions and clear constants aren't style for its own sake. They're a practical way to write code you can actually debug, share, and reuse. The decision rule is compact: if a value is meant to stay fixed by design, name it in uppercase and treat it as a constant; if it moves, name it in lowercase and let the name say what it does. Uppercase communicates intent—it never enforces it.

Next step: Take a small script you've already written and refactor it in one pass. Turn every fixed setting into an uppercase constant, rename any vague values into descriptive snake_case names, and replace any variable that shadows a built-in. Before you run it, read each name aloud and ask whether it tells you what the value does. After you step away for a while, come back and see how much faster you can follow your own code.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

What happens if Python code later assigns a new value to a name written in uppercase, such as MAX_USERS?
Question 1 of 2Misconception Check

Focus: Distinguish Python's constant naming convention from an interpreter-enforced rule.

MAX_USERS = 100
MAX_USERS = 5
Which statement accurately summarizes the article's warning about names such as `for` and `list`?
Question 2 of 2Single Choice

Focus: Distinguish reserved keywords from built-in names that can be shadowed.

References

  1. PEP 8 – Style Guide for Python Code | peps.python.orgpython.org
8sources checked
8source domains
5searches run

Research updated Sep 5, 2026

Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python Starter Bundle

A focused collection of beginner-friendly Python resources to help you move from setup to building practical projects.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

Close-up view of HTML and CSS code displayed on a computer screen, ideal for programming and technology themes.
beginner
11 min read

Basic Math in Python

Python does not make you memorize a calculator manual. It hands you a small set of operators and lets you run the calculation and read the answer…

Read tutorial
Close-up of a large pot filled with black dye used in traditional incense stick production indoors.
beginner
9 min read

Python Comments and Code Style

Comments are not for Python. They are for the next human who reads your code—and that human is often you, six weeks later. The interpreter skips every line…

Read tutorial