Pb-U7.1 Data Handling
Pb-U7.1 Data Handling
DATA HANDLING
Data types
o Data type in Python specifies the type of data we are going to store in any variable, the amount
of memory it will take and type of operation we can perform on a variable.
o Data can be of many types e.g. character, integer, real, string etc.
o Immutable types:
A Immutable data type cannot change its state or contents or value in place.
In python following types are immutable: integers, float, Boolean, strings, tuples.
Example: Code:
Values 10 20 30 40 50 a=10 b=a c=30
print(id(a)) print(id(b))
Address 100 101 110 1110 1111 print(id(c)) print(id(10)
0 0 0
Output:
1000 1000 1100 1000
a b c
Note : In python each value in memory is assigned a memory address. So each time a new
variable is pointing to that value they will be assigned the same address and no new memory
allocation.
o From the previous code it is clear that variable names are stored references to a value-object.
Each time we change the value the variable’s reference memory address changes. So it will
not store new value in same memory location that’s why Integer, float, Booleans, strings and
tuples are immutable.
o Variables (of certain type) are NOT LIKE storage containers i.e. with fixed memory address
where value changes every time. Hence they are immutable
o Mutable types:
A mutable data type can change its state or contents or value in place.
Mutable means in same memory address, new value can be stored as and when it is
required. Python provides following mutable types: Lists, Dictionaries, Sets
Example:
List 10 20 30 40 50 Output:
1010
Address 1010 [10,20,30,100,50]
Code: 1010
List =[10,20,30,40,50]
print(id(List)) See, even if we change the value, its
List[3]=100 reference memory address has remained
Print(List) same..
print(id(List))
Number:
o From the name it is very clear the Number data types are used to store numeric values.
o Numbers in Python can be of following types:
Integers
Integers(signed)
Booleans
Floating point numbers
Complex Numbers
o Integers
Integers allow storing whole numbers only and there are no fraction parts.
Integers or int are positive or negative numbers with no decimal point. Integers in Python 3
are of unlimited size.
Example: 100, 250, -12, +50
Booleans: it allows storing only two values True and False. The internal value of
Boolean value True and False is 1 and 0 resp. We can get Boolean value from 0 and 1
using bool() function.
Floating point numbers are mainly used for storing values like distance, area, temperature etc.
which have a fractional part.
Floating point numbers have two advantages over integers:
they can represent values between the integers
they can represent a much greater range of values
o Complex numbers
Complex numbers are combination of a real and imaginary part. Complex numbers are in the
form of X+Yj, where X is a real part and Y is imaginary pa”rt.
Example: 10 + 2j, 1+3.54j .
Strings
A string is a sequence of characters. In python we can create string using single (' ') or double
quotes (" "). Both are same in python. String is a collection of any valid characters in a
quotation marks ( ‘ ‘ or “ ”)
Each character of String in Python is a Unicode character
Strings are used to store information like name, address, descriptions. etc
Example: “hello”, “welcome‟, “sales2018”.
In Python string is a sequence of characters and each character can be individually access
using index. From beginning the first character in String is at index 0 and last will be at len-1.
From backward direction last character will be at index -1 and first character will be at –len.
Forward indexing
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C o m p u t e r S c i e n c e
Example :
str='Computer Science' Output
print('str-', str) # print string str- Computer Science
print('str[0]-', str[0]) # print first char str[0]- C
print('str[-1]-', str[-1]) # print last char str[-1]- e
print('str[9]-', str[9]) # print 10th char from forward str[9]-S
print('str[-11]-', str[-11]) # print 11th char from backward str[-11]-t
print('str[1:3]-', str[1:3]) # print string from position 1 to 3 str[1:3]- om
print('str[3:]-', str[3:]) # print string staring from 3rd char str[3:]- puter Science
print("str +'yes'-", str +'yes') # concatenated string str +'yes'- Computer Scienceyes
List
o Lists are compound data types i.e. they allow to store multiple values under one name of
different data types.
o Each item has its own index value. Index of first item is 0 and the last item is n-1.Here n is
number of items in a list.
o A list in python represents a list of comma-separated values of any data type between square
brackets [] .
o List can be changed/modified, i.e. Mutable
Code: Output:
List=[] []
List=[1,2] [1,2]
List=[1,2,13.34,16.24] [1,2,13.34,16.24]
Tuples
o Tulpes are compound data types i.e. they allow to store multiple values under one name of
different data types.
o Each item has its own index value. Index of first item is 0 and the last item is n-1.Here n is
number of items in a list.
o Tuples as those list which cannot be changed i.e. not modifiable (Immutable).
Code: Output:
Tuple=() ()
Tuple=(1,2) (1,2)
Tuple =(1,2,13.34,16.24) (1,2,13.34,16.24)
Dictionary
o It is an unordered collection of items where each item consists of a key and a value.
o It is mutable (can modify its contents) but Key must be unique and immutable.
o Dictionaries are defined inside Curly Brackets { } and values separated by comma.
o Keys defined in Dictionary cannot be same i.e. no two keys can be same.
Code: Output:
Dic={} {}
Dic={1:2,2:3,3:4} {1:2,2:3,3:4}
Dic={1:"JAVA",2:"C++",3:"HTML"} {1:"JAVA",2:"C++",3:"HTML"}
Variable Internals
o Python is an object oriented language. So everything in python is an object. An object is any
identifiable entity that have some characteristics/properties and behaviour.
o Every python object has three key attributes associated with it:
type of object
value of an object
id of an object.
Type of an object:
type of an object determines the operations that can be performed on the object.
Built – in function type() returns the type of an object.
Example: Output:
a=100 <class 'int'>
b=10.5 <class 'int'>
name="Jaques" <class 'float'>
type(a) <class 'str'>
type(100)
type(b)
type(name)
Value of an object
The data items stored in the object is a value of object. The value stored in an object is a
literals.
We can using print() to get the value of an object.
Example: Output:
a=100 100
name="India" India
print(a)
print(name)
Id of an object
It is the memory address of any object. Although id is dependent upon the system where it
is installed but in most cases it returns the memory location of the object
Built in function id() returns the id of an object.
Example: Output:
a=100 1911018608
id(a) 1911018608
id(100) 1911018608
print(id(a))