2.10. Reassignment¶
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).
bruce = 5
print(bruce)
bruce = 7
print(bruce)
(ch07_reassign1)
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:
a = 5
b = a # after executing this line, a and b are now equal
print(a, b)
a = 3 # after executing this line, a and b are no longer equal
print(a, b)
(ch07_reassign2)
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.
Note
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
data-10-1: After the following statements, what are the values of x and y?
x = 15
y = x
x = 22