This tutorial will teach you about Python logical operators and how to use them to combine multiple conditions.
You might want to verify several conditions at once. Use logical operators to accomplish this.
The logical operators in Python are as follows:
and
or
not
and operator in Python
The and operator determines if two conditions are True at the same time:
a and b
If both conditions are true, it returns True. If either condition an or b is False, it returns False.
The and operator is used in the following example to combine two conditions that compare price with numbers:
>>> price = 9.99 >>> price > 9 and price < 10 True
Because the price is larger than 9 and less than 10, the outcome is True.
Because the price isn’t more than ten, the following example returns False:
>>> price > 10 and price < 20 False
The condition price > 10 returns False in this case, but the second condition price 20 returns True.
The and operator’s result when two conditions are combined is shown in the table below:
a | b | a and b |
True | True | True |
True | False | False |
False | False | False |
False | True | False |
The condition a and b only yields True if both conditions evaluate to True, as shown in the table.
or operator in Python
The or operator, like the and operator, examines multiple criteria. However, it returns True if one or both of the following requirements are met:
a or b
The or operator’s outcome when two conditions are combined is shown in the table below:
a | b | a or b |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Only when both requirements are False does the or operator return False.
The or operator is demonstrated in the following example:
>>> price = 9.99 >>> price > 10 or price < 20 >>> True
Because the price of 20 returns True in this case, the entire equation returns True.
Because both conditions evaluate to False, the following example returns False:
>>> price = 9.99 >>> price > 10 or price < 5 False
not operator in Python
Only one condition is affected by the not operator. It also reverses the condition’s result: True becomes False and False becomes True.
not a
The not operator returns False if the condition is True and False if the condition is False.
The not operator’s result is shown in the table below:
a not a True False False True
The not operator is used in the following example. The not price > 10 returns True because the price > 10 returns False:
>>> price = 9.99 >>> not price > 10 True
Another example combining the not and and operators is as follows:
>>> not (price > 5 and price < 10) False
Python examines the conditions in this example in the following order:
For starters, (price > 5 and price 10) equals True.
Not True, on the other hand, evaluates to False.
This leads to an essential concept known as logical operator precedence.
Logical Operator Precedence in Python
Python evaluates logical operators in the order that they appear in an expression, which is known as operator precedence.
The operators not, and, and or have the following precedence:
Operator | Precedence |
---|---|
not | High |
and | Medium |
or | Low |
Python will initially group the operands for the operator with the highest precedence, then the operands for the operator with the lowest precedence, and so on, based on these precedences.
Leave a Reply