May 2019
Beginner
528 pages
29h 51m
English
Augmented assignments abbreviate assignment expressions in which the same variable name appears on the left and right of the assignment’s =, as total does in:
for number in [1, 2, 3, 4, 5]:total = total + number
Snippet [2] reimplements this using an addition augmented assignment (+=) statement:
In [1]: total = 0In [2]: for number in [1, 2, 3, 4, 5]:...: total += number # add number to total and store in number...:In [3]: totalOut[3]: 15
The += expression in snippet [2] first adds number’s value to the current total, then stores the new value in total. The table below shows sample augmented assignments:

Read now
Unlock full access