Skip to content

Logical Operators

What are Logical operators

Logical operators are operators that combines at least two expressions which returns either True or False when evaluated.

Logical Operators in Python

There are three logical operators in Python

and Logical operator

and Logical operator will return True if all the expressions evaluate to True

xyx and y
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

Example

>>>
>>> 10 > 9 and 9 < 10
True
>>>
>>> 20 > 10 and 10 < 20 and 10 > 9
True
>>>
>>> 2 > 4 and 4 < 6 and 6 < 8
False
>>>

or Logical operator

or Logical operator will return True if at least one of the expression evaluates to True, else it will return False

xyx or y
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

Example

>>>
>>> # Will return True only if at least one of the two is True: 10 > 8
>>> 10 > 8 or 8 < 10
True
>>>
>>> # will return False because all are False
>>> 5 > 8 and 5 < 4
False
>>>

not Logical operator

not Logical operator returns the oppsite boolean value of its operand. If the operand value is True the it will return False, else it will return True

xnot x
FalseTrue
TrueFalse

Example

>>> x = True
>>> y = False
>>>
>>> #
>>> not x
False
>>>
>>> #
>>> not y
True
>>>