Lesson 5
Writing Reusable and Self-explanatory Programs
Chapter 2: Shortcut assignment statements
Augmented assignment operators
Like many programming languages, Python also offers some ‘shortcut’ assignment operators that assign and perform an operation simultaneously. This can save you some typing.
These operators are +=
, -=
, *=
, /=
, //=
, %=
, **=
.
x += 1
is equivalent x = x + 1
.
x /= y
is equivalent x = x / y
.
etc.
These operators are officially called augmented assignment operators.
These operators can be used anywhere. For example, it is used often with counters inside loops.
i = 0
while i < 10:
print(i)
i += 1