Whenever data is stored somewhere it is stored with a specific data type. A data type specifies the type of value stored in a variable, e.g. 11 is an integer type, "abcd" is a string type, etc. Each data type varies in its storage size and also in its usage.
Python has several built-in data types like integer, string, float, list, tuple, etc. In C, C++, and Java it was necessary to declare the data type of the variable but in Python, it's not necessary. It automatically detects the data type of variable, there is no need for any declaration.
Numeric data types stores data as numbers. These values can be integers, float, or boolean. Let's see them one by one:
int class. Example: -30, 5, -6, 55555 ,-66879 and so on.
False. Remember that 'T' and 'F' will always be capital otherwise python will throw an error of invalid data types.
Sequence data types contain a collection of similar or different data types. It is a way to organize multiple values under a single variable name. There are various sequence data types in python like string(collection of characters), list, and tuple.
str class. Example: "hello", 'hello', '''hello'''.
It is advised that a list should be used when data gets frequently updated. Example: list of employee details. Whereas a tuple should be used when we are sure that there would be no need to update or add new data.
Mutable means 'can be changed', immutable means 'cannot be changed'. The data types in python are categorized into two types Mutable and Immutable. All the types falls under these two categories.
All the data types whose values can be changed after creation are called mutable data types. Ex. List, dictionary, sets, and user-defined classes(OOP topic).
The data types whose values cannot be changed after creation are called immutable data types. Ex. Integer, String, float, tuples, etc.
If you try to change the value of an integer or a float variable, you can do that very easily, so, why is it immutable? Let's find out!
Always remember that in Python everything is an object. Python treats each data type as an object and each object has 3 properties: id, type, and value:
Let's take an example of integer data type:
a=10, it is an object for python so its values will be:
Now, in mutable data types, when any change in the value occurs, then id of that object doesn't changes. Whereas, in Immutable objects, after alteration in value, the id of that object changes. It means that a whole new object is created for the new value.
In case of immutable objects, the old object gets lost in the memory and tha changes take place in a new memory space, i.e. it is a completely new object with its own memory space. But mutable objects accept the changes and does not create new memory space.
So, when we talk about integers, float, string or lists, although we can change the values but all these will allocate new memory spaces for the new values rather than overriding. That's why these are called Immutable objects. Hope after this Python Tutorial you learned about how Mutable and Immutable data types in Python work.