Python Logical Operators

python-logical-operators

Logical Operators in Python.

 
To perform certain logical operations or to combine conditional statements, the logical operator is used. In control statements such as if, else, and elif, primarily logical operators are used where we can verify more conditions together by using these operators alone. Real or FALSE values are often returned by logical operators.

 

 

All  logical operators in python are discussed below  in detail with their syntax and proper example:

1. And

It compares whether both the operands are logically True or not. If the values are found True then it returns TRUE else it returns FALSE.

Syntax

Logic1 and Logic2

Example

>>> a = 10
>>> b = 20
>>> a>2 and b<25
True
>>> 10 == 10 and 10<11
False
>>> a != 10 and  b==a
False

2. OR

It returns True if any of the conditions provided is True. It only returns False if all the conditions provided are False.

Syntax

Logic1 OR Logic2

Example

>>> a = 10
>>> b = 20
>>> a==10 OR b==20
True
>>> 10!=10 OR 10==10
True
>>> a!=10 OR 10<2
False

3.Not

It returns True if the condition provided is False and returns False if the condition provided is True.

Syntax

NOT Logic1 

Example

>>> a = 10
>>> b = 20
>>> NOT a==10
False
>>> NOT (10!=10 OR 10==10)
False
>>> NOT(a!=10 OR 10<2)
True