Python | a += b is not always a = a + b
Last Updated :
29 Nov, 2023
In Python, a += b doesn't always behave the same way as a = a + b, the same operands may give different results under different conditions. But to understand why they show different behaviors you have to deep dive into the working of variables. before that, we will try to understand the difference between Python Variable and List Reference in Python.
Difference Between Variable and List Reference
Here, we will see the difference between variable and list reference.
Creating New Variable
In this example, we have created a new variable and will see about its reference. Here, value 10 gets stored in memory and its reference gets assigned to a.
Python3
a = 10
print(" id of a : ", id(10) ," Value : ", a )
Output
id of a : 11094592 Value : 10
a
or
Modifying The Variable
In this example, we are modifying the variable and then comparing the reference after using + and += operators. Whenever we create or modify int, float, char, or string they create new objects and assign their newly created reference to their respective variables.
Python3
a = 10 # Assigning value to variable creates new object
print(" id of a : ", id(a) ," Value : ", a )
a = a + 10 # Modifying value of variable creates new object
print(" id of a : ", id(a) ," Value : ", a )
a += 10 # Modifying value of variable creates new object
print(" id of a : ", id(a) ," Value : ", a )
Output
id of a : 11094592 Value : 10
id of a : 11094912 Value : 20
id of a : 11095232 Value : 30
But the same behavior is not seen in the list. In the below example, we can see that the reference remain same when we use + and += operator in the list.
Python3
a = [0, 1] # stores this array in memory and assign its reference to a
print("id of a: ",id(a) , "Value : ", a )
a = a + [2, 3] # this will also behave same store data in memory and assign ref. to variable
print("id of a: ",id(a) , "Value : ", a )
a += [4, 5]
print("id of a: ",id(a) , "Value : ", a )
#But now this will now create new ref. instead this will modify the current object so
# all the other variable pointing to a will also gets changes
Output
id of a: 140266311673864 Value : [0, 1]
id of a: 140266311673608 Value : [0, 1, 2, 3]
id of a: 140266311673608 Value : [0, 1, 2, 3, 4, 5]
Python | a += b is not always a = a + b
In Python, a+=b is not always a=a+b. We will see this aspect with the help of som examples.
Example: In this example, we can see that list2 which is pointing to list1 gets changes without creating a new object. We can see that the content of list1 and list2 are same. It is because we are using a+=b.
Python3
list1 = [5, 4, 3, 2, 1]
list2 = list1
list1 += [1, 2, 3, 4] # modifying value in current reference
# as on line 4 it modify the value without creating new object
# variable list2 which is pointing to list1 gets changes
print(list1)
print(list2)
Output
[5, 4, 3, 2, 1, 1, 2, 3, 4]
[5, 4, 3, 2, 1, 1, 2, 3, 4]

Example: In this example, we can see that the contents of list1 are same as above but the content of list 2 are different. It is because we are using a=a+b in this example.
Python3
list1 = [5, 4, 3, 2, 1]
list2 = list1
list1 = list1 + [1, 2, 3, 4]
# Contents of list1 are same as above
# program, but contents of list2 are
# different.
print(list1)
print(list2)
Output
[5, 4, 3, 2, 1, 1, 2, 3, 4]
[5, 4, 3, 2, 1]

Similar Reads
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on th
3 min read
Global and Local Variables in Python
Python Global variables are those which are not defined inside any function and have a global scope whereas Python local variables are those which are defined inside a function and their scope is limited to that function only. In other words, we can say that local variables are accessible only insid
7 min read
Type Conversion in Python
Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions. There are two types of Type Conversion in Python: Python Implicit Type C
5 min read
Private Variables in Python
Prerequisite: Underscore in PythonIn Python, there is no existence of âPrivateâ instance variables that cannot be accessed except inside an object. However, a convention is being followed by most Python code and coders i.e., a name prefixed with an underscore, For e.g. _geek should be treated as a n
3 min read
__name__ (A Special variable) in Python
Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file
2 min read
Taking input in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ()
3 min read
Taking multiple inputs from user in Python
While taking a single input from a user is straightforward using the input() function, many real world scenarios require the user to provide multiple pieces of data at once. This article will explore various ways to take multiple inputs from the user in Python.Using input() and split()One of the sim
5 min read
Python - Print Output using print() function
Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations.Python# print() function example print("GeeksforGeeks") a = [1, 2, 'gfg'] print(a) print() Function Syntax
4 min read
Python end parameter in print()
In Python, the print() function, commonly used for displaying output, by default ends each statement with a newline character (\n), but this behavior can be customized using the end parameter, which allows you to specify a different string (such as a space, comma, or hyphen) to be printed at the end
3 min read
Python - Output Formatting
In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out
5 min read