Concepts / Naming Conventions and Best Practices

Naming Conventions and Best Practices

A useful naming practice is to select a name that helps identify the value it refers to.

  • Programming
Interactive lab

Try it: Assignment, Statement by Statement

How an assignment statement evaluates its right side first (using the names' current values) and only then binds the name on the left: updating x = x + 1, augmented operators like += and //=, chained a = b = 0, simultaneous a, b = b, a + b versus one-at-a-time assignment, rebinding a name to a value of another type, the NameError for a name that was never assigned, and the SyntaxError for a name that breaks the naming rules.

How it works

  1. Before anything runs, Python compiles the whole program; a name that breaks the rules (starts with a digit, contains a space or a dash, or is a keyword) is a SyntaxError and no line runs at all.
  2. For each assignment, evaluate the complete right side first, looking every name up in the variable table as it is now.
  3. Then bind the name(s) on the left to that value: a new name is added to the table, an existing one is replaced (even by a value of a different type).
  4. x += 5 reads x, applies the operator (+, -, *, //, %, **) and binds the result back to x; a = b = 0 evaluates 0 once and binds a, then b.
  5. a, b = b, a + b builds the whole right-hand tuple from the OLD values before binding a and b; two separate statements would use the new a in the second line.
  6. Reading a name that is not in the table raises NameError and the program stops at that line.

Default run (10 steps): A 4-line program. Python first compiles all of it, then runs it top to bottom. The variable table on the right starts empty. … Program finished. Final table: x = 16.

Simplified: The programs are fixed templates: you choose the template, its numbers, the operator and the variable name, never the code. The lab's own small Python tokenizer and parser (written for the statements these programs use: assignments, print calls, if / elif / else, for, while, def and return) checks the text; nothing is executed as real Python. Every program the lab can show was compiled by CPython 3.12 and the lab reproduces its exception type, message, line, caret columns and the printed report. A small interpreter written for the same statements then runs them (integers of any size, the str/float results these templates produce, and print); each program was also run in CPython 3.12 under sys.settrace, and the lab reproduces the variables after every line, the output and the error.

Educational simulation

Loading the simulation…

A Name That Carries a Value

A variable is a name that refers to a value. This name-value relationship lets a program store and manipulate data. Assignment is the operation that establishes this relationship: a value is produced, and a variable name is bound to it.

scorevariable name5referred valuerefers to
What does a variable contain, and how is its name connected to the value it refers to?

Trace the Changing State

Consider three assignment statements: score = 5, score = 10, and bonus = 2. The important question is not only what each line says by itself. The important question is how the collection of name-value relationships changes after each line executes.

What do you think happens?

After score = 5, then score = 10, then bonus = 2, what value does score refer to, and what value does bonus refer to?

  • score refers to 5 and bonus refers to 2

  • score refers to 10 and bonus refers to 2

  • score refers to 10 and bonus does not exist

Reveal answer
Answer:

score refers to 10 and bonus refers to 2

The first assignment creates score and binds it to 5. The second assignment uses the existing name score and updates its binding to 10. The third assignment creates bonus and binds it to 2.

program stateno assignments yetscore5score10bonus2score10score = 5score = 10bonus = 2
What happens to each variable's name-value relationship after each assignment statement executes?
Worked example

Following Three Assignments

Determine the variables and values after score = 5, score = 10, and bonus = 2 execute in order.

  1. 1
    First assignment

    The right side produces the value 5. Because score is being used as a new variable name, Python creates score and binds it to 5.

  2. 2
    Second assignment

    The right side produces the value 10. The name score already exists, so Python updates that existing name-value relationship.

  3. 3
    Third assignment

    The right side produces the value 2. The name bonus is new, so Python creates bonus and binds it to 2.

Result

The final relationships are score referring to 10 and bonus referring to 2.

How Assignment Works

The basic assignment syntax is variable_name = value. The equals sign in this statement means assign or bind; it does not mean equals in the sense of comparing two sides. Data flows from the right side toward the variable name on the left side.

right-sideexpressionproduces a valuevalueevaluation resultvariable_namereceives the bindingevaluate firstbind
How does a value move from the right-hand side into a variable name on the left-hand side?
totalvariable name=assign or bindprice + taxevaluate firstresult is bound to
What does each part of an assignment statement mean, and in what order is it evaluated?
  1. 1
    Python first evaluates the expression on the right side of the equals sign.
  2. 2
    That evaluation produces a value.
  3. 3
    Python creates a variable with the name on the left, or updates that name if it already exists.
  4. 4
    Python binds the variable name to the value produced by the right side.

Creating and Updating Names

Assignment can have two closely related effects. If the name on the left has not been used as a variable, the assignment creates a new variable and binds it to the evaluated value. If the name already exists, the assignment updates that existing variable's binding. Tracing the name after every line prevents the common mistake of treating every assignment as the creation of a completely separate variable.

variablesno score bindingvariablesscore refers to 5score = 5
What changes in the program's stored variables when an assignment uses a name that did not exist before?

Names That Explain Values

A variable name is the part of the relationship that a reader sees first. A useful naming practice is to select a name that helps identify the value it refers to. For example, total communicates more about its intended role than x in a generated assignment such as total = 25. The assignment mechanics are the same, but the first name gives a reader a clearer starting point for tracing the program.

Name choiceAssignmentWhat the name communicates
Less informativex = 25The name refers to a value, but the value's role is not identified.
More informativetotal = 25The name gives a reader a useful clue about the value's role.

Both examples use the same assignment mechanism. The difference is how much meaning the variable name provides while someone traces the name-value relationship.

Mistakes in Assignment Tracing

  • Common mistake

    Reading the equals sign as a statement that two sides are permanently equal.

    What it looks like
    score = 5 followed later by score = 10
    Why it fails

    In an assignment statement, the equals sign means assign or bind. The later statement updates the existing name-value relationship.

    Do this instead

    Read the right side as the value-producing side and the left side as the name being created or updated.

  • Common mistake

    Assuming Python assigns the left side before evaluating the right side.

    What it looks like
    total = price + tax
    Why it fails

    Python evaluates the right side first, producing a value before binding the variable name.

    Do this instead

    Trace the expression on the right first, then record the binding under the name on the left.

  • Common mistake

    Assuming every assignment creates a new variable.

    What it looks like
    score = 5 followed by score = 10
    Why it fails

    The second statement uses a name that already exists, so it updates that existing variable.

    Do this instead

    Check whether the left-side name is new or already present in the current program state.

  • Common mistake

    Choosing a name without considering the value it refers to.

    What it looks like
    x = 25 when total = 25 would identify the value's role more clearly
    Why it fails

    The name-value relationship still works, but the shorter name gives less information to someone reading or tracing the program.

    Do this instead

    Prefer a name that helps identify the referred value when the program is read.

Practice the State Trace

EASY

Trace these assignments in order: price = 8, price = 12, and tax = 3. For each line, write the current name-value relationships. Then explain which statement creates a new variable and which statement updates an existing one.

Hints
  • Evaluate the right side of each assignment first.
  • After price = 8, record the binding for price.
  • When price = 12 executes, check whether price is already present.
  • The name tax has not appeared in the earlier assignments.
MEDIUM

Improve the name in this generated assignment without changing its value: x = 25. Explain how your replacement name would help a reader identify the value's role.

Hints
  • The source material does not require a particular spelling convention.
  • Choose a name that communicates more about the value than x does.

Key Takeaways

  1. 1
    A variable is a name that refers to a value.
  2. 2
    The assignment form is variable_name = value.
  3. 3
    The equals sign in assignment means assign or bind, and data flows from the right side to the left-side name.
  4. 4
    Python evaluates the right side first, then creates or updates the variable named on the left.
  5. 5
    Clearer variable names make name-value relationships easier to identify while tracing a program.

At a glance

  • A variable is a name that refers to a value.
  • An assignment evaluates the right side first and binds the result to the name on the left.
  • An assignment with a new name creates a variable; an assignment with an existing name updates it.
  • Tracing each assignment in order reveals the program's current name-value relationships.
  • Names that help identify their referred values make those relationships easier to follow.

Related concepts

More concepts starting with N