Object
In general, anything that can be assigned to a variable in Python is referred to as an object.
Strings, Integers, Floats, Lists etc. are all objects.
Examples
- "A"
- 1.25
- [1,2,3]
Identity of an Object
Whenever an object is created in Python, it will be given a unique identifier (id).
This unique id can be different for each time you run the program.
Every object that you use in a Python Program will be stored in Computer Memory.
The unique id will be related to the location where the object is stored in the Computer Memory.
Finding Id
We can use the
Code
Output
Id of Lists
Output
Modifying Lists
Modifying Lists - 1
When assigned an existing list both the variables
Code
Output
Modifying Lists - 2
When assigned an existing list both the variables
Code
Output
Modifying Lists - 3
The assignment will update the reference to new object.
Code
Output
Modifying Lists - 4
The assignment will update the reference to a new object.
Code
Output
Modifying Lists - 5
Compound assignment will update the existing list instead of creating a new object.
Code
Output
Modifying Lists - 6
Updating mutable objects will also effect the values in the list, as the reference is changed.
Code
Output
Modifying Lists - 7
Updating immutable objects will not effect the values in the list, as the reference will be changed.