Augmented Assignment

Concepts covered: Boolean Operators

A running total repeats one shape of statement: read the variable, change it, store it back under the same name. total = total + 5 says total twice. The augmented form total += 5 says it once and does the same work: it evaluates the right side, applies the operator to the current value, and binds the result to the name.

ShorthandSpelled out
`total += 5``total = total + 5`
`total -= 5``total = total - 5`
`total *= 2``total = total * 2`
`total /= 2``total = total / 2`
`total //= 2``total = total // 2`
`total %= 2``total = total % 2`
`total **= 2``total = total ** 2`
balance = 100
balance += 25
balance -= 40
balance *= 2
print(balance)
balance /= 4
print(balance)
count = 7
count //= 2
count %= 2
count **= 3
print(count)

Output:

170
42.5
1

Follow balance line by line: 125, then 85, then 170. The /= step gives 42.5 and changes the type along the way, because / always produces a float even when the division is exact. count goes 3, then 1, then 1. Each shorthand follows the rule of its own operator, including the type of the result, so //= keeps an int and /= never does.

The right side can be any expression, and it's evaluated fully before the operator is applied. total += bonus * 2 doubles bonus first and then adds. Strings accept += too: label += "!" builds a new string with the exclamation mark on the end and binds label to it, since a string, once made, never changes.

A number is replaced, a list is changed

This section introduces one new kind of value, far enough to show the difference. A list is a sequence of values in square brackets, ["apple", "pear"], and + between 2 lists builds a longer list. Lists get their own lessons later; here they stand in for every value that can be changed after it is made. Play the animation and watch what each name points at after every line.

total = 10
alias_total = total
total += 5
print(total, alias_total)

cart = ["apple", "pear"]
alias_cart = cart
cart += ["fig"]
print(cart, alias_cart)

cart = cart + ["plum"]
print(cart)
print(alias_cart)
15 10
['apple', 'pear', 'fig'] ['apple', 'pear', 'fig']
['apple', 'pear', 'fig', 'plum']
['apple', 'pear', 'fig']

alias_total = total makes a second name for the same 10. When total += 5 runs, Python has no way to change the number 10 itself: an int is immutable, so it creates a new 15 and points total at it, while alias_total still points at 10. The list behaves differently. cart += ["fig"] extends the existing list object in place, and alias_cart names that same object, so printing either shows 3 items. The last step, cart = cart + ["plum"], builds a brand new list, and only cart moves to it.

Immutable: int, float, str, bool
  • += computes a new value
  • The name is rebound to the result
  • Other names for the old value keep it
  • x += 1 and x = x + 1 behave identically
Mutable: list
  • += changes the object in place
  • The name stays on the same object
  • Every other name for the object sees the change
  • x += [1] and x = x + [1] differ

The typo that assigns instead of adding

retries = 3
retries =+ 1
print(retries)

Output:

1

=+ 1 parses as = +1, a plain assignment of positive 1. Nothing is added, no error is raised, and retries becomes 1. The 2 characters in the wrong order are a frequent cause of counters that reset instead of counting, and because the program runs cleanly the mistake only shows in the numbers it reports.

What does this program print?

shared = [1, 2]
other = shared
shared = shared + [3]
shared += [4]
print(other)

A program with a =+ typo runs to the end and prints a wrong number, so the evidence sits in the output: a total equal to the last amount added means that line replaced the value instead of adding to it.

About this lesson

This section is part of the Python Expressions: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.