Augmented assignment statements in Python

Published: July 12, 2022

Augmented assignment statements in Python

In this article, we would cover Augmented assignment statements in Python. What makes Augmented assignment statements different from assignment statements is the binary operation. The Augmented assignment statements contain both assignment statements as well as binary operation.

Understand it with the help of an example. When we assignment integer value 10 to variable x then, we declare -

x = 10

That is the typical assignment statement. But, for Augmented assignment statements - it performs binary operation on two operands and outcome is stored accordingly. So, let's say we want to achieve the following result using augmented assignment statement -

x = x * 10

For augmented assignment statement, we use a expression operator (*) followed by an assignment operator (=). So, it would be -

x *= 10

Here, instead of writing x = x * 10, we are using x *= 10. And, both the statements would get us same outcome i.e. multiply x with 10 and store the result in x again.

Similar operations can be performed for addition, subtraction, division, exponentiation etc. We cover few of those with relevant examples next. Let's say we have -

x = 100

I. Addition (+=)

x += 10
print(x)

Outcome -

110

II. Subtraction (-=)

x -= 10
print(x)

Outcome -

90

III. Multiplication (*=)

x *=10
print(x)

Outcome -

1000

IV. Division (/=)

x /= 10
print(x)

Outcome -

10.0

V. Floor division (//=)

x //= 10
print(x)

Outcome -

10

VI. Modulus (%=)

x %= 10
print(x)

Outcome -

0

VII. Exponentiation (**=)

x **= 2
print(x)

Outcome -

10000

In conclusion, we have discussed how work with Augmented assignment statements in Python.