Class11 CS DataHandling Updated
Class11 CS DataHandling Updated
Backward indexing
0 1 2 3 4 5 6 7 8
Subject C O M P U T E R S
-9 -8 -7 -6 -5 -4 -3 -2 -1
8. + Unary + .This is a Eg
unary operator means A= +10
it works on a single
operand. This
operator is used to
denote a number is
positive.
9. - Unary - . This is a Eg
unary operator means A= -10
it works on a single
operand. This
operator is used to
denote a number is
negative.
Relational operators
Relational operators determine the relation among
different operands. Python provides six relational
operators for comparing values. These are also called
as comparison operators.
The six relational operators are
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Relational operators work on nearly all types of data in
Python such as numbers, strings, list, tuples etc.
They work on the following principles
1) For numeric types, values are compared after
removing trailing zeros after decimal point from a
floating point number(example 6 and 6.0 are treated as
equal)
2) Strings are compared on the basis of lexicographical
ordering (ordering in dictionary).
Capital letters are considered lesser than small letters
i.e 'A' is less than 'a'.
Care should be taken for non printing characters like
space.
Example >>>"Apple" == " Apple"
False
3) Two list and similarly two tuples are equal if they
have same elements in same order.
4) Boolean true is equivalent to 1(numeric 1) and
Boolean false is equivalent to 0(numeric zero) for
comparison purposes.
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 i.e
identity operators compare the memory locations of
the two objects and return True or False accordingly.
is: A is B , returns True if both its operands are
pointing to the same object (i.e both referring to the
same memory location) returns False otherwise.
is not: a is not b , returns True if both its operands are
pointing to different objects (i.e both referring to
different memory locations) returns False otherwise.
Example >>> a=235
>>> b=240
>>> c=235
>>> a is b
False
>>>a is c
True
>>> print (id(a),id(b),id(c))
492124000 492124080 492124000
Now on changing the value of B so that it is not
referring to the same integer object, then the
expression
will return True
>>> b=b-5
>>> a is b
True
>>>print(id(a),id(b),id(c))
492124000 492124000 492124000
Now b is also pointing to the same integer object as’ a’
, i.e all a, b, c reference to the same memory location.
The is not operator is opposite to the is operator. It
returns True when both its operands are not referring
to the same memory address.
Equality (==) and Identity (is) : An important relation
We have seen in previous examples that when two
variables are referring to the same value the is
operator returns True. When the is operator returns
true for two variables, it implicitly means that the
equality operator will also return True.
Expression a is b returns True means that a==b will
also be True
Example
>>> print (a,b )
235 235
>>> a is b
True
>>> a==b
True
The other way round may not be always true i.e in
some cases we find that the operator == returns True,
for some, but is operator returns False.
Example:
>>> s1= ‘ abc’
>>> s2= input(“ Enter a string “)
Enter a string abc
>>> s1== s2
True
>>> s1 is s2
False
>>> s3= ’abc’
>>> s1 is s3
True
The reason behind this behaviour is that there are few
cases where Python creates two different objects even
though both store the same value.
They are
1. Input of Strings from the console
2. Writing integers literals with many digits.
3.Writing floating point and complex literals (very big
integers).
Logical operators
The logical operators are[ OR , AND ,NOT] that refer to
the ways relationships(among values) can be
connected.
Truth value testing: Python Associates with every
value type, some truth value (the truthiness), i.e
Python internally categorises them as true or false.
Any object can be tested for truth value. Python
considers following false ( i.e the truth value as false)
and true.
Values with Truth value as Values with Truth
false value as true.
None
False (Boolean value false)
Zero of any numeric type
(0 ,0.0 ,0j) All other values are
An Empty sequence (Eg ‘ ‘, considered true
(),[])
(‘ ‘ is empty string,( ) is
empty tuple, [ ] is empty
list)
Any empty mapping eg
{ }
OPERATOR DESCRIPTION
() Parantheses
** Exponentiation
~ Bitwise NOR
+ ,- Positive,Negative,Unary
*,/,//,% Multiplication,Division,Floor division,Remai
OPERATOR DESCRIPTION
^ Bitwise XOR
| Bitwise OR
Operator
s Syntax Functioning
+ x+y Addition
– x–y Subtraction
* x*y Multiplication
/ x/y Division
// x // y Quotient
% x%y Remainder
** x ** y Exponentiation
Example:
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
print(div)
Output : 52
28
480
3.3333333333333335
C. Relational Expressions: In these types of
expressions, arithmetic expressions are written on
both sides of relational operator (> , < , >= , <=).
Those arithmetic expressions are evaluated first,
and then compared as per relational operator and
produce a boolean output in the end. These
expressions are also called Boolean expressions.
Example:
# Relational Expressions
a = 21
b = 13
c = 40
d = 37
p = (a + b) >= (c - d)
print(p)
Output: True
D. Logical Expressions: These are kinds of
expressions that result in either True or False. It
basically specifies one or more conditions. For
example, (10 == 9) is a condition if 10 is equal to 9.
As we know it is not correct, so it will return False.
Studying logical expressions, we also come across
some logical operators which can be seen in logical
expressions most often. Here are some logical
operators in Python:
Operato
r Syntax Functioning
Example:
P = (10 == 9)
Q = (7 > 5)
# Logical Expressions
R = P and Q
S = P or Q
T = not P
print(R)
print(S)
print(T)
Output
False
True
True
E. Bitwise Expressions: These are the kind of
expressions in which computations are performed
at bit level.
Bitwise AND &
Bitwise OR |
Bitwise left shift <<
Bitwise Right shift >>
Example:
# Bitwise Expressions
a = 12
x = a >> 2
y = a << 1
print(x, y)
Output
3 24
LIST AND TUPLES
List and tuples are Pythons compound data types.
Can be changed / modified (i.e immutable) but
tuples cannot be changed or modified (i.e
immutable).
List: A list in Python represents a list of comma -
separated values of any data type between square
brackets.
Example
[ 1, 2,3,4,5 ]
[ a, e, i ,o, u ]
[ "Riya", 102, 79.5]
>>> myset1 ={ 1, 2 , 3, 4, 4}
>>> print(myset1)
{ 1, 2,3, 4}
>>> type (myset)
< class ' set'>
Python data types strings, list, tuples, dictionary
etc are all iterables.
>>>set1={1,2.[3.4]}
set1={1,2,[3,4]} type error: Unhashable type: 'List'
Dictionary
A dictionary in Python is an an ordered set of
comma separated key: Value pairs, within{} , with
the requirement that within a dictionary no two
keys can be the same(i.e there are unique keys
within a dictionary)
Examples
>>> vowels={'a':1, 'e':2, 'i':3, 'o':4 , 'u':5 }
>>> vowels ['a']
1
In the above code segment a, e , i, o,u are keys of
the dictionary vowels, 1, 2,3,4,5 you are values of
these keys respectively. Specifying the keys inside[]
after the dictionary name gives the corresponding
value from the key: Value pair inside the
dictionary.
MUTABLE AND IMMUTABLE TYPES
Python data objects can be broadly categorised
into mutable and immutable types(changeable for
modifiable and non modifiable types)
Immutable types
The imitable types are those data objects which
can never change their value in place. Following
types are immutable: Integers, floating point
numbers, Boolean, strings, tuples.
Example
P=5
Q=P
R=5
All variables having same value reference, the
same value object, i.e P,Q,R will all reference the
same integer objects.
Your teach integer value is an immutable object.
>>>P=5
>>>Q=P
>>>R=5
>>>id(5)
1457662208
>>>id(P)
1457662208
>>>id(Q)
1457662208
>>>id(R)
1457662208
We notice had id () is returning same memory
address for values 5,P,Q,R which means all these
are referencing the same.
Example 2
P=10
R=7
Q=R
Here the variable names are made to point to
integer objects. Hence their memory addresses
that they reference will change.
>>>P=10
>>>R=7
>>Q=R
>>>id(10)
1457662288
>>>id(P)
1457662288
>>>id(7)
1457662240
>>>id(Q)
1457662240
>>>id(R)
1457662240
>>>id(5)
1457662208
On assigning 5 to any other variable
>>>t=5
>>>id(t)
1457662208
Hence it is clear that variable names are stored as
references to a value object. Time when we change
the value, reference memory addresses changes.
Variables (of certain types) are NOT LIKE are
storage containers i.e with fixed memory address
where value changes every time. Hence they are
immutable.
MUTABLE TYPES
The mutable types are those whose values can be
changed in place.
There are only three mutable types in Python -
lists, dictionaries and sets.
To change any member in a list you may write as
chk=[2,4,6]
chk[1]=40
This will make the change in chk as [2,40,6]
>>>chk=[2,4,6]
>>>id(chk)
150595536
>>>chk[1]=40
>>>id(chk)
150595536
From the above program segment we see that even
after changing a value in the list chk , its reference
memory address has remained the same. That
means the change has taken place- the lists are
mutable.
3 ) q/(r+s)5
Solution: q/math.pow((r+s),5)
Debugging
Debugging refers to the process of locating the
place of error, cause of error and correcting
the code accordingly.
An error is also called as a bug and it is
anything in the code that prevents a program
from compiling and running correctly.
The different types of errors are Compile time
error and Run runtime errors and Logical
errors
Errors that occur during compile time are
compile-time errors .The two types of which
fall under this category are Syntax errors and
Semantic errors.
Syntax errors: Syntax errors occur when rules
of a programming language are misused i.e
when a grammatical rule of Python is violated.
Example :
Print(x)
Marks 1=85
Semantic error: Semantics refers to the set of
rules which give the meaning to a statement. A
statement may be grammatically correct but
there are semantic rules of programming
language and violation of such rules is called
as a semantical error.
Example X*Y= Z.
****************************************************
**********************