Start learning free

LESSON 25 OF 410 · CALCULATIONS WITH NUMBERS

Augmented Assignment Operators in Python: +=, -=, *=, /=

Lesson Read the full explanation

In programming, it is common to use assignments to modify the contents of a variable.

Remember that everything to the right of the equals sign (=) is evaluated first, so we can say:

myVar = myVar + 5

to add 5 to myVar.

Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

One such operator is the += operator:

myVar = 10
myVar += 5
print(myVar)

This code would print 15 to the console. We call myVar += 5 an augmented assignment.

There is one such operator for every other arithmetic operator we've learned:

For subtraction: -=

myVar = 10
myVar -= 5 # short for myVar = myVar - 5
print(myVar) # Prints 5

For multiplication: *=

myVar = 3
myVar *= 5 # short for myVar = myVar * 5
print(myVar) # Prints 15

For division: /=

myVar = 20
myVar /= 4 # short for myVar = myVar / 4
print(myVar) # Prints 5

For remainder: %=

myVar = 11
myVar %= 3 # short for myVar = myVar % 3
print(myVar) # Prints 2

For exponentiation: **=

myVar = 3
myVar **= 3 # short for myVar = myVar ** 3
print(myVar) # Prints 27

For floor division: //=

myVar = 20
myVar //= 3 # short for myVar = myVar // 3
print(myVar) # Prints 6


Assignment
Follow the Coding Tutorial and let's do some augmented assignments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in Python, explained

    What an augmented assignment operator is

    An augmented assignment operator combines an arithmetic operation with an assignment. Instead of writing the variable name twice, you write it once and put the operator in front of the equals sign:

    number = 10
    number = number + 5   # long form
    number += 5           # augmented form, same effect
    print(number)         # 20
    

    As with a plain assignment, Python first computes the right-hand side, then stores the result back into the variable on the left. So x += y behaves like x = x + y. The variable must already exist, because the old value is needed for the calculation.

    The augmented operators in Python

    Every arithmetic operator has an augmented version:

    OperatorSame asExample (starting from the value shown)Result
    +=x = x + yx = 10; x += 515
    -=x = x - yx = 10; x -= 55
    *=x = x * yx = 3; x *= 515
    /=x = x / yx = 20; x /= 45.0
    //=x = x // yx = 20; x //= 36
    %=x = x % yx = 11; x %= 32
    **=x = x ** yx = 3; x **= 327

    There are also bitwise versions: &=, |=, ^=, <<= and >>=. They work on the binary digits of integers, for example c = 6; c &= 3 leaves c equal to 2.

    Notice the table entry for /=: it gives 5.0, not 5. Ordinary division with / always produces a float in Python, even when both operands are integers and the division is exact. If you want to stay with integers, use //=, which divides and rounds down.

    There is no ++ or --

    Python has no increment operator ++ and no decrement operator --. To add one to a counter, write count += 1. To subtract one, write count -= 1. Writing count++ is a syntax error, and count-- is as well.

    Augmented assignment beyond numbers

    The same operators work on any type that supports the matching operation. For strings, += joins text and *= repeats it:

    s = "ab"
    s += "cd"
    print(s)   # abcd
    s *= 2
    print(s)   # abcdabcd
    

    Mixing types still fails. After q = "a", the statement q += 1 raises TypeError: can only concatenate str (not "int") to str.

    One subtle difference from the long form appears with mutable objects such as lists. For a list, += changes the existing list in place instead of building a new one, so every name that refers to that list sees the change:

    a = [1, 2]
    b = a
    b += [3]
    print(a, b)   # [1, 2, 3] [1, 2, 3]
    
    t = (1, 2)
    u = t
    u += (3,)
    print(t, u)   # (1, 2) (1, 2, 3)
    

    A tuple cannot be changed, so u += (3,) creates a new tuple and rebinds u, leaving t alone.

    The exercise, step by step

    The lesson walks one variable through four augmented assignments and tracks its value after each one.

    1. Create number with the value 18. Python needs no type or keyword. Assigning a value creates the variable, and 18 makes it an int.
    2. Add 7 with +=. number += 7 turns 18 into 25.
    3. Subtract 5 with -=. number -= 5 turns 25 into 20.
    4. Divide by 4 with /=. 20 divided by 4 is 5, but because / always returns a float, number becomes 5.0 and its type changes from int to float.
    5. Multiply by 3 with *=. 5.0 * 3 is 15.0, still a float.
    number = 18
    number += 7
    number -= 5
    number /= 4
    number *= 3
    

    Printing number at the end shows 15.0.

    Common mistakes

    • Using an augmented operator on a variable that does not exist. A fresh z += 1 raises NameError: name 'z' is not defined, since there is no old value to read. Assign a starting value first.
    • Swapping the characters. The operator character comes first and the equals sign second. x =+ 1 is not an augmented assignment: it parses as x = +1 and sets x to 1.
    • Expecting /= to keep an integer. The result is always a float. Use //= when you need whole numbers.
    • Forgetting that //= rounds toward negative infinity. With x = -7, x //= 2 gives -4, not -3.
    • Trying to write x++. Use x += 1.

    Key takeaways

    • x op= y is a short form of x = x op y: the right side is computed first, then stored back into x.
    • Python has +=, -=, *=, /=, //=, %=, **= and the bitwise versions, but no ++ or --.
    • /= always produces a float. //= floors the result.
    • The variable must exist before you use an augmented assignment on it.
    • On a list, += extends the same object in place.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free