Scripting

# Python Logical Operators

Logical operators can be used to perform different logical operations, which can be in the form of true or false. These operators are divided into three categories: logical AND, logical OR, and logical NOT, and are represented by different symbols and keywords in the Python programming language. The operand refers to the value on which the operator conducts the computation. In this post, we’ll go through the logical operator’s execution and discuss how you can implement them in Python.

## Logical Operators in Python with Examples

In this section, we are going to discuss the logical operators in detail with examples for a better understanding.

### Logical AND operator

The first one is the logical AND operator, which we are going to apply between two different operands. If the condition for the operands is true, then their overall result will also be true. Otherwise, it will be considered false if any of the two conditions is false. For better understanding, we have explained the logical and operator in the form of the truth table.

 `x` `y` `x and y` `True` `True` `True` `True` `False` `False` `False` `False` `False` `False` `True` `False`

As shown in the table, the conditions x and y only return true if both conditions evaluate to be true.

Note: When using an operator, if the first expression is evaluated as false, and the subsequent expressions are not evaluated.

The implementation of the basic example in Python is shown below.

Example 1:

 ```x = 10 y = 20 x > 0 and y > 0 Output:  True```

In the above example, you can see that we have used two different variables with the names of “x” and “y” and the assigned values for both these variables are greater than 0. That’s why their overall result is also true.

Example 2:

 ```x = 10 y = 20 x > 0 and y < 0 Output:  False```

Now in this example, you can see that we have given the same values again to variables x and y, but this time we are testing if the value of the variable y is less than 0. As this condition is false, their overall result will also be false, as can be seen in the image shown above.

### Logical OR operator

The logical OR operator will also be used to compare the values of two different variables just like the AND operator, but the difference is that the overall result will be true if any or both of the conditions are true. To give you a better picture, let’s discuss this operator in more detail in the truth table as shown below.

 `x` `y` `x or y` `True` `True` `True` `True` `False` `True` `False` `True` `True` `False` `False` `False`

The OR operator returns false only when both conditions are false.

Note: When employing the OR operator, if the initial expression evaluates to true, then the subsequent expressions are not evaluated.

Now let’s discuss the first example:

Example 1:

 ```x = 10 y = 20 x > 0 or y > 0 Output:  True```

As you can see in the above image, both the variables x and y have a value greater than 0. That’s why their overall result will also be true.

Example 2:

 ```x = 10 y = 20 x < 0 or y < 0 Output:  False```

In this example, you can see that the values for the variable are greater than 0, but we are testing for values that are less than 0. That’s why their overall result is also false. Till now, it behaves just like the logical AND operator, so the clear difference can be seen in the next example mentioned below.

Example 3:

 ```x = 10 y = 20 x > 0 or y < 0 Output:  False```

Now, we are testing if the value of variable x is greater than 0, which is true, and if the value of variable y is less than 0. As you can see, one of the two conditions is true, which is why their overall result will also be true.

### Logical NOT operator

The logical not operator will take a single value as an input and will show you the opposite result of whatever was previously stored in it. For example, if the condition is true, then it will show you that the condition is false. This can also be seen in the below-mentioned truth table.

 `a` `not a` `True` `False` `False` `True`

Let’s discuss some examples for better understanding.

Example 1:

 ```x = 10 y = 20 >>> x < y True >>> not x > y False```

In the above example, you can see that the value of x is less than y so the condition is true, but after applying the logical and operator, it reverses the result, showing you the output as false.

## Conclusion

Logical operators are used to calculate the results based on logical operations. It compares the values of the two operands and will provide you with the result in the form of true or false. There are three different logical operators that we have discussed in this article, which are logical and operator, logical or operator, and logical not operator.