Python assignments

This article summarizес the following video:


Assignment statement:
When an assignment is made an object is created.
Examples of assignment statements:

my_number = 1
my_string = "abc"


Parts of the assignment statement:
In the assignment statement there are labels and objects:

1. Labels – also called “identifier”, “name”, “variable”.

Labels:
 my_number, my_string 

2. Objects – The objects are created when the program is executed.

"2" and "abc" are NOT the objects.
They are the values of the objects.

Objects have ID, Type, Value:

                                                                      Objects

                                                                 ID:     12345
                                                                 type:   Integer
                                                                 value: 2

                                                                 ID:     56789
                                                                 type:  String
                                                                 value: ab


Binding of label to object:
During the assignment a label (“identifier”, “name”, “variable”) is BOUND to the object.

my_number ---> [  id: 12345  |  type: Integer  |  value: 2  ]
      my_string------> [  id: 56789  |  type: String   |   value: "abc"  ]


New variables:
When an object with a certain value already exists and a new label is assigned the same value a new object is NOT created.
The new label is bound to the existing object.

my_number---> [ id: 12345 | type: Integer | value: 2  ] <---- my_new_number

When a different value is assigned to one of the variables a new object is created.
It holds that new value and has a different ID:

my_number --------->  [ id: 12345   | type: Integer   |  value: 2  ]  <-- Object A
my_new_number ---> [  id: 17812  |  type: Integer  |  value: 3  ] <--- Object B

Code:

a = 1                      // An object with a value of 1 is created
b =  a                     // b is bound to the same object as a. 
                              // Because Python creates a new object only if the value is different.

id(a)                       // ---> 123, The IDs of both variables are the same.                              
id(b)                       // ---> 123. Both are bound to an object with the same value.
id(a) == id(b)        //  ---> TRUE. Compare by value.
b = 3                     // Assign new value to b.  Meaning a new object is created (new ID).
id(b)                      // --->789

id(a) == id(b)       // ---> FALSE. The variables are bound to two different objects

Mutable objects (set, list, dict) have an internal structure can be changed.
They are bound to the same object OR to a different object depending on the way we do the assignment:

A. Bound to DIFFERENT objects WHEN ASSIGNED LITERALLY:

a = [1, 2]
b = [1, 2]
a is b       // False (2 objects are created during assignment)

BUT!

B. Bound to the SAME object WHEN ASSIGNED TO EACH OTHER:

x = [7, 8]
y = x

y now is bound to the same object as x.
When changing y x also changes – there is no new object created.

y.append(9)  //x is now [7, 8, 9]   
                      //     AND!    
                      //  y is [7, 8, 9]

Code:

a = [1, 2]
b = [1, 2]
a is b             // False, 2 different objects


x = [7, 8]
y = x
y.append(9)
x is y            // True, both bound to the same object
                    // And have the same values