Chapter-3 Data Handling
Chapter-3 Data Handling
Handling
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.
Data type continue
1. Number In Python
It is used to store numeric values
a= 100
b= -100
c
= 1*20
print(a)
print(b)
print(c)
O
utput :-
1
00
Data type continue
Type Conversion of Integer
int() function converts any data type to integer.
e.g.
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
Visit : python.mykvs.in for regular updates
Data type continue
2. Floating point numbers
It is a positive or negative real numbers
with a decimal point.
e.g.
a = 101.2
b = -101.4
c = 111.23
d = 2.3*3
print(a)
print(b)
print(c)
print(d)
Run
Code
Outpu
t :-
101.2
-101.4
Data type continue
3. 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)Run Code
O
utput :-
(5+0j)
(101+23j)
Data type continue
2. String In Python
Strings:
A string can hold any type of known characters it means letters numbers
and special characters of any known scripted language. In python 3.x, each
character stored in a string is a Unicode character.
Unicode is a system designed to represent every character from every
language.
Following are all legal strings in Python:
“abcd”, “1234”, “$%^&”, ‘????’
String as a sequence of Characters
A Python String is a sequence of characters and each character can be individually accessed using its
index.
Name[0]=’P’=name[-6]
Forward Indexing
Name[1]=’P’=name[-5]
0 1 2 3 4 5
Name[2]=’P’=name[-4]
P Y T H O N Name[3]=’P’=name[-3]
Name[4]=’P’=name[-2]
-6 -5 -4 -3 -2 -1
Name[5]=’P’=name[-1]
e.g. of tuple
tup=(66,99)
Tup[0]=3 # error message will be displayed
print(tup[0])
print(tup[1])
Data type continue
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 type continue
7. Dictionary In Python
The dictionary is an unordered set of comma-separated key: value pairs,
within { } ,with the requirement that within a dictionary no two keys can be
the same
e.g.
name = {'Navneet': 35, 'Gaurav': '42',
‘Sunit’: 36, ‘Vikas’:40}
print(name)
print ("Navneet: ") Output=>> 35
Names[“vikas”]=44
print (“Vikas: ") Output=>>
44
Print(names.keys()) Output=>>
Navneet,,36,40
Input From Keyboard
variable = input(< Prompt to display>)
e.g. name= input(‘What is your name:’)
The input () function always returns a value of string type .
If you enter integer value it will be treated as string .
So we have to convert to desired type int,float.
age= int(input(‘What is your age:’))
type(age) =>> int
Operator
Operators are special symbols in Python that carry out arithmetic or
logical computation. The value that the operator operates on is called
the operand.
Arithmetic operators
Used for mathematical operation
Operator Meaning Example
x+y
+ Add two operands or unary plus
+2
x-y
- Subtract right operand from the left or unary minus
-2
* Multiply two operands x*y
Divide left operand by the right one (always results
/ x/y
into float)
** Exponent - left operand raised to the power of right x**y (x to the power y)
Operator continue
Arithmatic operator continue
e.g.
x=5
y=4
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
OUTPUT
('x + y =', 9)
('x - y =', 1)
('x * y =', 20) • Write a program in python to calculate the
('x / y =', 1) simple interest based on entered amount ,rate
('x // y =', 1) and time
('x ** y =', 625)
Operator continue
Comparison operators
used to compare values
Exampl
Operator Meaning
e
> Greater that - True if left operand is greater than the right x>y
< Less that - True if left operand is less than the right x<y
Output
('x > y is', False)
('x < y is', True)
('x == y is', False)
('x != y is', True)
('x >= y is', False)
('x <= y is', True)
Operator continue
Logical operators
Operator Meaning Example
Outpur
('x and y is', False)
('x or y is', True)
('not x is', False)
Operator continue
Bitwise operators
Used to manipulate bit values.
e.g.
a=5
b = 10
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if ( b not in list ):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
output
Line 1 - a is available in the given list
Line 2 - b is not available in the given
list
Operator continue
Python Identity Operators
Opera Description
tor
Evaluates to true if the variables on either side of the operator point to the
is same object and false otherwise.
Evaluates to false if the variables on either side of the operator point to the
is not same object and true otherwise.
e.g.
a = 10
b = 10
print ('Line 1','a=',a,':',id(a), 'b=',b,':',id(b))
if ( a is b ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same
identity")
OUTPUT
('Line 1', 'a=', 10, ':', 20839436, 'b=', 10,
':', 20839436)
Line 2 - a and b have same identity
Operator continue
Operators Precedence :highest precedence to lowest precedence table
Operator Description
** Exponentiation (raise to the power)
~+- Complement, unary plus and minus (method names for the last two are +@ and
-@)
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'>)
floor(n) It returns the largest integer less than or equal to n math.floor(4.2) returns 4
e It is mathematical
Visit : python.mykvs.in constant
for regular updates e (2.71828...) It is (2.71828...)