Expressions
An assignment statement tells the computer to store a value in a variable.
Watching a Variable Change
Programs spend most of their time doing one thing over and over: taking a value that already exists, doing something with it, and keeping the result. An assignment statement is the piece of program code that makes this possible. It tells the computer to store a value in a variable, and when the value being stored is produced by a computation, the statement connects the two actions together: computing with data and storing the result.
What do you think happens?
If x holds 5 and then the statement x = x + 1 runs, what is the value of x afterward?
Reveal answer
6
The right side x + 1 is computed first, using the value 5 that x currently holds, which gives 6. That 6 is then stored back into x, replacing the old 5.
How a Computed Result Is Stored
Trace the statement x = x + 1 one step at a time. The variable on the left of the equals sign is the destination. The expression on the right side is evaluated first. Evaluating it means reading the variable's current value, performing the addition, and producing a new value. Only after that new value exists does the assignment store it in the destination. The same name, x, appears on both sides, but the two appearances play different roles: on the right it supplies the old value, and on the left it receives the new one.
x = 5x holds 5
x = x + 1x holds 6
x = 5
x = x + 1
print(x)- 1Line 1 the destination x receives the value 5.
- 2Line 2 the expression x + 1 reads 5, computes 6, and the result is stored back into x.
- 3Line 3 reading x now gives 6, showing the assignment replaced the old value.
- The first line stores the literal 5 in the variable x.
- The second line is the assignment: the right side x + 1 is computed from the current value of x.
print(x)shows the value stored after the assignment finished.
6Anatomy of an Assignment Statement
An assignment statement has the general form variable = expression. The left side is the destination that receives the value. The right side describes the value-producing expression. When tracing or debugging, it helps to separate the computation from the destination that receives its result: first ask what the right side computes, then ask where that result is saved.
- interest is the destination: the variable that receives the result.
- principal is a variable whose existing value takes part in the computation.
- 0.07 is a literal value written directly in the code.
- The whole right side principal * 0.07 is the expression, and its result is stored in interest.
In the source example interest = principal * 0.07, principal participates in a multiplication, and interest is the destination for the result. The assignment therefore combines two actions: it computes with data that already exists and stores the computed value. The diagram below breaks the statement into its parts.
What Counts as an Expression
An expression is program code that represents or computes a value.
When reading a piece of program code, ask one simple question: what value does this piece represent or compute? If the code has an answer, it is an expression. An expression does not have to be complicated. It might be one literal, one variable, one function call, or a combination of these parts.
Think of an expression as a value-producing unit assembled from smaller pieces. The smallest pieces are literals, variables, and function calls. Operators such as + and > can connect these pieces into a more complicated expression. Once the larger expression has a value, that value is the thing being passed along, whether it is assigned, passed as a parameter, or combined again.
Function Calls Inside an Expression
Consider the statement total = double(4) + 1, where double is a function defined as a subroutine that returns twice its input. When the computer reaches the function call, control moves into the function. The function returns 8, and that returned value takes the place of the call in the larger expression. The expression is now 8 + 1, which produces 9, and 9 is stored in total.
def double(n):
return n * 2
total = double(4) + 1
print(total)- 1The assignment starts with its right side:
double(4)+ 1. - 2Control transfers into double with n equal to 4, which returns 8.
- 3The expression 8 + 1 produces 9.
- 4The assignment stores 9 in total, and print shows it.
- double is a subroutine that returns a value.
double(4)is a function call used as one building block of the larger expression.- The returned value replaces the call, so the expression becomes 8 + 1.
9Where an Expression's Value Goes
Once an expression has computed a value, that value has several possible uses. It can be assigned to a variable, supplied as a parameter in a subroutine call, or combined with another value to build a more complicated expression. Programming does not require every computed value to be saved. In some situations the value can be ignored.
| Piece of code | What it does | Example |
|---|---|---|
| Expression | Represents or computes a value | principal * 0.07 |
| Assignment statement | Stores a value in a variable | interest = principal * 0.07 |
| Destination | The variable that receives the value | interest |
An expression produces a value; the assignment statement saves it.
Mistakes Beginners Make with Expressions
Common mistake Treating the destination as part of the computation
What it looks likeIn interest = principal * 0.07, reading interest as an input to the multiplication.
Why it failsThe variable on the left receives the result. The computation is the right side only.
Do this insteadEvaluate the right side first, then ask where the result is stored.
Common mistake Assuming every computed value must be saved
What it looks likeWriting a function call whose result is never assigned and expecting a variable to change.
Why it failsA computed value can be used directly, passed as a parameter, or ignored. Only an assignment stores it.
Do this insteadCheck whether the code assigns the value to a variable before expecting a variable to change.
Common mistake Thinking only literals and variables can be part of an expression
What it looks likeBelieving that a call such as double(4) cannot appear inside x + 1.
Why it failsA function call supplies a returned value, so it can appear wherever an expression needs a value.
Do this insteadReplace the call mentally with the value it returns, then evaluate the rest of the expression.
Common mistake Forgetting that the old value of a variable is used on the right side before it is replaced
Practice Tracing Assignments
Trace the following program. After each line, write down the value stored in a. Then state what a holds at the end. a = 3, then a = a * 2 + 1.
Hints
- Start by finding the value of a on the right side of the second line, using the value stored after line 1.
- Compute the whole right side first: multiply the old value by 2, then add 1.
- The result replaces the old value of a in the destination.
7- After line 1, a holds 3.
- Line 2 computes 3 * 2 + 1 = 7 and stores it back into a.
- The printed value confirms the final stored result.
Key Takeaways
- An assignment statement, written variable = expression, tells the computer to store a value in a variable; the left side is the destination and the right side is the expression.
- In an assignment that uses the same variable on both sides, the right side is computed from the current value first, and only then is the new result stored.
- An expression is program code that represents or computes a value, built from literals, variables, and function calls joined by operators such as + and >.
- A function call can appear wherever an expression needs a value, because the returned value becomes part of the surrounding expression.
- An expression's value can be assigned, passed as a subroutine parameter, combined with other values, or sometimes ignored.