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:

  • and
  • or
  • not

The and operator

The and operator checks whether two conditions are both True simultaneously:

a and bCode 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 TrueCode 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 FalseCode 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:

aba and b
TrueTrueTrue
TrueFalseFalse
FalseFalseFalse
FalseTrueFalse

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 bCode language: Python (python)

The following table illustrates the result of the or operator when combining two conditions:

aba or b
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

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 >>> TrueCode 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 aCode 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:

anot a
TrueFalse
FalseTrue

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 TrueCode language: Python (python)

Here is another example that combines the not and the and operators:

>>> not (price > 5 and price < 10) FalseCode language: Python (python)

In this example, Python evaluates the conditions based on the following order:

  • First, (price > 5 and price < 10) evaluates to True.
  • Second, not True evaluates to False.

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 notand, and or operators:

OperatorPrecedence
notHigh
andMedium
orLow

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 cmeansa or (b and c)
a and b or c and dmeans(a and b) or (c and d)
a and b and c or dmeans((a and b) and c) or d
not a and b or cmeans((not a) and b) or c

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *