- Python Logical Operators
- Introduction to Python logical operators
- The and operator
- The or operator
- The not operator
- Precedence of Logical Operators
- Summary
- What are Logical Operators in Python?
- And Operator
- Explanation:
- Or Operator
- Explanation:
- Not Operator
- Explanation:
- Order of Evaluation of Logical Operators
- Learn More
- Conclusion
Python Logical Operators
Summary: in this tutorial, you’ll learn about Python logical operators and how to use them to combine multiple conditions.
Introduction to Python logical operators
Sometimes, you may want to check multiple conditions at the same time. To do so, you use logical operators.
Python has three logical operators:
The and operator
The and operator checks whether two conditions are both True simultaneously:
a and b
Code language: Python (python)
It returns True if both conditions are True . And it returns False if either the condition a or b is False .
The following example uses the and operator to combine two conditions that compare the price with numbers:
>>> price = 9.99 >>> price > 9 and price < 10 True
Code language: Python (python)
The result is True because the price is greater than 9 and less than 10.
The following example returns False because the price isn’t greater than 10:
>>> price > 10 and price < 20 False
Code language: Python (python)
In this example, the condition price > 10 returns False while the second condition price < 20 returns True .
The following table illustrates the result of the and operator when combining two conditions:
a | b | a and b |
True | True | True |
True | False | False |
False | False | False |
False | True | False |
As you can see from the table, the condition a and b only returns True if both conditions evaluate to True .
The or operator
Similar to the and operator, the or operator checks multiple conditions. But it returns True when either or both individual conditions are True :
a or b
Code language: Python (python)
The following table illustrates the result of the or operator when combining two conditions:
a | b | a or b |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
The or operator returns False only when both conditions are False .
The following example shows how to use the or operator:
>>> price = 9.99 >>> price > 10 or price < 20 >>> True
Code language: Python (python)
The following example returns False because both conditions evaluate to False :
>>> price = 9.99 >>> price > 10 or price < 5 False
Code language: Python (python)
The not operator
The not operator applies to one condition. And it reverses the result of that condition, True becomes False and False becomes True .
not a
Code language: Python (python)
If the condition is True , the not operator returns False and vice versa.
The following table illustrates the result of the not operator:
The following example uses the not operator. Since the price > 10 returns False , the not price > 10 returns True :
>>> price = 9.99 >>> not price > 10 True
Code language: Python (python)
Here is another example that combines the not and the and operators:
>>> not (price > 5 and price < 10) False
Code language: Python (python)
In this example, Python evaluates the conditions based on the following order:
This leads to an important concept called precedence of logical operators.
Precedence of Logical Operators
When you mix the logical operators in an expression, Python will evaluate them in the order which is called the operator precedence.
The following shows the precedence of the not , and , and or operators:
Operator | Precedence |
---|---|
not | High |
and | Medium |
or | Low |
Based on these precedences, Python will group the operands for the operator with the highest precedence first, then group the operands for the operator with the lower precedence, and so on.
In case an expression has several logical operators with the same precedence, Python will evaluate them from the left to right:
a or b and c | means | a or (b and c) |
a and b or c and d | means | (a and b) or (c and d) |
a and b and c or d | means | ((a and b) and c) or d |
not a and b or c | means | ((not a) and b) or c |
Summary
- Use logical operators to combine multiple conditions.
- Python has three logical operators: and , or , and not .
- The precedence of the logical operator from the highest to lowest: not , and , and or .
What are Logical Operators in Python?
In Python, logical operators carry out logical operations and return Boolean values based on the result. We need to verify multiple conditions simultaneously or use logical operators by combining multiple conditions in some cases. There are three types of logical operators in Python:
Let’s discuss all of these logical operators one by one in detail.
And Operator
The And operator is used to verify where both conditions associated with it are True Simultaneously. It is represented as «X and Y«.
The conditions that can occur are:
- If Both are True, then the result is True.
- If one is True and the other is False, the result is False.
- If both are False, the result is False.
Sample Example:
Explanation:
The above example verifies whether both conditions are true or not. In the first case, they are true as 7 > 5 and 7 < 10, so it returns true, but in the second case, 7 < 5 is false, so it returns false.
Note: And Operator returns True if both conditions are true; else, it returns False.
Or Operator
The Or Operator is used to verify that either of the associated conditions is true. It is represented as «X or Y«.
The conditions that can occur are:
- If Both are True, the result is True
- If One is True and the other is False, the result is True
- If both are False, the result is False
Sample Example:
Explanation:
Note: Alternatively, the OR operator can return true . If either of the conditions is true, it returns True; otherwise, it returns False.
Not Operator
The Not Operator is associated with a single condition. It inverts the results, i.e., true is changed to false, and false is changed to true. It is represented as «not X«.
The conditions that occur are:
- If the condition is True, the result is False
- If the condition is False, the result is True
Sample Example:
Explanation:
In the above example, it is visible that the Not Logical Operator changes true to False and Changed False to true in Second Case.
Note: The Not Operator inverts the result. It changes True to False and vice versa.
Order of Evaluation of Logical Operators
As there are three logical operators, we need to evaluate any expression to evaluate these logical operators. The precedence of logical operators in Python is:
The order of evaluation of logical operators in Python is as follows:
- Not operator is at the highest precedence.
- And operator is at medium precedence.
- Or operator has the least precedence.
Sample Example:
- X or y and z: This will be converted into (x or (y and z)) as the OR operator has lower precedence than the AND operator.
- not x and y or z: This will be converted into (((not x) and y) or z), as NOT has the highest precedence so that it will be evaluated first and then AND operator and at last OR operator because of the least precedence.
Learn More
Learn more about operators in Python:
Conclusion
It’s time to conclude our topic with the most important points we have discussed. So let’s see them one by one.
- We can use logical operators to obtain results simultaneously in the case of various conditions.
- In Python, there are three types of logical operators: and, or, and not.
- And Operator returns true only when both conditions are true simultaneously.
- The Or Operator returns True if either of the conditions is met.
- Not Operator inverts the result, i.e., True changes to False and Vice versa.
- The precedence of logical operators in Python is: Not < And < Or.