How do i use boolean operators and precedence?



  • The boolean operators are:

    NOT

    AND

    OR

    If both comparisons are true or false, the result will be true

    AND

    The and operator, both comparisons are true 5 is equal to 5 and 12 is greater than 6.

    >>> and_example = (5 == 5) and (12 > 6)
    >>> print(and_example)
    True
    

    Let's change one of the comparisons, make 12 less than 6 and see the result.

    >>> and_example = (5 == 5) and (12 < 6)
    >>> print(and_example)
    False
    

    If both comparisons are not true when using the and operator.

    >>> and_example = (5 == 4) and (12 < 6)
    >>> print(and_example)
    False
    

    OR

    If one or the other comparison is true, the result will be true.

    >>> or_example = (5 == 5) or (12 > 6)
    >>> print(or_example)
    True
    
    >>> or_example = (5 == 5) or (12 < 6)
    >>> print(or_example)
    True
    
    >>> or_example = (5 == 4) or (12 < 6)
    >>> print(or_example)
    False
    

    NOT

    Make the result opposite. This comparison is false, but the not makes it true.

    >>> not_example = not (12 > 12)
    >>> print(not_example)
    True
    

    The boolean operators have a precedence when comparisons are done, they have the following precendence:

    NOT

    AND

    OR


Log in to reply
 

© Lightnetics 2024