Python Data Types

Data types is special functionality of every high level programming language by which we can manipulate the data in different - different ways.

Data can be of many types for example : character, integer, string, inform of real numbers etc.

As we know that, the data to be dealt with are many types , a programming language must provide different ways and functionalities to handle all these data.

Before learning how to work with python data types, let us discuss several data types supported by python programming.

Python Datatypes, Codiens

The following are built-in data types in python programming :-

Numeric data types : int, float, complex
Boolean data type : bool
Character data type : str
Collections as data types : list, tuple, set, dict(dictionary)

In python data types can be two types mainly, which are immutable and mutable data types.

Before diving into python built-in data types , first we have to understand what are mutable and immutable data types?

Immutable data types

The immutable data types are those that can never change their value in place (changing inplace means updating the value in the same memory locations).

We know that every variable in python is an object or reference variable to the value, that means the python variables are not capable of storing values themselves. They point to another memory chunk where data will be stored

Okay, let’s understand this more clearly through an example :-

x = 10

python immutable datatypes storage memory.

As we can see above, each memory location has a memory address and when you write x=10, it will create a reference variable named x.

The Output of the following code for my system will be - id() function returns the address of memory location :

2696959605952
2696959604070

If you change the value of an immutable object data type then they do not change the value in place, rather they create a second memory chunk and store the latest or new value in it and then the reference variable (which is x in our case) starts pointing to the new memory chunk and the python garbage collector frees old memory block. As shown in the below :

how immutable data stored in memory

Thus python immutable variables do not have fixed memory locations because they change every time their values change.

Datatype Typecast Class of Datatype
Integer int() <class 'int'>
Floating Point Value float() <class 'float'>
Complex Number complex() <class 'complex'>
String str() <class 'str'>
Tuple tuple() <class 'tuple'>
Boolean bool() <class 'bool'>

The immutable data uses memory pooling i.e sharable.

Mutable Data types

Mutable data types are those whose values can be changed in place. It means if we define a variable named try_list, it is capable of changing its value in the same location where its previous value was stored.

Datatype Typecast Class of Datatype
List list() <class 'list'>
Set set() <class 'set'>
Dictionary dict() <class 'dict'>