Distinction amongst “a+=b” and “a=a+b” in python for data science.
The two operations, “a+=b” and “a=a + b” do not have the same behavior in the case of python. Let us look at this through an example:
- In the first example, list1 += [1, 2, 3, 4] makes changes in the list. It creates an extension such that both list1 and list2 refer to the old list.
- Now coming to the second example, list1 = list1 + [1, 2, 3, 4] makes a new list. list1 takes reference from the new list, and list2 takes reference from the old list.
Distinction amongst == and is operator:
The operator ‘==’ is used for comparison of values of the operands and check their equality of value. On the other hand, ‘is’ operator checks that the reference of both the operands is to the same object or not.
- Because both the lists 1 and 2 are empty so, the first if condition returns TRUE.
- Even in second condition the lists are empty but at different memory locations, so it returns FALSE.
- Now coming to the third if condition. As both the list1 and list3 are referring to the same object so, the output is TRUE.