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:
and
or
not
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)
In this example, the price < 20
returns True
, therefore, the whole expression returns True
.
The following example returns False
because both conditions evaluate to False
:
>>> price = 9.99 >>> price > 10 or price < 5 False
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:
a | not a |
True | False |
False | True |
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:
- First,
(price > 5 and price < 10)
evaluates toTrue
. - Second,
not True
evaluates toFalse
.
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 |
Leave a Reply