Course outline · 0% complete

0/27 lessons0%

Course overview →

Changing variables

lesson 3-2 · ~12 min · 9/27

Boxes can be refilled

Assigning to an existing variable replaces its contents, and the old value is simply gone:

mood = "sleepy"
mood = "focused"
print(mood)

That prints focused. There is no history and no way to ask what mood used to be.

The most important pattern in all of programming uses a variable's current value to compute its next value:

score = 0
score = score + 10

That looks impossible as math, but remember from lesson 3-1 that = means store, not equals. Python works the right side first: look up score, currently 0, add 10, then store the result of 10 back into score.

Updating a variable step by step like this is how programs count things, total things, and track state. It powers everything in the loops unit coming up.

Two updates in a row

score starts at 0, gains 10, then gains 5.

score = 0
score = score + 10
score = score + 5
print(score)

Output

15

Tracking the box contents line by line gives 0, then 10, then 15. Each line reads the current value and writes a new one.

Reading a program this way, one variable at a time down the page, is the single most useful debugging habit there is. When a total comes out wrong, you find the line where the tracked value stopped matching what you expected.

An update that subtracts

print(lives) outputs 2.

lives = 3
lives = lives - 1
print(lives)

Python evaluates the right side first using the current value, so lives - 1 is 3 − 1 = 2, and then 2 is stored back into lives.

Reading the right side first is the key to every update pattern. The name on the left is the destination, and the name on the right is the old value, even though they are spelled the same.

Adding then doubling

Two updates in sequence, each using the variable's current value.

followers = 100
followers = followers + 25
followers = followers * 2
print(followers)

Output

250

Reading the pieces

  • Both lines put followers on both sides of =, once as the destination and once as the current value.
  • The math checks out as 100 + 25 = 125, then 125 × 2 = 250.
  • Order matters here, and it is worth pausing on. Doubling before adding would give 100 × 2 + 25 = 225, a different answer from the same two operations. Sequences of updates are not interchangeable.

The shorthand professionals actually type

Updating a variable using its current value is so common that Python has compact operators for it. score += 10 means exactly score = score + 10: take the current value, add 10, store the result back. The same shorthand exists for the other operators as -=, *=, and /=.

score = 0
score += 10
score += 5
score -= 3
print(score)

Nothing new is happening. It is the same right-side-first update from the top of this lesson with less typing, and the two forms are interchangeable.

You will see += constantly in real code, so learn to read it as "increase by". Reading it as a separate concept rather than as shorthand is what makes it confusing.

The shorthand in sequence

The same score, now written with compact operators: it starts at 0, gains 10, gains 5, then loses 3.

score = 0
score += 10
score += 5
score -= 3
print(score)

Output

12

Tracking the box gives 0, then 10, then 15, then 12. That is the identical sequence from earlier in the lesson with three characters saved per line.

The reason to prefer this form is that the variable's name appears once. score = score + 10 gives you two chances to type the wrong name, and in a program with score and high_score that is a bug waiting to happen.

Shorthand with multiplication

steps starts at 4000, gains 2500, then doubles.

steps = 4000
steps += 2500
steps *= 2
print(steps)

Output

13000

Reading the pieces

  • steps += 2500 then steps *= 2, each rewriting the variable from its own current value.
  • The math checks out as 4000 + 2500 = 6500, doubled to 13000.
  • *= follows exactly the same rule as +=, which is the useful part. Learn the pattern once and every operator with an = after it reads the same way: apply this operation to the current value and store the result back.