Data Handling
Std XI – Computer Science / Informatics
Practices
Logical operators
Python provides three logical operators or, and
and not to combine existing expressions.
Before we study logical operators , lets learn Truth
value testing
Truth value testing
Python associates, with every value type some
truth value(the truthiness),ie. Pyhton internally
categorizes them as true or false.
Any object can be tested for truth value.
Python considers following values false,(ie. With truth –
value as false) and true:
The result of a relational expression can be True or False
depending upon the values of its operands and the
comparison taking place.
Boolean values True or False and truth values
(truthiness values) true ,false are different.
We can put truth-value test for zero-ness or
emptiness of a value.
Boolean values, belong to just one data type ie.
Boolean type, whereas we can test truthiness for
every value object in Python.
We shall use truth values true and false in small
letters and with a subscript tval.
true tval and false tval
The or Operator
The or operator combines two expressions , which
are its operands.
The or operator works in two ways:
1. Relational expressions as operands.
2. Numbers or strings or lists as operands.
Relational expressions as
operands
When or operator has its operands as relational
expressions (eg. p>q, j!=k, etc) then the or operators
perform as per the following principle:
The or operator evaluates to True if either of its
(relational ) operands evaluates to True;
Example False if
both operands evaluate to False.
(4==4) or (5 == 8)
X Y X or Y
True
False False False
False True True
True False True
5>8 or 5<2
True True True
False
Numbers/Strings/Lists as
operands
When or operator has its operands as numbers or strings or
lists (eg. ‘a’ or ‘ ‘ , 3 or 0 ) then it follows the following
principle:
In an expression x or y , if operand, (ie. x) has false
tval , then return second operand y as result, otherwise
return x.
X Y X or Y
false tval false tval Y
false tval true tval Y
true tval false tval X
true true X
Example
The or operator will test
the second operand only
if the first operand is
false; otherwise ignore
it; even if the second
operand is logically
wrong eg.
20>10 or “a” +1 >1
Will result as True
without checking the
second operand of or ie.
“a” +1>1 which is
syntactically wrong.
The and Operator
The and operator combines two expressions, which make
its operands.
The and operator works in two ways:
1. Relational expressions as operands.
2. Numbers or strings or lists as operands.
Relational expressions as
operands
When and operator has its operands as relational
expressions (eg. p>q, j!=k, etc) then the and operator
performs as per the following principle:
The and operator evaluates to True if both of its
Example
(relational ) operands evaluates to True; False if
either or both operands evaluate to and
(4==4) False.
(5 == 8)
False
X Y X and Y
False False False 5>8 and 5<2
False True False False
True False False
True True True 8>5 and 2<5
Numbers/Strings/Lists as
operands
When and operator has its operands as numbers or strings or
lists(eg. ‘a’ and ‘ ‘ , 3 and 0 ) then it follows the following principle:
In an expression x and y , if operand, (ie. Expression x) has
false tval , then return first operand x as result, otherwise
return y.
X Y X and Y
false tval false tval X
false tval true tval X
true tval false tval Y
true tval true tval Y
Example
The and operator will
test the second
operand only if the
first operand is true;
otherwise ignore it;
even if the second
operand is logically
wrong eg.
10 >20 and “a” +10
<5
Will result as False
ignoring the second
operand completely
even if it is wrong.
The not Operator
The Boolean/Logical not operator , works on single expression
or operand i.e. it is a unary operator.
The logical not operator negates or reverses the truth value
of the expression following it, i.e.
If the expression is True or true tval , then not expression is
False, and vice-versa.
The not operator returns always a Boolean value True or
False.
Example
Note Operator
not has a lower
priority than non-
Boolean
operators, so
not a = = b is
interpreted as
not (a = = b) ,
and a==
not b is a syntax
The Logical Operators
Chained comparison Operators
Example 1 1<2 and 2<3 can be written as
1<2<3
This statement will check if 1 was less than 2 and if 2 was less
than 3
Example 2 11<13 and 13>12 can be written as
11<13>12
This statement will check if 11 was less than 13 and if 13 was
greater than 12
Operator Precedence
When an expression or statement involves multiple
operators, Python resolves the order of execution through
Operator Precedence.
Operator Associativity
Python allows multiple operators in a single expression.
Example a<b+2 p<q>r
If the operators used in an expression have different
precedence, there is not any problem as Python will
evaluate the operator with higher precedence first.
But what if the expression contains two operators that
have the same operations.
In this case associativity helps determine the order of
operations.
Associativity is the order in which an expression (having
multiple operators of same precedence) is evaluated.
Almost all the operators have left-to-right
associativity except exponential (**) which has
right –to –left associativity.
Example *,/,// are having same associativity so
they will be evaluated from left to right order.
Example
>>> 7*8/5//2
5.0
>>>(((7*8)/5)//2)
5.0
>>>7*((8/5)//2)
0.0
>>>7*(8/(5//2))
28.0
An expression having multiple ** operators is evaluated
from right to left, ie.
2**3**4 will be evaluated as 2**(3**4) and Not as
(2**3)**4
Example
>>>3**3**2
19683
>>>3**(3**2)
19683
>>>(3**3)**2
729
Example :What is the o/p of the
codes:
x, z =5, 10
y= x+3
x=x-1
x=x+z
print(‘x:’, x, ’y:’, y, ’z:’, z)
O/P x: 14 y:8 z: 10
print(14//4, 14%4, print(2* ‘No’ +3* ‘!’)
14/4) print(2* (‘No’ + 3* ‘!’))
O/P 3 2 3.5
O/P NoNo!!!
No!!!No!!!
print(11+3)
print(type(1+3))
print(type(1+3.0)) print(‘11’ + ‘3’)
O/P <class ‘int’> O/P 14
<class ‘float’> 113
Why is the following code
giving
print(11+3)
error?
print(‘11’ + 3)
Error TypeError: can only concatenate str(not
“int”) to str
An integer cannot be added to a string value.
Which of the following expressions will yield an integer,
floating point, string type values as its output? Which
expression will result into an error?
i. 5*2
i, ii, vi, vii, will yield
ii. 5**2 integer type result.
iii. ‘5’ +’2’
iv. ‘5’ * 2 v, viii, ix will yield
v. 5/2 floating point result.
vi. 5//2 Iii, iv will yield string
vii.5%2 result.
viii.5+2.0
ix. 5.0 * 2.0 x will give an error because –
operator cannot be used for
x. ‘5’ -2 string and integer values.
Progress in Python 8.2
Check point 8.6