For the last 10 years programming has been a relevant topic to people, new technologies, new languages, new machines are changing the world and the base of the new age is the code. The last one always tries to be more close to real language in this case English (Natural language), for this reason, Python could be a good solution to do that and now, we are going to understand how python works with its values. Guido Van Rossum said that he did a language where all the objects were first class and for this, all objects have equal status. (Everything is treated the same way) but, what does it mean?
First, we are going to know how to know the type of a variable a the identify:
ID is an identifier to each store value in python similar or equal to C memory address if you want to know the identifier of something you have to type:
id(object to identify)
and to know the type or a variable you can use the type function:
type(object or variable)
Now, we are going to understand the Guido Van Rossum words with Mutable objects:
It refers to an object that may change state over time, in other words, all mutable objects can change the value during they exist.
for example list and dictionaries are mutable objects like we can see in the next picture:
We can see that the identifier doesn’t change when we add a new element in our list for this reason we can modify the value with the same object.
Dictionaries have the same behavior:
We add a new element in our dictionary but ID doesn’t change and for this reason, we can know that we are in the same object.
After knowing Mutable Objects we are going to understand Immutable Objects, They are the objects that can’t change the value over time. For example integers and tuples:
Like tuples are unchangeable we tried to assign the same value and we can see that the identifier is different
It's very important to know how is the behavior of variables, in this case, objects to know how to manipulate and can do things like concatenate, add new values, and always think in the best way to save memory.
For example, if you want a variable that always updates the best option should be a mutable object.
Another important topic is how to use or how is the behavior in functions, for example for immutables objects:
How you can see if you try to change the value of an immutable object in a function you get the same value because In the first line you are saying that “strt” gets the same value of “strtorigin” but in the third line, in next line “strt” gets new value and It creates a new object is equal if you try to change value when you assign a new variable with the value of the first and try to change the value in the second variable and It only creates a new object.
For Mutable objects:
In this case, the value changes because your input arguments are working with the same object and for this reason, you can change the attributes and methods of this one is equal to:
9. Lists - How to Think Like a Computer Scientist: Learning with Python 2nd Edition documentation
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are…