7. Augmented assignment

An augmented assignment combines an arithmetic operator with =:

StatementEquivalent to
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
x //= yx = x // y
x %= yx = x % y
x **= yx = x ** y

The variable must already exist before the augmented assignment: score += 5 reads the old score, adds 5, and stores the result back in score.

An augmented assignment is a statement, not an expression, so it does not produce a value you can print:

The division forms follow the operator: x /= y always produces a float, because it uses /.

Coming from JavaScript? There is no ++ or --; use x += 1 and x -= 1. Unlike an expression, an augmented assignment cannot be printed or used inside another expression.

#01

Add and assign

Store 10 in a variable named score. Add 5 to it with +=, then print score.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Subtract and assign

Store 3 in a variable named lives. Subtract 1 from it with -=, then print lives.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Multiply and assign

Store 4 in a variable named coins. Multiply it by 3 with *=, then print coins.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Divide and assign

Store 10 in a variable named total. Divide it by 2 with /=, then print total. /= uses division, so the result is a float.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#05

Floor divide and assign

Store 17 in a variable named steps. Floor-divide it by 5 with //=, then print steps.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#06

Power and assign

Store 3 in a variable named power. Raise it to the power of 2 with **=, then print power.

Print exactly:

Create a free account to save your progress and unlock chapter 2.