Boolean Logic – AND operator – Control Structure In Python

 

To process the inputs as per user’s requirement, there are some conditional statements available in Python like other languages. They are:

  • if, else and elif block
  • for loop
  • while loop
  • Boolean logic
  • Operator precedence

We will discuss the above points one by one.


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 about the boolean operators in brief:


‘AND’ Operator in Python

‘and’ operator takes two arguments and returns a boolean value after comparing the two arguments:

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

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

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

As output we will get:

Both are True

That was all about ‘and’ operator.

Leave a Comment

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