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.
Here at Ibmi Media, we shall look into the logical operator's execution and how you can implement them in Python.
1. Logical AND operator
Here, we are going to apply the logical AND operator 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.
Note: When using an operator, if the first expression is evaluated as false, and the subsequent expressions are not evaluated.
i. Have a look at an example:
x = 10
y = 20
x > 0 and y > 0
Output:
True
Here, you can see that we have used two different variables with the names of "x" and " and the assigned values for both these variables are greater than 0. That's why their overall result is also true.
Hence, the conditions x and y only return true if both conditions evaluate to be true.
ii. Another example is given below:
x = 10
y = 20
x > 0 and y < 0
Output:
False
Here, 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
2. 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.
i. Have a look at the below example:
x = 10
y = 20
x > 0 or y > 0
Output:
True
Here, both the variables x and y have a value greater than 0. That’s why their overall result will also be true.
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.
ii. Another example is:
x = 10
y = 20
x < 0 or y < 0
Output:
False
Here, 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.
iii. Another example is given below:
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.
3. 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.
i. Have a look at the below example:
x = 10
y = 20
>>> x < y
True
>>> not x > y
False
Here, 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.
This article covers different logical operators which are logical and operator, logical or operator, and logical not operator. In fact, 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.