Variables, Types & Operators · Lesson 10

Valid Names and Naming Conventions

A Python identifier may contain letters, digits and underscores but cannot begin with a digit or be a reserved keyword.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Valid Names and Naming Conventions

A Python identifier may contain letters, digits and underscores but cannot begin with a digit or be a reserved keyword. Valid syntax is only the first requirement: names should also communicate meaning. Python style commonly uses snake_case for variables/functions, PascalCase for classes and UPPER_CASE for constants by convention.

Learning goal: explain why Valid Names and Naming Conventions behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Choose a valid identifier that starts with a letter or underscore.

Deeper walkthrough

Read Valid Names and Naming Conventions as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Choose a valid identifier that starts with a letter or underscore. Stage 2: Do not use reserved keywords such as class, for or if as names. Stage 3: Prefer descriptive snake_case names for ordinary variables and functions. Final checkpoint: Use the same term consistently when a variable represents the same business concept.

Mechanism

Follow the transformation

Choose a valid identifier that starts with a letter or underscore.

Do not use reserved keywords such as class, for or if as names.

Prefer descriptive snake_case names for ordinary variables and functions.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
How it works

Trace the mechanism step by step

  1. Choose a valid identifier that starts with a letter or underscore.
  2. Do not use reserved keywords such as class, for or if as names.
  3. Prefer descriptive snake_case names for ordinary variables and functions.
  4. Avoid shadowing built-ins such as list, str, sum or id.
  5. Use the same term consistently when a variable represents the same business concept.
Worked demonstration

Names that communicate meaning

# Step 1 — Compute the right-hand expression and store its result in `monthly_sales` for the next step.
monthly_sales = 12500
# Step 2 — Compute the right-hand expression and store its result in `tax_rate` for the next step.
tax_rate = 0.10
# Step 3 — Compute the right-hand expression and store its result in `net_sales` for the next step.
net_sales = monthly_sales * (1 - tax_rate)
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(net_sales)
Expected / illustrative result
The names reveal the meaning of each value; the same calculation would be harder to audit with names such as x, y and z.
Interpret the result.

For Valid Names and Naming Conventions, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.

Distinctions & related ideas

Place the concept correctly

InputObjects/values supplied to the operation.
StateNames or mutable objects that may change during execution.
OutputReturned value, side effect, file, plot or exception to inspect.
Use deliberately

When it is appropriate

Use Valid Names and Naming Conventions when it answers a defined question in Variables, Types & Operators and its inputs/assumptions match the current data or program state.

Boundary conditions

When to stop or reconsider

Reconsider Valid Names and Naming Conventions when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

Construct a tiny example of Valid Names and Naming Conventions. First choose a valid identifier that starts with a letter or underscore. Then do not use reserved keywords such as class, for or if as names. Predict the result before execution and explain one boundary or failure case.

Use the smallest values that expose the language rule. Write the expected value and type first, then compare the actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Valid Names and Naming Conventions?

Quick reference

Remember the logic

Step 1Choose a valid identifier that starts with a letter or underscore.
Step 2Do not use reserved keywords such as class, for or if as names.
Step 3Prefer descriptive snake_case names for ordinary variables and functions.
Step 4Avoid shadowing built-ins such as list, str, sum or id.
Lesson summary

What to remember

  • A Python identifier may contain letters, digits and underscores but cannot begin with a digit or be a reserved keyword. Valid syntax is only the first requirement: names should also communicate meaning. Python style commonly uses snake_case for variables/functions, PascalCase for classes and UPPER_CASE for constants by convention.
  • Choose a valid identifier that starts with a letter or underscore.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.