Concepts / Updating Variables with New Values

Updating Variables with New Values

A variable is a name that refers to a value, allowing you to store and manipulate data in your program.

  • Programming

A Name Connected to a Value

A variable is a name that refers to a value. This name-value relationship lets a program store and manipulate data. When you work with a variable, you use its name to refer to the value currently connected to it.

refers toscorevariable name10value
Which name is bound to which value?

Creating the First Binding

An assignment statement creates a new variable and binds it to a value. Its general form is variable_name = value. For example, consider the generated assignment statement points = 5. If points did not already exist, executing this statement creates the name points and binds it to the value 5.

evaluatesbinds under namepoints = 5assignment statement5evaluated valuepointsname bound to 5
How does executing an assignment create a new binding that did not previously exist?

Creating a Variable

What happens when the assignment statement points = 5 executes and points has not been used before?

Evaluate the right side: The right side produces the value 5.

Use the left-side name: The name points is used for the new variable.

Create the binding: The variable points is bound to the value 5.

The program now has a variable named points that refers to 5.

Right Side First

The equals sign in an assignment statement means assign or bind; it does not mean equals in the sense of comparing two sides. Data flows from right to left. Python first evaluates the expression on the right side, producing a value. It then creates a variable with the name on the left, or updates that variable if the name already exists.

producesbinds toRight sideevaluate the valueProduced valueevaluation resultLeft-side namecreate or update binding
Which part of an assignment happens first, and how does the result become available through the name?

Following Several Assignments

To trace a sequence of assignments, update the name-value state after each statement. In this generated sequence, the first statement creates points, the second creates level, and the third changes points. The final state must reflect all three statements in their execution order.

points = 5level = 1points = 8No bindingsbefore executionpoints: 5after points = 5points: 5; level: 1after level = 1points: 8; level: 1after points = 8
What does the program's name-to-value state look like after each assignment executes?

Tracing Three Statements

Trace the generated statements points = 5, level = 1, and points = 8 in order.

After points = 5: The right side produces 5, and the name points is bound to that value.

After level = 1: The right side produces 1, and a new name level is bound to that value. The existing points binding remains present.

After points = 8: The right side produces 8, and the existing name points is updated so that it refers to 8.

The final state has points bound to 8 and level bound to 1.

Replacing an Existing Value

Assignment is used both to create a new variable and to update an existing one. If the left-side name already exists, Python evaluates the new right-side value and binds the existing name to that value. The name remains the same, while the value associated with it changes.

refers torefers toscorenamescoresame name10value15new value
What changes when a new assignment replaces a variable's old value, and what stays the same?

When a variable is updated, write down the new state rather than combining the old and new values. For example, after score = 15 executes, the current binding is score to 15.

Mistakes in State Tracing

  • Treating the equals sign as a comparison between two already-known values.

    In an assignment statement, the equals sign means assign or bind.

    Fix: Read the statement as: evaluate the right side, then bind the result to the name on the left.

  • Evaluating the left side first.

    Python evaluates the right side first.

    Fix: Find the value produced by the right side before recording the left-side name.

  • Keeping the old value as the current value after an update.

    Assignment updates an existing variable when the name already exists.

    Fix: Record score as referring to 15 after the second assignment.

  • Forgetting that a new left-side name creates a new variable.

    An assignment statement creates a new variable when that name did not already exist.

    Fix: Add the new name-value binding to the current state.

Practice the Execution Order

EASY

Trace this generated sequence in order: total = 12, category = 3, total = 20. Write the name-value state after each assignment. Then identify which name was created by the second statement and which existing name was updated by the third statement.

Hints
  • For each statement, evaluate the right side first.
  • Add a name when it appears for the first time.
  • When total appears again, update its associated value.

What do you think happens?

After the generated statements total = 12, category = 3, and total = 20 execute in order, what value does total refer to?

  • 3
  • 12
  • 20
Reveal answer

Answer: 20

The first statement binds total to 12. The second creates category and does not change total. The third assignment updates total so it refers to 20.

Key Takeaways

  1. A variable is a name that refers to a value.
  2. The assignment form is variable_name = value.
  3. The right side is evaluated first, and its result is bound to the name on the left.
  4. An assignment creates a new variable when the name is new.
  5. An assignment updates the value associated with an existing name.

Key Takeaways

  • A variable represents a name-value relationship.
  • Assignment sends the result of the right side to the name on the left.
  • Python evaluates the right side before creating or updating the variable.
  • A new name creates a binding; an existing name receives the new value.
  • Tracing each assignment in order reveals the program's current state.