L4_Data Handling
L4_Data Handling
Python provides a predefined set of data types for handling the data it uses.
Data can be stored in any of these data types.
Lets discuss in more details.
Data Types
• Data Types are means to identify the type of data and associated
operations of handling it. Python offers following built-in datatypes:
1. Numbers (int, float, complex)
2. String Click to add text
3. List
4. Tuple
5. Dictionary
1.Number data types are used to store numeric values in python. It is further divided
into :
a) Integer : used to store whole number. Means no decimal. Integer can be
positive or negative Eg: 10, -7 etc. Further Integers are divided into :
-Integers signed or signed Integers: used to store numbers normal integers
i.e. whole nos. Integer can be any length, it is only limited by the memory available. The
integers can be +ve as well as –ve.
- Booleans : represents the truth values True or False. The Boolean type
values False/ True behave like values 0/1 i.e. False=0 and True=1.
To get the Boolean equivalent of 0 or 1 we can type bool(0) or bool(1)
EG:
• Further Boolean values True and False can be converted into string
i.e. ‘True’ and ‘False’. By using str( ) function.
• NOTE: str( ) function converts a value to string.
EG:
Floating Point nos: A number having fractional part is a floating-point
number. For eg: 3.14159 is floating-point number. The no. 12 is integer
but 12.0 is floating-point no.
In python, the floating point no. have precision of 15 digits.
ADVANTAGES: They can represent values between the integers.
They can represent a much greater range of values.
All variables are pointing to same location. Remember location no. is NOT
fixed.
NOTE: Python is an object oriented language so everything in python is an
object. So all variables are treated as an object.
id() function used on any data value/ variable returns the unique identity of
an object, which is assigned memory address.
• What are Mutable types?
Definition of Mutable object is an object is whose value can be changed in place.
Means in scene of the computer memory.
Lists, Dictionaries and sets are mutable types in python.
EG
Observe the memory location
of list, which remains the
same. Means the change
happens in same location.
Variable Internals
• Python is an Object Oriented Programming Language (OOPs)
• Every data stored in python is in the form of object.
• An object is an entity that has certain properties & that exhibits
a certain behaviour. EG: integer values are objects they hold
whole numbers only & they have infinite properties and
arithmetic behaviour.
• Similarly for all data types.
• Data type/ object has 3 key attributes:
Data type/ object has 3 key attributes:
Explain 3key attributes of variable
internals.
1) Type of an Object 2) Value of an object. 3)id of an object.
EG: EG: EG:
a=4 a=4 a=4
type(a) print(a) id(a)
• Two lists and two tuples are equal if they have same elements in the
same order.
• Boolean True is equivalent to 1 and Boolean False is equivalent to 0
for comparison purpose.
Program on comparison of values
#Program
a=3
b=13
p=3.0 True
a<b
True
a==p
False
a==True #checks for truth value i.e. 1
True
0==False #truth values of 0 is False so o/p True
True
1==True
#Program 3
#Program 2 c,e='n','N' NOTE: small letters
x='a' f,g='god','God' are considered bigger
y=97 d,h='g','god' than capital. Length
False
print(c<g) False of string will not
x==y
False
matter here.
print(f<h)
True Here, small letter ‘g’
y==ord('a') True
print(f==h) has bigger ascii value
False than capital ‘G’.
p=5
print(c==e)
False
p==True True
print(g=='God‘)
True
p=1 print('God'<'God house‘)
True False
p==True print('god'<'God house‘)
Comparison of tuple and lists
#program 4 #program 5
L=[1,2,3] L=(1,2,3)
M=[2,4,6] M=(2,4,6)
N=[1,2,3] N=(1,2,3)
L==M False L==M False
True True
L==N L==N
NOTE:
• Floating-point numbers are presented in memory in binary form.
Conversion to binary is done internally. So the comparing floating
numbers may give different result.
• EG: 0.1+0.1+0.1==0.3 o/p is False
• As compiler converts the 0.1+0.1+0.1 into binary and calculated the
answer is also binary. And comparing it with 0.3 but as 0.1+0.1+0.1 is
not equal to 0.3
17digit precision is the result so the
o/p is False
Relational operators with Arithmetic operators
a=10 and c=20 so a+5=15 c-2 = 18
So 15 > 18 is False
And 15 < 18 is True
1) None
5 or 0.0 5
‘hello’ or ‘ ‘ hello
‘ ‘ or ‘Xyz’ Xyz
NOTE:
1) ‘or’ operator checks for 1st operand and then 2nd operand.
2) If 1st operand is true then will NOT check for 2nd operand.
3) Will return result immediately if 1st operand is True.
Logical operators (and)
Eg: (4==4) and (5==5) results to True (4>3) and (5<8) results True
(4==8) and (5==5) results to False
Logical operators (and)
II) Number/string/lists as operands
In an expression x and y , if the 1st operand has falsetval , then return 1st
operand x as the result, otherwise return y
X Y Result (X and Y)
false truth value false truth value X
‘hello’ and ‘ ‘ ‘‘
‘ ‘ and ‘Xyz’ ‘‘
‘ ‘ and ‘ ‘ ‘‘ If 1st expression’s truth val is True the will returns Yth
val.
‘a’ and ‘b’ b
Logical operators (and)
NOTE:
1) ‘and’ operator checks for 1st operand and then 2nd operand.
2) If 1st operand is false then will NOT check for 2nd operand.
3) ‘and’ operator will test 2nd operand only if the 1st operand is True.
4) Will return result immediately if 1st operand is False.
Logical operators (not)
• The Boolean/logical not operator, works on single expression or
operand means it is a unary operator.
• The logical not operator negates or reverses the truth value of the
expression following it means if the expression is True or the truth
value, then not expression is False, and vice versa.
• It has lower priority than non-boolean operator, so not a==b is
interpreted as not(a==b), where as, a==not b is an syntax error.
Logical not operator can be represented using != operator
EG:
A=C=10
B=20
if(A!=B):
print(“A is not equal to B”)
EG: 11 <13 and 13>12 this statement is equivalent to the given below
Operator precedence
Like which operator will execute first like bodmas rule maths.
Refer to PG 104 (4.4.5)
right-to-left left-to-right
import math
print(math.ceil(100.1))
print(math.ceil(100.5))
print(math.ceil(47.5))
print(math.ceil(-45.17))
1)math.floor() return the integer by
eliminating the decimal part.
#Program on math module.
import math
print(math.floor(100.1))
print(math.floor(100.78))
print(math.floor(-100.1)) #goes back
print(math.pow(2,3)) #returns the 2 raised to 3 i.e. 8.0
print(math.sqrt(4)) #returns the sqrt of 4 i.e. 2.0
math.sqrt(math.pow(a,2)+math.pow(b,2)+math.pow(c,2))