Python Logical Operators

In this tutorial, we will take you through each of the logical operators that you can use in Python.

Python logical operators

In Python, a logical operator performs operations on the output of two conditional statements (Operands). The output will be either true or false.

Using logical operators allows you to perform a check on two operands. For example, you can check if both operands are true or if only one of them is true. You can create some smart logic for controlling your Python script using these operators.

It makes perfect sense to get a good understanding of all the different types of operators in Python. However, for this tutorial, we will be purely focusing on logical operators.

The table below showcases each of the logical operators you can use. It is an excellent reference if you need to remind yourself of the functionality of each operator.

NameExampleResult
ANDx and ytrue if both x and y are true
ORx or ytrue if x or y is true
NOTnot xtrue if x is false

The above table is a very simplistic explanation of each logical operator you can use in Python. We go into more detail on each operator further down this page.

This tutorial covers the (logical) Boolean operators, which are different from the bitwise operators. We also assume you are using Python 3 and not any older versions of Python where syntax and terminology may differ slightly.

Example of Logical Operators in Python

In this section, we will take you through each of the logical operators in more detail. We will show you examples of how you can use these operators in your next Python program.

We will often refer to operands in this tutorial, which are the objects, quantity, or Boolean values being evaluated by our operation. For example, the x and y in the following expression are the operands, and the “and” is the operator.

x and y

Logical and Operator in Python

The “and” operator in Python allows you to compare two operands. If both operands are True, the operator will return True. If either value is False, the operator will return False. Lastly, if both operands are False, the operator will return False.

The syntax is very straightforward, with the operator “and” placed between two different operands.

x and y

By using the “and” operator, the result will only be true when x and y both are true.

The table below shows the different outcomes of using the “and” operator with different values for x and y.

xyx and y
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

When evaluating the “and” operator, Python will check the left operand before it checks the right operand. There is also precedence between the different types of logical operators.

Code Example of the and Operator

This section will demonstrate how the “and” operator works in Python. We use the print function to output the result of each of our operations.

  • Our first print function will return True as both operands are True.
  • The second print will return False as only one operand is True and the other is False. The “and” operator requires both operands to be True.
  • Lastly, the third print will return false as both operands are False.
print(True and True)

print(True and False)

print(False and False)

The output below shows the results of each of our “and” operator examples.

True
False
False

Logical or Operator in Python

You can use the “or” operator in Python to compare two operands to see if either is True. The operator will return True if at least one operand is True.

The syntax is very straightforward, with the operator “or” placed between two different operands.

x or y

The table below demonstrates the different outcomes of using the “or” operator to compare two operands. The only time that or returns False is when both x and y are False. Every other combination will result in a True result from the “or” operator.

xyx or y
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

When evaluating the “or” operator, Python will check the left operand before it checks the right operand. There is also precedence between the different types of logical operators.

Code Example of the or Operator

In the example below, we have some Python code demonstrating how the “or” operator functions. We will go through each of the examples and the result they will return.

  • The first print will return True as both of our operands are True.
  • Our second print will return True as one of our operands is True. The “or” operator allows one of the operands to be False.
  • Lastly, our third print will return False as both of our operands are False.
print(True or True)

print(True or False)

print(False or False)

If you run the above Python script, you should get an output similar to the one below.

True
True
False

Logical not Operator in Python

The “not” operator allows you to flip the result of an operand. So, if an operand returns True, the “not” operator will change the result to False.

You can only use the “not” operator on a single operand, so you will need to use it multiple times if you need to apply it on multiple operands.

The syntax of the “not” operator is straightforward within Python as it is written simply as “not“. Below is an example of how you can use the operator.

not x

The table below demonstrates how the “not” operator will handle an operand that is true or false.

xnot x
falsetrue
truefalse

Code Example of the not Operator

In the example below, we have two examples of how the not operator will function with an operand.

  • The first print will simply return the inverse of x, which will be False.
  • Our second example contains the “and” operator that normally return False because y is false. However, by using the “not” operator on y, we get the inverse, so y is True; thus, our “and” operator will also return True.
x = True

print(not x)

x = True
y = False

print(x and not y)

Running the above code will result in the following output into the terminal.

False
True

Logical Operator Precedence

Python will evaluate code from left to right and applies the same for grouping operators. However, logical operators have a precedence ordering, so one operator may take precedence over another operator. It is important to understand the precedence as it may impact the outcome of your conditional statements.

Our logical operators are ranked in the following order.

  1. not
  2. and
  3. or

In our example below, we show a few examples of how the logical operator precedence might impact the outcome of your code.

  • Our first print statement contains a not operator. The “not” operator is evaluated first, so our output will be True. The expression is evaluated as(True and (not False))
  • The second print statement contains both an “and” operator and “or” operator. Since “and” is higher up in precedence, it will be evaluated first, resulting in an output of True. The expression is evaluated as ((False and False) or True)
  • Lastly, our third print is similar to our second print, but the expression is flipped. Since the “and” is evaluated first, the “or” operator will guarantee our return value is True. The expression is evaluated as (True or (False and True)).
print(True and not False)

print(False and False or True)

print(True or False and False)

Running the Python code above will result in the output below.

True
True
True

Conclusion

I hope by now that you have a decent understanding of how Python logical operators will work with different operands. We discussed the “and“, “or“, and “not” operators alongside a few different examples. Also, we discussed the precedence of the operators and how Python will execute them.

We have plenty more Python tutorials that I highly recommend checking out if you want to learn more about Python. For example, you might find our tutorial on if else statements useful if you are new to coding in the Python programming language.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Leave a Reply

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