Ch-8 Data Handling
Ch-8 Data Handling
DATA HANDLING
By:
Kshama Raut
PGT CS
K V Ambarnath
INTRODUCTION
1. Number In Python:
It is used to store numeric values
a. Integers
a = "101" # string
b=int(a) # converts string data type to integer.
c=int(122.4) # converts float data type to integer.
print(b)
print(c)
Run Code Output :-
101
122
DATA TYPES
Output :- 301.4
121.0
DATA TYPES
c. 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 part.
e.g.
a = complex(5) # convert 5 to a real part val and zero imaginary part
print(a)
b=complex(101,23) #convert 101 with real part and 23 as imaginary part
print(b)
Output :-
(5+0j)
(101+23j)
DATA TYPES
2. String In Python:
A string is a sequence of characters. In python we can create string using single ('
') or double quotes (" ").Both are same in python.
e.g.
str='computer science'
print('str-', str) # print string
print('str[0]-', str[0]) # print first char 'h'
print('str[1:3]-‘,str[1:3]) # print string from postion 1 to 3 'ell'
print('str[3:]-',str[3:]) # print string staring from 3rd char 'llo
world'
print('str *2-',str *2 ) # print string two times
print("str +'yes'-",str +'yes') # concatenated string
Output
str- computer science str[0]- c
str[1:3]- om
str[3:]- puter science
str *2- computer sciencecomputer science
str +'yes'- computer scienceyes
DATA TYPES
Output
c
o
m
p
s
c
DATA TYPES
3. Boolean In Python:
Output
False
DATA TYPES
4. List In Python:
List are collections of items and each item has its own
index value.
It is ordered sequence of values.
Values of list enclosed in square bracket.
It contain items Homogeneous(same) or e.g. of list
Hetrogeneous(different) list =[6,9]
list[0]=55
It is mutable(changeable data type. print(list[0])
Ex : print(list[1])
OUTPUT
F=[1,2.5,”abc] 55
9
DATA TYPES
5. Tuple In Python
Tuple are also collections of items and each item
has its own index value.
It is also ordered sequence of values.
Values of tuple enclosed in round bracket.
It also contain items Homogeneous(same) or
Hetrogeneous(different)
It is immutable(not changeable) data type
e.g. of tuple
tup=(66,99)
Tup[0]=3 # error message will be
DATA TYPES
6. Set In Python
It is an unordered collection of unique and
immutable (which cannot be modified)items.
e.g.
set1={11,22,33,22}
print(set1)
Output
{33, 11, 22}
DATA TYPES
7. Dictionary In Python
It is an unordered collection of items and each item consist of a
key and a value.
e.g.
dict = {'Subject': 'comp sc', 'class': '11'} print(dict)
print ("Subject : ", dict['Subject'])
print ("class : ", dict.get('class'))
Output
{'Subject': 'comp sc', 'class': '11'}
Subject : comp sc
class : 11
Mutable and Immutable Types
In Python, Data Objects are categorized in two types-
• Mutable (Changeable)
• Immutable (Non-Changeable)
Look at following statements carefully p
= 10
q=p
r = 10
they will represent 10, 10, 10
Now, try to change the values p
= 17
r=7
q =9
did the values actually change?
Answer is NO. Because here values are objects and p, q, r are their
reference name. To understand it, lets see the next slide.
Mutable and Immutable Types
OUTPUT
('Data type of num_int:', <type 'int'>)
('Data type of num_str before Type Casting:', <type 'str'>) ('Data
type of num_str after Type Casting:', <type 'int'>) ('Sum of
num_int and num_str:', 57)
('Data type of the sum:', <type 'int'>)
Type Casting
• As we know, in Python, an expression may be consists of
mixed datatypes. In such cases, python changes data types of
operands internally.
This process of internal data type conversion is called implicit type
conversion.
• One other option is explicit type conversion which is like-
<datatype>(identifier)
For ex
a=“4”
b=int(a)
Another ex
If a=5 and b=10.5 then we can convert a to float. Like
d=float(a)
In python, following are the data conversion functions-
(1) int ( ) (2) float( ) (3) complex( ) (4) str( ) (5) bool( )
TypeconversionandTypecasting
Logical Error
If a program is not showing any compile time error or run
time error but not producing desired output, it may be
possible that program is having a logical error.
Some example-
• Use a variable without an initial value.
• Provide wrong parameters to a function
• Use of wrong operator in place of correct operator
required for operation X=a+b (here – was required in
place of + as per requirement
DEBUGGING
Logical errors –logicalerrors causetheprogramtobehaveincorrectly,but they do not
usually crash the program. Unlike a program with syntax errors, a program with logic errors can
be run, but it does not operate as intended. Consider the following example of an logical error:
x = float(input('Enter a number: ')) y
= float(input('Enter a number: ')) z =
x+y/2
print ('The average of the two numbers you have entered is:',z)
The example above should calculate the average of the two numbers the user enters. But,
because of the order of operations in arithmetic (the division is evaluated before addition)
the program will not give the right answer:
Enter a number: 3
Enter a number: 4
The average of the two numbers you have entered is: 5.0
>>>
To rectify this problem, we will simply add the parentheses: z = (x+y)/2
Now we will get the right result:
>>>
Enter a number: 3
Enter a number: 4
The average of the two numbers you have entered is: 3.5
>>>
DEBUGGING
OverFlowError This exception generates when result of a mathematical calculation exceeds the
limit.
KeyError This exception generates due to non-availability of key in mapping of dictionary
FOFError This exception generates when end-of-file condition comes without reading input
of a built in function.
DEBUGGING
import pdb
pdb.set_trace()
Debugger tool
Then, a box will be opened and a message will come
saying DEBUG ON
Then, we will open our program from file menu and will run it.
DEBUGGING
Debugger tool
Then after it will be shown like this in debugger
Click on STEP button for each line execution one by one and result will be displayed in
output window. When we will get wrong value, we can stop the program there and can
correct the code.