Python Logical Operators with Examples

Python logical operators allow us to perform logical AND, OR, and NOT operation between boolean values. In this article, we will look into the logical operator’s execution in more detail.

Python Logical Operators

There are three logical operators in Python.

Logical Operator Description Simple Example
and Logical AND Operator flag = True and True = True
or Logical OR Operator flag = False or True = True
not Logical NOT Operator flag = not(False) = True

Python Logical AND Operator

Here is a simple example of a logical and operator.

Output: Both x and y are positive numbers

Before the logical operation is performed, the expressions are evaluated. So the above if condition is similar to below:

In Python, every variable or object has a boolean value. We have explained this in more detail at Python bool() function.

Output: Both x and y have boolean value as True because ‘x’ and ‘y’ len() function will return 2 i.e. > 0.

If we use “and” operator and the first expression evaluates to False, then the further expressions are not evaluated.

Let’s confirm the above statement by defining a custom object. Python uses __bool__() function to get the boolean value of an object.

Output:

Notice that __bool__() function is called only once, confirmed from the print statement output.

Below diagram depicts the flow chart of logical and operation.

Python Logical and Operator Flow Chart

Python Logical and Operator Flow Chart

Did you noticed the one-liner return statement? If you are confused with it, please check out Python ternary operator.

Python Logical OR Operator

Let’s look at a simple example of logical OR operator.

Output: At least one of x and y is positive number

If we use “or” operator and the first expression evaluates to True, then the further expressions are not evaluated.

Let’s confirm this with a simple code snippet. We will reuse the Data class defined earlier.

Output:

Notice that the __bool__() method is called only once and since it’s evaluated to True, further expressions are not evaluated.

Let’s run another example where both Data object boolean value will return False.

Output:

Finally, below image depicts the flow chart of logical or operator.

Python Logical and Operator Flow Chart

 

Python Logical or Operator

Python Logical NOT Operator

This is a very simple operator that works with a single boolean value. If the boolean value is True then not operator will return False and vice-versa.

Let’s look at a simple example of “not” operator.

Output: 37 is not divisible by either 3 or 4

Python Logical and Operator Flow Chart

 

Python Logical Not Operator

Python Operators Are Evaluated From Left to Right

Let’s look at an example where we have multiple logical operators. We are not mixing different types of Python operators, so that operator precedence won’t come in picture and it will be easier to explain the output.

We will also override the __bool__() function to print “id” value.

Output:

Notice that the boolean value is calculated for d1 first, thus confirming that the execution is happening from left to right.

Logical operators work with any custom object?

In Python, every object has a boolean value. So logical operators will work with any custom objects too. However, we can implement __bool__() function to define our custom rules for an object boolean value.

Summary

Python logical operators allow us to perform logical operations. We looked into logical operators in detail, how their execution is optimized to take the least amount of time. We also confirmed that the operator’s execution is performed from left to right side.

You can checkout complete python script and more Python examples from our GitHub Repository.

By admin

Leave a Reply