Python Data Handling Part 1
Python Data Handling Part 1
Hopefully, all of you go through the previous uploaded materials. In this material we shall be talking about various
types of data store in Python.
Introduction
Most of the computer programming language support data type, variables, operator and expression like
fundamentals. Python also support these.
Data Types
Data Type specifies which type of value a variable can store.
Example:-
(i) integers
integers (signed) – whole numbers like 5917, 5, +63, -12 (can be of any length; it is only
limited by the memory available)
Boolean – represent values either True or False
Example:
Output:
(Here, a, b and c are integer variables as they stored integer type values.)
Example:-
Output:-
Output:-
(Here, a, b, c and d are float type variables as they stored floating point values)
(iii) complex numbers – is in the form of A + Bj or A + BJ
Example: a = 1.5 + 2j here 1.5 is the real part and 2 is the imaginary part
You can retrieve the two components using real and imag attribute as following:
Example:-
Output:-
Strings:-
A Python string is a sequence of characters enclosed with quotes and each character can be individually accessed
using its index.
Example:
a = “Welcome”
The internal structure of the above string:-
Example:-
Output:-
Lists:-
A List in Python represents a list of comma – separated values of any data type between square brackets.
Example:-
a = [1, 2, 1.5, 12, 3.4, ‘ram’]
Tuples:-
Tuples are represented a list of comma - separated values of any data type within parenthesis.
Example:-
a = (1, 2, 15, 3.5, ‘s’)
Dictionary:-
The dictionary is an unordered set of comma-separated key:value pairs, within { }.
Example:-
a = {‘Name’:’Ram’, ‘Class’:’XI’, ‘Roll’:12, ‘Marks:45.2}
Output:
Immutable types:- Immutable types are those that can never change their value in place. (in simple words non-
modifiable types).
In Python – integers, floating point numbers, Booleans, strings and tuples are immutable type objects.
Now, the conclusion is when the value of x and y has changed the memory addresses is also changed. So, it indicates
the value of x and y is not modifiable in place.
Another example:-
p=5
q=5
r=5
here p, q and r three integer variables. These three variables having same value i.e. reference the same value objects.
So, the conclusion is three integer variables refer the same value objects.
Figuratively:
p=7
q=8
r=9
The above three variables are now point to different integer objects that’s why their memory addresses are also be
changed.
The original memory addresses of p, q and r that were having value 5 will be the same with the same value i.e. 5 but
p, q and r will no longer reference it, so the memory addresses are also be changed.
So, integer variables are immutable type objects as they can never change their values in place.
Similarly, floating point numbers, Booleans, strings, tuples are also immutable type objects.
Mutable Types:-
The mutable types are those whose values can be changed in place. Only three types are mutable in Python. These
are: lists, dictionaries and sets.
Example:
Output:
On the above code, x is a list type variable, which holds three values 10, 20 and 30 in 0th, 1st, and 2nd position of x.
Then we change the value of 1st position as x [1] = 50,
and after then the value x become 10, 50 and 30 in 0th, 1st and 2nd position.
So, the values in a list can be changed. For that, list is mutable type object.