Skip to main content

Section 3.14 Modifying Variables

As we have mentioned previously, it is legal to make more than one assignment to the same variable. A new assignment makes an existing variable refer to a new value (and stop referring to the old value).
The first time bruce is printed, its value is 5, and the second time, its value is 7. The assignment statement changes the value (the object) that bruce refers to.
Here is what reassignment looks like in a reference diagram:
It is important to note that in mathematics, a statement of equality is always true. If a is equal to b now, then a will always equal to b. In Python, an assignment statement can make two variables refer to the same object and therefore have the same value. They appear to be equal. However, because of the possibility of reassignment, they don’t have to stay that way:
Line 4 changes the value of a but does not change the value of b, so they are no longer equal. We will have much more to say about equality in a later chapter.

Subsection 3.14.1 Developing your mental model of How Python Evaluates

Its important to start to develop a good mental model of the steps Python takes when evaluating an assignment statement. In an assignment statement Python first evaluates the code on the right hand side of the assignment operator. It then gives a name to whatever that is. The (very short) visualization below shows what is happening.

Checkpoint 3.14.1.

There was once a (temporary) Runestone exercise here, which would only render in HTML output, and never in static output forms. That (temporary) device is no longer supported as of 2025-11-04, since the exercise should now be authored in supported PreTeXt syntax. You might alert the author to this situation.
In the first statement a = 5 the literal number 5 evaluates to 5, and is given the name a. In the second statement, the variable a evaluates to 5 and so 5 now ends up with a second name b.

Note 3.14.2.

In some programming languages, a different symbol is used for assignment, such as <- or :=. The intent is that this will help to avoid confusion. Python chose to use the tokens = for assignment, and == for equality. This is a popular choice also found in languages like C, C++, Java, and C#.
Check your understanding

Checkpoint 3.14.3.

After the following statements, what are the values of x and y?
x = 15
y = x
x = 22
  • x is 15 and y is 15
  • Look at the last assignment statement which gives x a different value.
  • x is 22 and y is 22
  • No, x and y are two separate variables. Just because x changes in the last assignment statement, it does not change the value that was copied into y in the second statement.
  • x is 15 and y is 22
  • Look at the last assignment statement, which reassigns x, and not y.
  • x is 22 and y is 15
  • Yes, x has the value 22 and y the value 15.

Subsection 3.14.2 Updating Variables

One of the most common forms of reassignment is an update where the new value of the variable depends on the old. For example,
x = x + 1
This means get the current value of x, add one, and then update x with the new value. The new value of x is the old value of x plus 1. Although this assignment statement may look a bit strange, remember that executing assignment is a two-step process. First, evaluate the right-hand side expression. Second, let the variable name on the left-hand side refer to this new resulting object. The fact that x appears on both sides does not matter. The semantics of the assignment statement makes sure that there is no confusion as to the result. The visualizer makes this very clear.

Checkpoint 3.14.4.

There was once a (temporary) Runestone exercise here, which would only render in HTML output, and never in static output forms. That (temporary) device is no longer supported as of 2025-11-04, since the exercise should now be authored in supported PreTeXt syntax. You might alert the author to this situation.
If you try to update a variable that doesn’t exist, you get an error because Python evaluates the expression on the right side of the assignment operator before it assigns the resulting value to the name on the left. Before you can update a variable, you have to initialize it, usually with a simple assignment. In the above example, x was initialized to 6.
Updating a variable by adding something is called an increment; subtracting is called a decrement. Sometimes programmers talk about incrementing or decrementing without specifying by how much; when they do they usually mean by 1. Sometimes programmers also talk about bumping a variable, which means the same as incrementing it by 1.
Incrementing and decrementing are such common operations that programming languages often include special syntax for it. In Python += is used for incrementing, and -= for decrementing. In some other languages, there is even a special syntax ++ and -- for incrementing or decrementing by 1. Python does not have such a special syntax. To increment x by 1 you have to write x += 1 or x = x + 1.πŸ”—
Imagine that we wanted to not increment by one each time but instead add together the numbers one through ten, but only one at a time.
After the initial statement, where we assign s to 1, we can add the current value of s and the next number that we want to add (2 all the way up to 10) and then finally reassign that that value to s so that the variable is updated after each line in the code. This will be tedious when we have many things to add together. Later you’ll learn about an easier way to do this kind of task.
Check your understanding

Checkpoint 3.14.5.

What is printed when the following statements execute?
x = 12
x = x - 1
print(x)
  • 12
  • The value of x changes in the second statement.
  • -1
  • In the second statement, substitute the current value of x before subtracting 1.
  • 11
  • Yes, this statement sets the value of x equal to the current value minus 1.
  • Nothing. An error occurs because x can never be equal to x - 1.
  • Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned.

Checkpoint 3.14.6.

What is printed when the following statements execute?
x = 12
x = x - 3
x = x + 5
x = x + 1
print(x)
  • 12
  • The value of x changes in the second statement.
  • 9
  • Each statement changes the value of x, so 9 is not the final result.
  • 15
  • Yes, starting with 12, subtract 3, than add 5, and finally add 1.
  • Nothing. An error occurs because x cannot be used that many times in assignment statements.
  • Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned.

Checkpoint 3.14.7.

Construct the code that will result in the value 134 being printed.

Checkpoint 3.14.8.

Which of the following statements are equivalent?
  • x = x + y
  • x is updated to be the old value of x plus the value of y.
  • y += x
  • y is updated to be the old value of y plus the value of x.
  • x += x + y
  • This updates x to be its old value (because of the +=) plus its old value again (because of the x on the right side) plus the value of y, so it’s equivalent to x = x + x + y
  • x += y
  • x is updated to be the old value of x plus the value of y.
  • x++ y
  • ++ is not a syntax that means anything in Python.

Note 3.14.9.

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
You have attempted of activities on this page.