Data Handling GR 11
Data Handling GR 11
***************************************************************
4. DATA HANDLING
Python provides a predefined set of data types for handling the data it uses.
Data can be stored in any of the data types.
Data type
It is the way to identify the type of data stored in a variable. Data can be of
many types. Python offers the following built-in core data types:
1. Numbers
Number data types are used to store numeric values in Python. The Numbers in
Python have following core data types:- i) Integers
Integers are whole numbers that have no fractional parts. It can be positive or
negative.
There are two types of integers in Python:
a) Integers (signed):- It is the normal integer representation of whole
numbers. Python 3.x provides a single data type called int to store any integer;
whether big or small.
b) Boolean:- These represent the truth values False and True. The
Boolean type is a subtype of integers as the Boolean values (True or False)
represents 1 or 0 respectively. It is represented by the data type bool.
OPERATORS
The symbols that trigger an operation/action on data are called Operators. The
operations are represented by operators and objects of the operands are referred
to as Operands.
Python provides the following operators:-
1. Arithmetic Operators
Python provides two types of arithmetic operators. They are
a) Unary Operators
The operators that act only on one operand is referred to as Unary operator.
Unary + and Unary – are the two operators.
Eg: if a=5 then +a means 5 (Unary +)
If a=-5 then +a means -5 (Unary -)
b) Binary Operators
Operators that act upon two operands are referred to as binary operators. The
binary operators in Python are:
i) Addition Operator (+)
It adds the values of its operands and the result is the sum of the values of its
two operands. Eg: 5+10 results 15
ii) Subtraction Operator(-)
It subtracts the second operand from the first.
Eg: 5-10 results -5
iii) Multiplication Operator(*)
It multiplies the value of its operands.
Eg: 5 * 10 results 50
2. Relational Operators
Relational operators determine the relation among different operands. Python
provides six relational operators for comparing values (thus also called
comparison operators). If the comparison is true, the relational expression results
into the Boolean value True. If the comparison is false, the relational expression
results into the Boolean value False.
The six relational operators are:
< (less than) > (greater than) <=(less than or equal to)
>= (greater than or equal to) = =(equal to) != (not equal to)
For example,
3. Identity Operators
There are two identity operators in Python. They are
i) is ii) is not
They are used to check if both of its operands points to the same memory
location or not and also returns True or False accordingly.
4. Logical Operators
Python provides three logical operators to combine existing expressions. These
are or, and & not.
i) The or Operator
The or operator combines two expressions, which makes its operands. The or
operator evaluates to True if either of its operands (expressions) evaluates to
True; False if both operands evaluates to False.
E.g. >>>(5>10) or (5%2==1) results True
>>>(6>10) or (10<6) results False ii)
The and Operator
The and operator combines two expressions, which makes its operands. The and
operator evaluates to True if both of its operands evaluates to True; False if
either or both operands evaluates to False.
E.g. >>> (5>10) and (5%2==1) results False
>>> (6<10) and (10>6) results True.
iii) The not operator
The Logical not operator works on a single operand. i.e it is a unary operator. The
logical not operator negates the truth value of an expression. That is if the
expression is true, the not expression results a False value and vice versa. e.g.
>>> not 5 results a False value because 5 is non-zero.
>>> not 0 results a True value
>>>not(5>10) results True
5. Augmented Assignment Operator
Python has an assignment operator = which assigns the value specified on the
RHS to the variable on the LHS of ‘=’.
Python also offers augmented assignment arithmetic operators, which combine
the impact of arithmetic operator with an assignment operator.
If you want to add value of b to a and assign the result to a, then instead of
writing a=a+b, we can write a+=b.
PRECEDENCE OF OPERATORS
Operator Description
** Exponentiation (raise to the power)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
<= < > >= Comparison operators
== , != Equality operators
is, is not Identity operators
in, not in Membership operators
not, and, or Logical operators