Python Logical Operators [Blog + Video]

Logical operators work on Boolean values but can be used on integers and other objects as well. Python has three logical operators: and, or, and not.

The following table provides a quick overview of Python logical operators:

OperatorDescriptionExample
andReturns True if both operands are True, and False otherwise.(True and True) == True
orReturns True if at least one of the two operands is True, and False otherwise.(False or True) == True
notReturns True if the single operand is False, and False otherwise.(not True) == False

Let’s dive into the operators one by one.

Python AND Operator

Python’s and operator performs the logical AND operation that returns True if both operands evaluate to True. The operator performs an optimization called short-circuiting, so if the first operand evaluates to True, it returns the second operand; and if the first operand evaluates to False, it returns False without further evaluating the second operand.

As you read through the article, you can also watch my video for supporting explanations:

Here’s the result of the and operator when applied to Boolean operands:

First Operand ASecond Operand BA and B
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

You can see those examples in the following script:

>>> False and False
False
>>> False and True
False
>>> True and False
False
>>> True and True
True

You can also apply the and operator to integers:

First Operand ASecond Operand BA and B
000
010
100
111

The same can be seen in the following Python script:

>>> 0 and 0
0
>>> 0 and 1
0
>>> 1 and 0
0
>>> 1 and 1
1

Python OR Operator

Python’s or operator performs the logical OR operation that returns True if at least one of the operands evaluates to True. The operator performs an optimization called short-circuiting, so if the first operand evaluates to True, it returns the first right away without further evaluating the second, and if the first operand evaluates to False, it returns the second operand.

As you read through the article, you can also watch my video for supporting explanations:

Here’s the result of the or operator when applied to Boolean operands:

First Operand ASecond Operand BA or B
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

You can see those examples in the following script:

>>> False or False
False
>>> False or True
True
>>> True or False
True
>>> True or True
False

You can also apply the or operator to integers:

First Operand ASecond Operand BA or B
000
011
101
111

The same can be seen in the following Python script:

>>> 0 or 0
0
>>> 0 or 1
0
>>> 1 or 0
0
>>> 1 or 1
1

Full Tutorial: Python OR Operator

Python NOT Operator

Python’s not operator returns True if the single operand evaluates to False, and returns False if it evaluates to True. Thus, it logically negates the implicit or explicit Boolean value of the operand.

As you read through the article, you can also watch my video for supporting explanations:

You can apply the not operator to a Boolean value and Python invert the Boolean operand. Thus, the expression not False becomes True and not True becomes False.

Operand: Anot A
FalseTrue
TrueFalse

You can see those examples in the following script:

>>> not False
True
>>> not True
False

You can apply the not operator to an integer value. Python internally converts the integer value to a Boolean value, i.e., all non-zero integers will be converted to True and integer 0 to False. The resulting Boolean is then inverted by the not operator. For example, the expression not 1 becomes False and not 0 becomes True.

Operand: Anot A
1False
99False
-99False
0True

All integers except 0 are internally converted to a True Boolean value. Thus, the integers 1, 99, and even -99 lead to the calculation of not True which evaluates to False.

You can see those examples in the following script:

>>> not 1
False
>>> not 99
False
>>> not -99
False
>>> not 0
True