Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Augmented Assignment

Learn how +=, -=, *=, and the other augmented-assignment operators combine an operation with assignment, including their target-evaluation and in-place behavior.

Levels: Beginner ~18 min read

Basic

An augmented assignment combines a binary operation with assignment in one statement. Instead of writing a longer form such as x = x + 1, you can write x += 1. Python supports augmented forms for arithmetic, matrix multiplication, shifts, and bitwise operations.

The basic pattern

The left side is the target, and the operator combines an operation with assignment. The common forms are +=, -=, *=, @=, /=, //=, %=, **=, >>=, <<=, &=, ^=, and |=.

Interactive Runnable example
Edit

Ready to run.

The result of each operation is assigned back to the original target. The syntax is still a statement: unlike an assignment expression (:=), augmented assignment does not produce a value that can be used as part of a larger expression.

It is similar to ordinary assignment, but not identical

For a simple name, x += y is often described as similar to x = x + y. The difference matters because augmented assignment evaluates its target only once and, when the type supports it, can perform the operation in place.

Interactive Runnable example
Edit

Ready to run.

The list was changed in place, so both names still refer to the same list. With ordinary values that are immutable, an augmented assignment can instead create a new object and rebind the target.

Mutable and immutable values behave differently

For a mutable built-in type such as list, an augmented operation can update the existing object. For an immutable type such as str or tuple, the existing object cannot be changed, so the result is a new object that is assigned back to the target.

Interactive Runnable example
Edit

Ready to run.

Do not infer mutation from the operator itself. The object's type determines whether the operation can modify the existing object or must produce another object.

The target is evaluated only once

Unlike a written-out x = x + y form, augmented assignment evaluates the target only once. This becomes visible with a subscription or attribute access whose lookup has an observable effect.

Interactive Runnable example
Edit

Ready to run.

Here index() runs once. Python first evaluates the target, gets its current value, evaluates the right-hand expression, performs the operation, and assigns the result back through that target.

Subscriptions and attributes can be targets

An augmented assignment target can be a name, an attribute reference, or a subscription. Unpacking is not allowed on the left side of an augmented assignment.

Interactive Runnable example
Edit

Ready to run.

The target object decides how assignment is completed. For a subscription, the container handles the final item assignment; for an attribute, the object or its class machinery handles the final attribute assignment.

In-place behavior is a type feature

Python gives a type an opportunity to implement an in-place operation. When that operation is supported, augmented assignment can preserve object identity. If the operation cannot be done in place, Python can fall back to producing a new value before assigning it back to the target.

Interactive Runnable example
Edit

Ready to run.

The list keeps its identity because it can be updated in place. The tuple cannot be changed, so total is rebound to a new tuple. Python performs the appropriate operation for the target type; the details of special-method customization belong to the Special Methods lesson.

The left side is processed before the right side

For an augmented assignment such as a[i] += f(), Python evaluates the left-hand target first, then evaluates the right-hand expression, performs the operation, and writes the result back to the target. This can matter when either side has side effects.

Interactive Runnable example
Edit

Ready to run.

The recorded order is target, then right. This is a statement-level rule for augmented assignment; the general precedence and expression-evaluation lesson continues to own expression grouping and broader evaluation-order reasoning.

A practical boundary

Use augmented assignment when the operation naturally updates the same target. It is especially useful for counters, accumulating values, updating mutable collections, and changing attributes or items. Do not use it just because the shorter syntax exists; a longer ordinary assignment can be clearer when it expresses a different transformation.

Interactive Runnable example
Edit

Ready to run.

When you are ready, take the Python Augmented Assignment quiz.

Was this page helpful?

Mark this learning resource complete to track your learning progress.