Followers

Wednesday, November 3, 2021

Logical Operator - Operators

Logical Operator
In an earlier section, we discussed relational operators that establish relationships among the values. This section talks about logical operators, the Boolean logical operators (or, and, not) that refer to the ways these relationships (among values) can be connected. Python provides three logical operators to combine existing expressions. These are or, and, not. 
Before we proceed to the discussion of logical operators, it is important for you to know about Truth value Testing, because in some cases logical operators base their results on truth value testing.
 



Truth Value Testing
Python associates with every value type, some truth value (the truthiness), i.e., Python internally categorizes them as true or false. Any object can be tested for truth value. Python considers the following values false, (i.e., with true-value as false) and true :

Do not confuse between Boolean values True, False and truth values (truthiness values) true, false. Simply put truth-value test for zero-ness or emptiness of a value. Boolean values belong to just one data type, i.e., Boolean type, whereas we can trust truthiness for every value object in Python. But to avoid any confusion, we shall be giving truth values true and false in small letters and with a subscript tval, i.e., now on this chapter truetval and falsetval will be referring to truth-values of an object.

The or Operator 
The or operator combines two expressions, which makes its operands. The or operator works in these 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                (e.g., p>q, j!=k) then the or operator performs as per following 
        principle :
          The or operator evaluates to True if either of its (relational)               operands evaluates to True; False if both operands evaluate 
         to false. 
That is : 
               X                        Y                           X or Y
             False                  True                          False
             False                  True                          True                                             True                   False                         True                                             True                   True                          True

Following are some examples of this or operations:  
(4==4) or (5==8)    results into True because first expressions                                             (4==4) is True.
5 > 8 or 5 < 2         results into False because both expressions 5 > 8
                                and 5 < 2 are False.
  • Numbers / Strings / Lists as operands
      When or operator has its operands as numbers or strings or lists(e.g., 'a', "or", 3 or 0, etc)  then the or operator performs as per the following principle :
     In an expression x or y, if the first operand (i.e., expression x) has falsetval, then return second operand y as result, otherwise, return x.
That is : 
                X                       Y                         X or Y
            false
tval                falsetval                       y
            falsetval                truetval                        y
            truetval                 falsetval                       x
            truetval                 truetval                        x 

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  e.g., 

      20 > 10 or "a" + 1 > 1

will give you result as
        True        
without checking the second operand of or i.e., "a" + 1 > 1, which is syntactically wrong - you cannot add an integer to a string.

So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!

Vande Mataram!!! 

No comments:

Post a Comment

If you have any doubts, please do let me Know.