a= ['1','2','3'] b= ['1','2','3'] a=b print(id(a)) print(id(b)) print(a is b)
Here is what the above code is Doing:
1. We create a list a and assign it to the list [‘1′,’2′,’3’]
2. We create a list b and assign it to the list [‘1′,’2′,’3’]
3. We assign a to b
4. We print the id of a
5. We print the id of b
6. We print whether a is b
Here’s what the output is:
140107908749416
140107908749416
True
So, what’s going on here?
When we create a list a and assign it to the list [‘1′,’2′,’3’], we are creating a list object in memory and assigning it to a.
When we create a list b and assign it to the list [‘1′,’2′,’3’], we are creating a list object in memory and assigning it to b.
When we assign a to b, we are not creating a new list object in memory. We are simply assigning the variable b to the same object that a is pointing to.
When we print the id of a, we are printing the id of the list object that a is pointing to.
When we print the id of b, we are printing the id of the list object that b is pointing to.
When we print whether a is b, we are printing whether a and b are pointing to the same object.
So, what’s the takeaway here?
When we use the assignment operator (=) to assign a variable to an object, we are not creating a new object. We are simply assigning the variable to the object.
If we assign a variable to another variable, we are not creating a new object. We are simply assigning the variable to the object that the other variable is pointing to.
If we assign a variable to another variable and then change the value of the first variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the second variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the first variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the second variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the first variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the second variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the first variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the second variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the first variable, we are not creating a new object. We are simply changing the value of the object that both variables are pointing to.
If we assign a variable to another variable and then change the value of the second variable, we are not creating