Computer Science with Python Class 11
Data Handling in Python
Data Type : It identifies the type of data values a variable can hold , the amount of memory
it takes and the operations that can be performed on that data.
Mutable Data Types
Variables whose values can be changed after they are created and assigned are called
mutable e.g. objects like lists, dictionaries and sets.
Immutable Data Types
Variables whose values cannot be changed after they are created and assigned are
called immutable e.g. objects like integer, float, decimal , boolean , strings , tuples etc.
When an attempt is made to update the value of an immutable variable, the old variable
is destroyed and a new variable is created by the same name in memory.
Difference between mutable and immutable data types
Python handles mutable and immutable objects differently. Immutable are quicker to
access than mutable objects. Also, immutable objects are fundamentally expensive to
"change" as they occupy a lot of memory of the system because doing so involves
creating a copy. Changing mutable objects is cheap.
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
1. Numbers :
e.g.
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
2. Sequence :
A Python sequence is an ordered collection of items, where each item is indexed by an
integer. The three types of sequence data types available in Python are Strings, Lists and
Tuples.
( a ) String : String is a group of characters. These characters may be alphabets, digits or
special characters including spaces. String values are enclosed either in single quotation
marks (e.g., ‘Hello’) or in double quotation marks (e.g.“Hello”). The quotes are not a part
of the string, they are used to mark the beginning and end of the string for the
interpreter. For example str1 = 'Hello Friend' and str2 = "452".
We cannot perform numerical operations on strings, even if the string contains a
numeric value.
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
( b ) List : List is a sequence of items ( same or different data types ) separated by
commas and the items are enclosed in square brackets [ ].
Example
#To create a list the syntax is :
list1 = [5, 3.4, "New Delhi", "20C", 45]
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
( c ) Tuple : Tuple is a sequence of items separated by commas and items are enclosed
in parenthesis ( ). This is unlike list, where values are enclosed in brackets [ ]. Once
created, we cannot change the data items of tuple.
Example
#create a tuple tuple1
tuple1 = (10, 20, "Apple", 3.4, 'a')
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
Difference between a List and a Tuple :
3. None :
It is a special data type with a single value. It is used to signify the absence of value in a
situation. None supports no special operations, and it is neither False nor 0 (zero).
Example :
myVar = None
>>>print(type(myVar))
<class 'NoneType'>
>>>print(myVar)
None
4. Set :
A Set is an unordered collection of items separated by commas and the items are
enclosed in curly brackets { }. A set is similar to list, except that it cannot have duplicate
entries. Once created, elements of a set cannot be changed. However a set itself is
mutable. We can add or remove items from it.
Example
# To create a set
set1 = {10,20,3.14,"New Delhi"}
#duplicate elements are not included in set
set2 = {1,2,1,3}
>>>print(set2)
{1, 2, 3}
5. Mapping :
Mapping is an unordered data type in Python. Currently, there is only one standard
mapping data type in Python called dictionary.
A Dictionary in Python holds data items in key-value pairs. Items in a dictionary are
enclosed in curly brackets { }. Dictionaries permit faster access to data. Every key is
separated from its value using a colon ( : ) sign. The key : value pairs of a dictionary can be
accessed using the key. The keys are usually strings and their values can be any data type.
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
Keys defined in a dictionary cannot be same. In order to access any value in the dictionary,
we have to specify its key in square brackets [ ].
Example
# To create a dictionary
dict1 = {'Fruit' : 'Apple' , 'Climate' : 'Cold' , 'Price(kg)' : 120}
>>>print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
>>> print(dict1['Price(kg)'])
120
Memory Allocation to mutable and immutable data types
1. Mutable Data Types:
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
2. Immutable Data Types:
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
Key attributes of Variables
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
1. type ( ) :
2. value ( ) :
Computer Science Dept. Mount Carmel School AN
Computer Science with Python Class 11
3. id ( ) :
Computer Science Dept. Mount Carmel School AN