Python tutorials > Core Python Fundamentals > Operators and Expressions > What are augmented assignment operators?
What are augmented assignment operators?
Augmented assignment operators in Python provide a shorthand way to perform an arithmetic or bitwise operation and assign the result back to the original variable in a single statement. They combine an operator (like +, -, , /, etc.) with the assignment operator (=).
Introduction to Augmented Assignment
Augmented assignment operators streamline code by reducing redundancy. Instead of writing x = x + 5
, you can use the equivalent x += 5
. This makes the code more concise and readable.
Basic Examples
This code demonstrates the fundamental usage of augmented assignment operators with various arithmetic operations such as addition, subtraction, multiplication, division, floor division, modulo, and exponentiation.
x = 10
x += 5 # Equivalent to x = x + 5
print(x) # Output: 15
y = 20
y -= 3 # Equivalent to y = y - 3
print(y) # Output: 17
z = 5
z *= 2 # Equivalent to z = z * 2
print(z) # Output: 10
a = 25
a /= 5 # Equivalent to a = a / 5
print(a) # Output: 5.0
b = 7
b //= 2 # Equivalent to b = b // 2 (floor division)
print(b) # Output: 3
c = 5
c %= 2 # Equivalent to c = c % 2 (modulo)
print(c) # Output: 1
d = 2
d **= 3 # Equivalent to d = d ** 3 (exponentiation)
print(d) # Output: 8
Concepts Behind the Snippet
Augmented assignment combines two operations: the arithmetic or bitwise operation and the assignment. The interpreter evaluates the expression on the right-hand side and then assigns the result back to the variable on the left-hand side. This operation is performed in-place (when possible for mutable objects) which can improve performance.
Real-Life Use Case Section
Imagine managing an inventory. Augmented assignment operators can efficiently update the quantities of different items. For example, increasing or decreasing the stock levels of fruits in a dictionary.
inventory = {
"apple": 50,
"banana": 30,
"orange": 25
}
# Increase the quantity of apples by 20
inventory["apple"] += 20
print(inventory)
# Decrease the quantity of oranges by 5
inventory["orange"] -= 5
print(inventory)
Best Practices
Interview Tip
Be prepared to explain the difference between x = x + y
and x += y
, particularly regarding mutable objects. Know that x += y
might modify x
in-place, while x = x + y
always creates a new object and reassigns it to x
. For immutable objects like integers and strings, they behave similarly, creating a new object in both cases.
When to Use Them
Use augmented assignment operators when you need to modify a variable's value based on its current value. They are especially useful within loops or functions where a variable is repeatedly updated.
Memory Footprint
For mutable objects (like lists), augmented assignment (e.g., list += [item]
or list.append(item)
) can modify the list in-place, avoiding the creation of a new list and potentially improving memory efficiency, especially with large lists. However, for immutable types, they always create a new object.
Alternatives
The main alternative is the standard assignment operator (=
) combined with the arithmetic or bitwise operator (e.g., x = x + 5
). While functionally equivalent, augmented assignment offers brevity.
Pros
Cons
FAQ
-
Are augmented assignment operators more efficient than regular assignment?
For mutable objects, augmented assignment can be more efficient because it modifies the object in-place, avoiding the creation of a new object. For immutable objects, there's typically no significant performance difference, but the augmented assignment is often more readable.
-
Can I use augmented assignment with strings?
Yes, you can use the
+=
operator for string concatenation. For example:my_string += " world"
-
Do augmented assignments create a new object?
It depends on the object's mutability. For immutable objects like numbers, strings, and tuples, augmented assignment creates a new object. For mutable objects like lists and dictionaries, it modifies the object in place (if possible), potentially avoiding the creation of a new object.