Assignment operators in Python store values in variables and update those values with a single concise expression. The most basic assignment operator is the single equals sign, which you have been using since the variables section to bind names to values. When you assign a value to a variable, Python creates the name and points it at that value. This is the foundation that every compound assignment operator builds on, and understanding the distinction between assignment, which stores a value, and equality comparison, which tests a value, is essential before moving into the comparison operators covered in the next article.
Python also provides a family of compound assignment operators that combine an arithmetic operation with assignment in one step. Instead of writing a full expression that reads the variable, performs the math, and stores the result back, you use a shorthand that does all three in one operator. Python supports add-and-assign, subtract-and-assign, multiply-and-assign, and matching forms for every arithmetic operator covered in the previous article on arithmetic operators in Python. These compound operators build directly on the variable concepts from Python variables explained, because every compound assignment reads the current value of a variable before updating it.
How assignment creates and updates variables
The single equals sign binds a name on its left side to the value produced by the expression on its right side. Python evaluates the right side first, computes the result, and then attaches that result to the variable name. This means you can use the same variable on both sides of an assignment to update its value, because the current value is read before the new value is written.
counter = 0 # create a variable
counter = counter + 1 # read counter, add 1, store back
print(counter) # 1Assignment is not equality. A common beginner mistake is using a single equals inside a condition where a double equals was intended. Python catches this in most contexts by raising a SyntaxError because assignment inside a condition is usually not what you meant. The walrus operator, introduced in Python 3.8, does allow assignment inside expressions using colon-equals, but that is an advanced feature covered later and is not a replacement for the basic assignment operator.
Assignment also handles multiple targets in a single statement. Python can unpack values from a tuple, list, or any iterable into multiple variables on the left side, provided the number of targets matches the number of values. This feature was introduced in the earlier article on assigning multiple variables at once and is a common pattern for swapping values without a temporary variable.
x, y = 10, 20
x, y = y, x # swap: x becomes 20, y becomes 10
print(x, y) # 20 10Compound assignment operators
Every arithmetic operator has a matching compound assignment form. The pattern is the same for all of them: the operator performs the arithmetic on the current value of the variable and the right operand, then stores the result back in the same variable. These operators exist primarily to reduce repetition and make the intent clearer. When a reader sees a compound add-and-assign, they immediately know that a value is being added to an existing accumulator.
The compound assignment operators work across the same types as their arithmetic counterparts. Add-and-assign concatenates strings when the variable holds a string, and it extends lists when the variable holds a list. Multiply-and-assign repeats a string or list when the right operand is an integer. This consistency means you do not need to learn a separate syntax for updating different types.
The key distinction to understand is how compound assignment interacts with mutable and immutable types. Numbers, strings, and tuples are immutable, so compound assignment on a string does not modify the original string object. It computes the concatenated result and reassigns the variable to point at the new string. Lists and dictionaries are mutable, so compound assignment on a list modifies the list object in place. If two variables reference the same list, an in-place update on one variable will be visible through the other variable. This is the same reference behavior explained in the article on Python variables and object references, and it is one of the most common sources of subtle bugs for beginners.
When you write a full addition followed by assignment with a list, Python creates a new list and assigns it to the variable. The old list is unaffected, and any other variable that pointed to the old list still sees the old contents. When you write a compound add-and-assign with a list, Python calls the list's internal update method, which modifies the list in place. The variable still points to the same list object, but its contents have changed. This difference matters when the list was passed as an argument to a function or shared across multiple parts of a program.
The compound assignment operators for bitwise operations follow the same pattern as the arithmetic ones. They are covered later alongside bitwise operators and are less common in everyday beginner code, but they exist for completeness across the entire operator family. For now, focus on the arithmetic compound operators because they appear in nearly every Python program you will read or write.
Rune AI
Key Insights
- The equals sign assigns a value to a variable; it does not test equality.
- Compound operators combine an arithmetic operation with assignment in one step.
- Python provides compound versions for every arithmetic operator.
- Compound assignment on immutable objects replaces the reference with a new object.
- Augmented assignment on mutable objects like lists modifies the object directly, which affects every variable that references it.
Frequently Asked Questions
What does `x += 1` mean in Python?
Does `+=` work with strings?
What is the difference between `=` and `==`?
Conclusion
Assignment operators store values and update them efficiently. The basic equals operator creates variables, and the compound operators make updates shorter and clearer. Understanding how assignment works with mutable and immutable types prevents subtle bugs when objects share references.
More in this topic
Nested Lists in Python
Learn how to create, access, and modify nested lists in Python. Build grids and matrices, and understand how shallow copies affect multi-dimensional data.
List Comprehensions in Python
Master Python list comprehensions to create, filter, and transform lists in a single line. Learn the syntax, common patterns, and when to use a regular loop instead.
Copy Python Lists
Learn how to copy Python lists using slicing, the copy method, the list constructor, and the copy module. Understand shallow vs deep copies.