=================================================================
4. Logical Operators (Comparision Operator)---Most
Imp
=================================================================
=>The Logical Operators are Used for Combining Two OR More Relational Expressions.
=>If Two OR More Relational Expressions are connected with Logical Operators then
It is Called Logical Expression.
=>The Logical Expression gives the Result either True OR False.
=>The Logical Expression is also Called Compound Test Condition and whose Result
can be either True OR False.
=>In Python Programming, we have 3 Types of Logical Operators. They are Given in
the following Table.
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
SLNO SYMBOL MEANING
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
1. and Physical ANDing
2. or Physical ORing
3. not ---------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
1. 'and' Operator
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
Syntax: RelExpr1 and RelExpr2
----------
=>The Functionality of 'and' Operator can be expressed by using the Following Truth
table
-----------------------------------------------
-------------------
RelExpr1 RelExpr2 RelExpr1 and RelExpr2
-----------------------------------------------
-------------------
True False False
False True False
False False False
True True True
-----------------------------------------------
-------------------
--------------------------
Examples-1
--------------------------
>>> True and False
False
>>> False and True
False
>>> False and False
False
>>> True and True
True
--------------------------------
Examples-2
--------------------------------
>>> 10>20 and 30>20---------------------False----Short Circuit Evaluation
>>> 10>20 and 30>20 and 30>40-----False----Short Circuit Evaluation
>>> 10>2 and 20>3 and 40>30--------True------Full Length Evaluation
>>> 10>2 and 30>40 and 40>20-------False----Short Circuit Evaluation
********************************************
Short Circuit Evaluation in the case of 'and Operator
********************************************
=>If Multiple Relational Expressions connected with Logical Operator (Logical Expr)
and if Initial Relational Expression is Evaluated as False then PVM will not
evaluate Rest of the Sub Sequent Relational Expressions and Result of Entire
Logical Expression is Considered as False. This Prcoess of Evaluation is Called
Short Circuit Evaluation
********************************************
Examples-3
********************************************
>>> 100 and 200-------------------200
>>>
>>> -123 and 234-------------------234
>>> 0 and 123-----------------------0
>>> 123 and 0----------------------0
>>> 123 and 234 and 145--------145
>>> False and 123 and True-----False
>>> -12 and 0b000 and 234-----0
>>> "PYTHON" and "DSA" and "C"------'C'
>>> "False" and "True"---------------------'True'
>>> "" and "$"---------------------------------''
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
2. 'or' Operator
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
Syntax: RelExpr1 or RelExpr2
----------
=>The Functionality of 'or' Operator can be expressed by using the Following Truth
table
-----------------------------------------------
-------------------
RelExpr1 RelExpr2 RelExpr1 or RelExpr2
-----------------------------------------------
-------------------
True False True
False True True
False False False
True True True
-----------------------------------------------
-------------------
---------------------------------------------
Examples-2
---------------------------------------------
>>> True or False
True
>>> False or True
True
>>> False or False
False
>>> False or True
True
---------------------------------------------
Examples-2
---------------------------------------------
>>> 10>2 or 20>30-------------------------True------Short Circuit Evaluation
>>> 20>10 or 30>40 or 50>60----------True--------Short Circuit Evaluation
>>> 10>20 or 40>50 or 50>30---------True--------Full Length Evaluation
>>> 20>30 or 30>40 or 40>50----------False--------Full Length Evaluation
********************************************
Short Circuit Evaluation in the case of 'or' Operator
********************************************
=>If Multiple Relational Expressions connected with Logical Operator (Logical Expr)
and if Initial Relational Expression is Evaluated as True then PVM will not
evaluate Rest of the Sub Sequent Relational Expressions and Result of Entire
Logical Expression is Considered as True. This Prcoess of Evaluation is Called
Short Circuit Evaluation
********************************************
Examples-3
********************************************
>>> 10 or 20
10
>>> 10 or 0
10
>>> 0 or 20
20
>>> 10 or 30 or 40
10
>>> "PYTHON" or "Java" or "C"
'PYTHON'
********************************************
Examples-4
********************************************
>>> 10 or 20 and 30
10
>>> 20 and 30 or 40
30
>>> 20 or 40 and 50 or 60
20
>>> "Python" or "Java" and "C" or "HTML"
'Python'
********************************************
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
3. 'not' Operator
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax-1: not RelExpr
=>Syntax-2: not(RelExpr1 or RelExpr2)
=>Syntax-3: not(RelExpr1 and RelExpr2)
=>The functionality of 'not' Operator shown following Truth Table
-----------------------------------------------------------
-------
RelExpr1 not RelExpr1
-----------------------------------------------------------
-------
True False
False True
-----------------------------------------------------------
-------
---------------------------------------
Examples-1
---------------------------------------
>>> not True
False
>>> not False
True
---------------------------------------
Examples-2
---------------------------------------
>>> 10>20 and 30>20-----------False
>>> not(10>20 and 30>20)----True
>>> 20>10 or 30>20------------True
>>> not(20>10 or 30>20)------False
---------------------------------------
Examples-3
---------------------------------------
>>> not 20
False
>>> not 0
True
>>> not 123
False
>>> not "PYTHON"
False
>>> not 0b0000
True
>>> not 10-11
False
>>> not "not"
False
>>> not "False"
False
>>> not(not(bool(123)))
True
---------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------