LOGICAL OPERATORS IN PYTHON
Now, we are going to have a look at the logical operators, in Python. Basically, logical operators are used in some logical operations. These Python logical operators are often used with logical values, like when we deal with some conditional statements. There are some logical operators, like ‘and’, ‘or’, and ‘not’, which we are going to explore now.
Have a look at the below table, which shows the logical operators, with some descriptions, and examples.
Operator | Description | Example |
and | This operator checks if all the given conditions are True. We get True as output if all the conditions are True, and otherwise returns False. | 10<20 and 4>0 printing this will print True as both the conditions are True. (You can add more conditions separated with and operator) for example: 6 > 3 and 5 < 10 and 3>2 and 4>1 remember that we get a True output if all the expressions here evaluate to True. |
or | This operator checks if any of the conditions are True. We get True as output if any of the specified expression evaluate to True, and False otherwise. | 4>1 or 5>10 printing this will print True as one of the conditions is True. (You can add more conditions separated by or operator) 6 > 3 or 5 < 10 or 3>2 or 4>1 this will give True as an output if at least one condition is True. |
not | This operator reverses the state of the condition, i.e., we get True if condition is False and so on | not 5>3 printing this will print False since it is reversing the True state of the condition. |
As you can see, with the logical operator, for the output to be True, all the expressions should evaluate to True. On the other hand, for the logical or operator, at least one expression of all, should evaluate as True, and the output would be True. Also, the not operator is going to reverse the state of the condition, which means that it would give True if something is False. We will get these operators when we would see some examples. So, let’s have a look at a simple example, which demonstrates logical operators.
As you can see, in the above program, we are taking a number as user input. In the second line, in the print statement, we have some expressions, with an operator. The thing is that with the and operator, the output is True if all the expressions evaluate to True. On the other hand, with the or operator, at least one expression should evaluate to True, and the output will be True. Also, with the not operator, the value is reversed, which means that not True is simply False for example.
So, now let’s have a quick look at the output of the above program.
Please enter a number:26
True
True
False
As you can see, in the output, the number entered by the user is 26 for example, and the first output from an operator is True, since the number 26 is even, and also less than 100. The second output is from the or operator, and it is also True since the number 26 is divisible by 2. Also, the third output is from the not operator, and it is False because not True is simply False.
So, in this way, we can make use of logical operators. Mostly, these operators are going to be used with logical values. We would make use of the different logical operators, as and when required.