Data Handling Part 4 Ip
Data Handling Part 4 Ip
(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:
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.
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
11<13>12
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’)