Python | a += b is not always a = a + b
a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we must delve deeply into the operation of variables to comprehend why they exhibit diverse behaviours. These operators are convenient ways to add or subtract values from a variable without having to use an intermediate variable. For example, if we want to add two numbers together and store the result in a new variable, we will write:
firstn = firstn + secondn
# But we may just write: if we wish to use the += operator instead
firstn += secondn
To combine two numbers, use the += operator. The variable to the left of the += symbol will hold the sum as a consequence of the addition. In this example, the variable x is used to store the outcome of adding y to x.
But the addition assignment is always not true in all cases and to understand that why this is not equal we need to get into depth into the topic of working and using of variables.
Creating a New Variable:
m = 10
print(" id of m : ", id(10) ," Value : ", m )
Output:
id of a : 11094592 Value : 10
By Updating and Changing the Variable:
m = 5 # Giving a variable a value creates a new object.
print(" id of m : ", id(m) ," Value : ", m )
m = m + 10 # An object is created when a variable's value is changed.
print(" id of m : ", id(m) ," Value : ", m )
m += 10 # An object is created when a variable's value is changed.
print(" id of m : ", id(m) ," Value : ", m )
Output:
id of m: 11096592 Value : 5
id of m: 11093912 Value: 15
id of m: 11091232 Value: 25
Because each time we create or alter an int, float, char, or string, a new object is formed and its reference is assigned to the appropriate variable. Whereas in the list type this is not able to observe.
m = [0, 1, 2] # m is given the reference to this array after it is stored in memory.
print("id of m: ",id(m) , "Value : ", m )
a = a + [4, 3] Similarly, this will behave by storing data in memory and assigning a variable a reference.
print("id of m: ",id(m) , "Value : ", m )
m += [6, 5]
print("id of m: ",id(m) , "Value : ", m )
# However, this will now provide a new ref. this will change the current object instead, so
# Every other variable that points to a will likewise experience changes.
Output:
id of a: 140266311673864 Value : [0, 1, 2]
id of a: 140266311673608 Value : [0, 1, 2, 4, 3]
id of a: 140266311673608 Value : [0, 1, 2, 4, 3, 6, 5]
Now we understand why a = a + b can occasionally differ from a += b.
l1 = [6, 7, 5, 4, 3, 2, 1]
l2 = list1
l1 += [1, 2, 3, 4] # changing the value of the current reference
print(l1)
print(l2)
# The value of the variable list2, which is pointing to list1, is changed on line 4 #since it was modified without generating a new object.
Output:
6, 7, 5, 4, 3, 2, 1, 1, 2, 3, 4
6, 7, 5, 4, 3, 2, 1, 1, 2, 3, 4
l1 = [6, 5, 4, 3, 2, 1]
l2 = l1
l1 = l1 + [1, 2, 3, 4,7, 8, 9]
# List 1's contents match those of
#the preceding programme,
#however List 2's contents differ.
print(l1)
print(l2)
Output:
6, 5, 4, 3, 2, 1, 1, 2, 3, 4,7, 8, 9
6, 5, 4, 3, 2, 1
The expression list1 += [1, 2, 3, 4, 7, 8, 9] extends the list while keeping "l1" and "l2" as references to the same list. This is known as an in-place modification.
listing 1 = listing 1 + [1, 2, 3, 4, 7, 8, 9] generates a new list, modifies "l1" to refer to it, and leaves "l2" referring to the previous list.