Home » Python » Python Data Types

Python Data Types - Mutable and Immutable

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.


Note: The type() function in python returns the data type of a variable. If you need to know the data type of a variable, just pass that variable as a parameter to this function and it will tell you the data type of the value stored in that variable.

Numeric data types in Python

Numeric data types stores data as numbers. These values can be integers, float, or boolean. Let's see them one by one:

  1. Integer data type: Integer data type contain all integer values from negative to positive. These numbers are pure integers without fractions and decimals. It is represented by int class. Example: -30, 5, -6, 55555 ,-66879 and so on.
  2. Float data type: The Float data type contains values with decimals like 0.5, 2.33, 99.7. Float type is represented by float class.
  3. Boolean data type: Boolean data type contains any of the two built-in values True and False. Remember that 'T' and 'F' will always be capital otherwise python will throw an error of invalid data types.
  4. None data type: None data type specifies the null value in python. It means that the variable currently doesn't hold any value. None is also returned by the functions that don't return any value.
Example
# Python Integer
a = 5
print(a, "is of type", type(a))

# Python Float
a = 2.0
print(a, "is of type", type(a))

# Python Boolean
print("\n Boolean:Print a message based on whether the condition is True or False \n")
x = 200
y = 33

if y > x:
print("y is greater than x")
else:
print("y is not greater than x")

Output

5 is of type <class 'int'>
2.0 is of type <class 'float'>

Boolean: Print a message based on whether the condition is True or False.

y is not greater than x

Sequence data Types in Python

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.

  1. String data type: String data type is a collection of characters. It can be only one character to multiple. A String is written inside single(' '), double(" ") or triple(''' ''') quotes. It is represented by str class. Example: "hello", 'hello', '''hello'''.
  2. List: A List data type is a collection of similar or different data types stored in one variable. The data is stored in a sequential way with index numbers to access each data item. These are just like arrays in other programming languages. The benefit of lists over arrays is that it can store different data types in a single list. For example: Under employee list, it can store name as string type and mobile number as integer type.
  3. Tuple: Tuple data type in python is similar to list, it is also a collection of different data types but with a difference. Tuples are immutable whereas lists are mutable. It means, once stored, tuples values cannot be altered.

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.

Example
# Python String
s = "This is a string"
print(s)

# Python List
a = [10, 20, 30]
a[2] = 40
print(a)

# Python Tuple
demo_tuple = (1, 2, 3)
print(demo_tuple)

Output

This is a string
[10, 20, 40]
(1, 2, 3)

Mutable and Immutable data types in Python

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.

Mutable data types in Python

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).

Example
# Python List
print("\nPython List Adress")
a = list(('OnePlus', 'Samsung', 'Apple'))
print(id(a))

print("\nPython List Adress after Changes")
a.append('Nokia')
print(id(a))


# Python Sets
print("\nPython Sets Adress")
c = set(('San Francisco', 'Sydney', 'Sapporo'))
print(id(c))

print("\nPython Sets Adress after Changes")
c.pop()
print(id(c))

# Python Dictionaries
print("\nPython Dictionaries Adress")
d = {
'a': 'Apple',
'b': 'Ball',
'c': 'Cat',
}
print(id(d))

print("\nPython Dictionaries Adress after Changes")
d.update({
'c': 'dog'
})
print(id(d))

Output

Python List Adress
140666589491264

Python List Adress after Changes
140666589491264

Python Sets Adress
140666589440256

Python Sets Adress after Changes
140666589440256

Python Dictionaries Adress
140666590272512

Python Dictionaries Adress after Changes
140666590272512

Immutable data types in Python

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:

  • Id: It is the address of the object in the memory.
  • Type: It is the data type of that object.
  • Value: The value stored in that object.

Let's take an example of integer data type:a=10, it is an object for python so its values will be:

  • Id: 012bx5fg(assumed address)
  • Type: Int
  • Value: 10
Example
# Python String
print("\nString address")
a = 'Hello, Python!'
print(id(a))

print("\nString address after changes")
a = 'Hello, JAVA!'
print(id(a))

# Python Integer
print("\nInteger address")
b = 5
print(id(b))

print("\nInteger address after changes")
b = (25)
print(id(b))

# Python Float
print("\nFloat address")
c = 5.5
print(id(c))

print("\nFloat address after changes")
c = (25.5)
print(id(c))

# Python Tuples
print("\nTupple address")
d = (1, 3, 5)
print(id(d))

print("\nTupple address after changes")
d = (30, )
print(id(d))

Output

String address
140101712738288

String address after changes
140101712738352

Integer address
9780928

Integer address after changes
9781568

Float address
140101713185040

Float address after changes
140101713184912

Tupple address
140101713154624

Tupple address after changes
140101713990848

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.











Follow Us: