Python | Logical Operators

Some of the most commonly used logical operators are: and, or, and not.

Let’s consider for the all examples here that a = 5 and b = 3.

The and operator returns True if all conditions are true. Otherwise it returns False.

>>> ( a == 4 ) and ( b == 2 )
False

To make it true we need to have a == 5 and b == 3.

>>> (a == 5) and (b == 3)
True

The or operator returns True if at least one of the conditions is true. It returns also True if all conditions are true. Otherwise it returns False.

>>> (a == 6) or (b == 4)
True
>>># Both conditions are false
>>>
>>> (a == 5) or (b == 4)
True
>>># One condition is true
>>>
>>> (a == 5) or (b == 3)
True
>>># Both conditions are true

The not operator negates the result of a condition. If the result was True, now it is False.

>>> not(a == 5)
False

Leave a Reply