0% found this document useful (0 votes)
1 views

Python Data Handling Part 1

This document provides an overview of data handling in Python, focusing on various data types such as integers, floating point numbers, complex numbers, strings, lists, tuples, and dictionaries. It explains the concepts of mutable and immutable types, highlighting that integers, floats, Booleans, strings, and tuples are immutable, while lists and dictionaries are mutable. The document also includes examples and explanations of how to determine variable types and memory addresses using the type() and id() functions.

Uploaded by

ritamdawn01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python Data Handling Part 1

This document provides an overview of data handling in Python, focusing on various data types such as integers, floating point numbers, complex numbers, strings, lists, tuples, and dictionaries. It explains the concepts of mutable and immutable types, highlighting that integers, floats, Booleans, strings, and tuples are immutable, while lists and dictionaries are mutable. The document also includes examples and explanations of how to determine variable types and memory addresses using the type() and id() functions.

Uploaded by

ritamdawn01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Class 11

Data Handling – in Python Part - 1

For Computer Science and Informatics Practices students’

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.

type( ) function is used to determine a variable's type in Python.

Example:-

The following figure summarizes the core data types in Python:-


Numbers:- Number data types are used to store numeric values.
The numbers in Python have following core data types:

(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:-

(Here, a is a Boolean type variable as it stored Boolean value True)


(ii) floating point numbers – numbers with decimal points like 3.142, 56.0 etc.
 Fractional form – normal decimal notation like 3547.75, 0.0005, 147.945
 Exponent form – like 3.575E03, 1.479E02 etc.
Example:-

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}

(Lists, Tuples and Dictionaries shall be covered in details in a later chapter)

None:- it indicates nothing.


Example:

Output:

MUTABLE AND IMMUTABLE TYPES:-


In Python data objects can be broadly categorized into – mutable and immutable types.

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.

Look at the following code:-


Output:-

Ok…..Now you tell…What is the conclusion?


in the first line value of x is 5 and value of y is 10
later we change the value of x as 12 and y as 25.
So, you can say that values of integer variables x and y could be changed effortlessly and also you think that integer
types can change values.

But hold: It is not the actual case. Let’s see how:

We can check that with the help of id( ) function.


The id( ) function returns the memory addresses which a variable is referencing.

So, on the above example:


At first, x = 5 and y = 10

Now, if we write the following code:

Now, what is our expectation?


The outputs will be same in both the cases as in both cases it refers the addresses of x and y.
But the actual output is following:

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.

print(id(5)) it gives the memory address of 5  1597072624


print(id(p)) it gives the memory reference of p  1597072624 (same as the address of 5)
print(id(q)) it gives the memory reference of q  1597072624 (same as the address of 5)
print(id(r)) it gives the memory reference of r  1597072624 (same as the address of 5)

So, the conclusion is three integer variables refer the same value objects.

Figuratively:

Now, if we write following (try to change the values of three variables):

p=7
q=8
r=9

print(id(p)) it gives the memory reference of p  1597072656 (same as the address of 7)

print(id((q)) it gives the memory reference of q  1597072672 (same as the address of 8)

print(id(r)) it gives the memory reference of r  1597072688 (same as the address of 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.

If any problem then message me in my personal whatsapp no. 8145472773

You might also like