Data Handling CH - 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Ch – 4

Data Handling

Python provides a predefined set of data types for handling


the data it uses. Data can be stored any of data types.

Data Types :- Data can be my types e.g. Character, integer,


real, string etc. Anything enclosed in quotes represents
string data in python.
1. Numbers – It use to store numeric values.
(a) Integers - an unlimited range
(b) Float – an unlimited range, in python, the floating point
numbers have precision of 15 digits.
(c) Complex a=3j
(d) Booleans – Two values True (1) , False (0)
2. Strings – A string can hold any type of known
characters i.e., letters, numbers, and special characters,
of any known scripted language.
"abcd", "1234", "%$^15", "?##???"
A python string is a squence of characters and each
character be individually accessed using its index.
3. List – A list in python represents a list of comma-
separated values of any datatype between square
brackets.
[1,2,3,4,5]
['a', 'e','i','o','u']
4. Tuple – Tuples are represented as group of comma-
seprated valus of any type within parentheses.
A=(1,2,3,4)
b= ('a','e','i','o','u')
5. Dictionary – The dictionary is an unordered set of
comma-separated key:value pairs, within {}.
vowels={'a' : 1, 'e' : 2, 'i' : 3, 'o' : 4, 'u' : 5}
vowels=['a']
1

Mutable and Immutable Type


1. Imnutable type – are those that can never change their
value in place. int, float, string, tuple, boolean.
p=5
q=p
r=5
Variable names are just the references to value objects. The
variable name do not store values themselves.
id(5)
id(p)
id(q)
2. Mutable types – are those values can be changed in
place. Only three types are mutable in python lists,
dictionaries and sets.
chk=[2,4,6]
id(chk)
chk[1]=40
id(chk)
Note: - in mutable see, even after changing a value in the
list chk, its reference memory address has remained same.
That means the change has taken in place – the list are
mutable.

Variable Internals – Python is an object oriented language.


Python calls every entity that stores any values or any type
of data as an object.
An Object – is an entity that has certain properties and that
hold certain type of behavior e.g., integer vaue are object
they hold whole numbers only.
(i) The type of an object - determines the operations
that can be performed on the object.
a=4
type(a)
<class 'int'>
type(4)
<class 'int'>
(ii) The value of an object – It is the data- item
contained in the object.
A=4
print(A)
(iii) The id of an object – The id of an object is
generally the memory location of the object.
id(A)
16216240
id(4)
16216240
Operators – The operations being carried out on data, are
represented by operators. The symols that trigger the
operation/action on data are called

operators..
1. Arithmetic operators – Arithmetic operators uses by
Python for basic calculations. It requires two valaus
(operands) to calculate.
Additon, multiplication, division substraction, remainder,
exponentiation, floor division.
2. Unary operators – The operators that act on one
operand are referred to as Unary operators.
Unary - and Unary +
3. Binary Operators – Operators that act upon two
operands are referred to as binary Operators.
4. Relational Operators – Relational operators determine
the relation among different operands. Python provide
six relational operators for comparing values.
< less than, <= less than or equal to, == equal to >
greater than, >= greator than or equal to , != not
equal to
5. Identity operators – There are two identity operators in
Python (is and is not) . The identity operators are used
to check if both the operands reference the same object
memory, Identity operators compare the memory
locations of two objects and return True or False
accordingly.

6. Logical Operators: - Python provide three logical


operators (OR, AND , NOT) to combine existing
expression.
OR operator – evaluates to True if either of its (relational)
operands evaluates to True; False if both operands evaluate
to False.
eg. (4==4) or (5==8)
5>8 or 5 < 2
AND operator: - The and operator evaluates to True if both
of its (relational) operands evaluate to True; False if either
or both operands evaluae to false.
eg. (4==4) or (5==8)
5>8 or 5 < 2
NOT operator :- It work on single expression or operand. It
is a unary operator.
eg. not (5>2)
7. Combined Comparision Operators – We can chain
multiple comparisions which are like shortened version
of larger Boolean expressions.
A,B,C=5,7,2
A>B>D
8. Operator Precedence - When an expression or
statement involves multiple operators, Python resolves
the order of execution through Operator Precedence.

Operator Description
() Parentheses
** Exponentiation
+x, - x Positive, Negagive
( unary +, - )
*,/,//,%, +, - Multiplication,
Division, Floor division,
remainder, addition,
Substracation
< , <= , > , Comparision, identity
>= , operators
<>, != ,
== . is , is
not
Not x Boolean NOT
and Boolean AND
or Boolean OR
Python will evaluate the operator with higher precedence
first. But if the expression contains two operators that have
the same precedence? That time it determine the order of
operators.

9. Expression: - An expession in Python is any valid


combination of operators, lierals(value) and variables.
An expression is composed of one or more operations.
eg.: x=5*5 Valid expression
x-x7=>x y+-*2y (error)

10. Type Casting – Python internally changes the data


type of some operands so that all operands have the
same data type. This type of conversion is automatic
i.e., implicit and hence known implict type conversion.
An explicit type conversion is user – defined conversion
that forces and expression to be of specific type. The
explicit type conversion is knkown as Type Casing.
eg. d=float(27)

Working with math Module of Python


Python' standard library provides a module namely
math for math related functions that work with all number
types except for complex number.
By importing math library we ccan use its module.

#(cos x / ten x) +x

import math
(math.cos(x)/math.tan(x))+x

Q. The radius of a sphere is 7.5 meters. Write Python script


to calculate its area and volume. (Area of a sphere = Лr2
Volume of a sphere = 4Л3.

import math
r=7.5
area=math.pi*r*r
volume=4*math.pi*math.pow(r,3)
print("radius of the sphere : ", r, "Meters")
print("Area of the sphere : ", area, "Units square ")
print ("Volume of the sphere :" , volume, "Units Cube")

Dubugging – Dubuggng involves rectifying the code so that


the reason behind the bug gets resolved and thus bug is also
removed. Errors in a program - An error , sometimes is
called a bug.
1. Complile Time Errors – When a program complies, its
soruce code is checked for whether it follows the
programming languages rules or not.
Syntax error
x<-y*z
if X=(X*Y)
2. Semantics Error
3. Logical Error
4. Run Time Error

You might also like