Using Variables in Expressions
A variable is a name that refers to a value, allowing you to store and manipulate data in your program.
Names That Refer to Values
A variable is a name that refers to a value. This name-value relationship lets a program store and manipulate data. When you use a variable in an expression, Python can use the value associated with that variable while evaluating the expression.
The diagram shows two separate relationships: the name score refers to 10, and the name bonus refers to 5. The names are not the values themselves. They provide names through which the program can refer to those values.
Reading an Assignment
An assignment statement creates a new variable and binds it to a value. Its basic syntax is variable_name = value. In this syntax, the equals sign means assign or bind; it does not express a comparison of two quantities. The data flows from right to left: Python first evaluates the right side, then associates the resulting value with the name on the left.
- Python evaluates the expression on the right side of the equals sign.
- That evaluation produces a value.
- Python creates the name on the left if it does not already exist.
- If the name already exists, Python updates its binding to the newly produced value.
Creating the First Binding
Binding a value to points
Trace the assignment statement: points = 12
Evaluate the right side: The right side produces the value 12.
Create the name: Because points is being used as a new variable name in this example, Python creates that variable.
Bind the result: Python associates points with the value 12.
After the statement executes, points refers to 12.
An assignment can create a variable that did not previously exist in the program state. The important order is evaluation first and binding second. The name points to the result produced by the right side.
Following Several Assignments
What do you think happens?
Predict the bindings after these statements execute in order: score = 10, bonus = 5, score = 15.
Reveal answer
Answer: score refers to 15 and bonus refers to 5
The first statement creates score and binds it to 10. The second creates bonus and binds it to 5. The third evaluates 15 and updates the existing score binding, so score now refers to 15.
Trace assignments one statement at a time. A later assignment with an existing name does not create a second value under that same current binding; it updates the name so that it refers to the newly evaluated result. In the example, bonus remains associated with 5 while score changes from 10 to 15.
Evaluating an expression before rebinding
Trace the sequence: score = 10, score = score + 5.
First statement: The right side produces 10, and Python binds score to 10.
Second statement: read the existing binding: The expression on the right uses the current value referred to by score, which is 10, together with 5.
Second statement: evaluate and bind: The right side produces 15. Python then updates score so that it refers to 15.
After both statements execute, score refers to 15.
Mistakes in State Tracing
Reading the equals sign as a claim that both sides are already equal.
In an assignment statement, the equals sign means assign or bind. Python evaluates the right side and then associates the result with the left-side name.
Fix:
Read the statement as: evaluate 10, then bind that result to score.Assuming the left side is evaluated before the right side.
Python evaluates the right side first. The existing value referred to by score is used to produce the new result.
Fix:
Trace the current value, evaluate the right-side expression, and only then update the left-side binding.Keeping an old value as the current binding after a later assignment.
The later statement updates the existing score binding.
Fix:
Record the newest evaluated result for that variable name.Confusing a variable name with the value to which it refers.
A variable is a name that refers to a value; the name-value relationship is the useful distinction.
Fix:
Write the state as score refers to 15.
When tracing a sequence, keep a small state record. After each assignment, write the current value associated with every variable name you have encountered. This makes updates visible and prevents an earlier value from being mistaken for the current one.
Practice the Trace
Trace these assignments in order: total = 20, extra = 3, total = 25. What value does total refer to at the end? What value does extra refer to at the end? For each statement, identify the right-side result and the name that receives it.
Hints
- Start with the first assignment and write the state after it.
- The second assignment creates or binds a different name.
- The third assignment updates total because that name already exists.
Trace this sequence: amount = 8, amount = amount + 2. Explain the order in which Python evaluates the right side and updates amount. Then state the final value associated with amount.
Hints
- Use the current binding of amount when evaluating the second right side.
- The result of the right-side expression becomes the new binding for amount.
Key Takeaways
- A variable is a name that refers to a value.
- The basic assignment form is variable_name = value.
- The equals sign in an assignment means assign or bind, with data flowing from right to left.
- Python evaluates the right side first, then creates or updates the variable named on the left.
- To trace multiple assignments, record the current binding after each statement.
Key Takeaways
- Variables represent name-value relationships.
- An assignment evaluates its right side before binding the result to the left-side name.
- An assignment can create a new variable or update an existing variable.
- Tracing each statement in order reveals the current state of every variable.