Boolean logic – OR operator – Control Structure In Python

 

Boolean Logic in Python, As we all know, there is a type of variables available in Python, which is of Boolean type. A boolean type variable can have only two possible values, ‘True’ and ‘False’. Note that, as Python is a case-sensitive language so if we assign the values as ‘true’ and ‘false’ to a boolean type variable, then we will get an error.

Boolean logic is used to make more complicated conditions for if, elif statements, that rely on more than one conditions. We use Boolean Operators in Python to setup boolean logic. There are three boolean operators in Python –> ‘and’, ‘or’, and ‘not’. These boolean operators take two arguments, evaluates them (evaluation will be done as per the boolean operator is defined) and returns a boolean value (i.e. either ‘True’ or ‘False’). This boolean value is considered by the conditional statements (i.e. either ‘if’ or ‘elif’ statements) and the process is completed accordingly. If the value returned by the boolean operator is True then the conditional statement will be executed, otherwise not.

Let’s take some example of boolean logic using boolean operators:

# and operator

>>>True and True
True

>>>True and False
False

>>>False and False
False

# or operator

>>>True or True
True

>>>True or False
True

>>>False or False
False

# not operator

>>> not True
False

>>>not False
True

Now, there is a point we need to keep in mind. The point is, every non-zero value in Python is considered as ‘True’. If a variable contains any non-zero value, then that variable will also be considered as a ‘True’ value if we pass that variable in any boolean operator.

Let’s discuss the boolean operators in brief:


‘OR’ Operator in Python

Like ‘and’ operator, ‘or’ operator also takes two arguments and returns one boolean value after comparing the two arguments.

  • ‘or’ operator returns False if both arguments are False.
  • If only one argument is True among the two arguments, ‘or’ operator will return True value.
  • If the two arguments are True (obviously, the result will be True) then ‘or’ operator returns the first argument (even if the first argument is a numeric non-zero value then also ‘or’ operator will return that numeric value, not boolean True value). As example
    # Keep in mind every non-zero values are True in Python
    #______________________________________________________
    
    >>> 5 or 6
    
    5
    
    >>> 6 or 5
    
    6
    
    #______________________________________________________
    
    >>> True or 5
    
    True
    
    >>> 5 or True
    
    5
  • If the two arguments are False (obviously, the result will be False) then ‘or’ operator returns the second argument. As example:
    # Keep in mind 0 is False in Python
    #__________________________________
    
    >>> False or 0
    
    0
    
    >>> 0 or False
    
    False
  • There is a special case regarding ‘or’ operator. Assume among the two arguments one is a numeric value (may be zero or a non-zero value, if zero then it is False and if non-zero then it is True). Now, after evaluation, if the result is present as a numeric value in the arguments (that means if the result is True but in the argument, there is one boolean False and a non-zero number), then that numeric value will be the output, not a boolean representation of that value. Let’s clear this with an example:
    # One numeric value and a boolean value
    #_______________________________________
    
    >>> 5 or False
    
    5
    
    >>> False or 6
    
    6
    
    # Note that there is no True keyword present in the arguments,
    # but as any non-zero number is considered as True in Python 
    # so the result will be True, but as in the arguments, instead of True 
    # 5 and 6 are there respectively so the result will be 5 and 6 respectively
    
    #________________________________________________________________________
    # Now observe the difference of the below examples with the above examples
    
    >>>True or False
    True
    
    >>>False or True
    True
  • If a variable contains a non-zero value then the variable will be treated as a True value, and if the variable contains zero then the variable will be treated as False in python. As example:
    # Case - 1 --> Variable contains non zero value
    #______________________________________________
    >>>a=5
    #______________________________________________
    
    >>> a or True
    
    5
    
    # Note that, in this case the output is 5. As we know if both of the arguments are
    # True then the first argument is returned by the 'or' operator. Now in this case
    # as the variable a contains 5 so 5 is the output
    
    >>> True or a
    
    True
    
    >>>a or a
    5
    
    # The above mentioned reason also applies here
    
    #______________________________________________________________________________
    
    # Case - 1 --> Variable contains zero value
    #______________________________________________
    >>>a=0
    #______________________________________________
    
    >>> False or a
    
    0
    
    # Note that, in this case the output is 0. As we know if both of the arguments are
    # False then the second argument is returned by the 'or' operator. Now in this case
    # as the variable a contains 0 so 0 is the output
    
    >>> a or False
    
    False
    
    >>>a or a
    0

Let’s take some programming examples of ‘or’ operator:

a=5
b=6
if(a or b):
    print("At least one is True")
else
    print("Both are False")

As output we will get:

At least one is True

That was all about ‘or’ operator.

Leave a Comment

Your email address will not be published. Required fields are marked *