Python Logical Operators

Python Operators are symbols/words that tell the Python Interpreter to perform or execute certain manipulation tasks. The logical operators are used to combine multiple boolean statements.

There are three logical operators in Python.

  1. and
  2. or
  3. not
Python Logical Operators
Python Logical Operators

Python Logical Operators Flowchart

The below image depicts the flowchart of the logical operators.

Python Logical Operators Flowchart
Python Logical Operators Flowchart

Logical AND Operator in Python

The statement returns to be true when both the statements/conditions are true.

Example:

a=20
b=10
if a > 0 and b > 0
  print("Both a and b are positive numbers.")

Output: Both a and b are positive numbers.


Logical OR Operator in Python

The statement returns to be true when either of the statements/conditions is true.

Example:

a=20
b= -10
if a > 0 or b > 0
  print("True")

Output: True


Logical NOT Operator in Python

This operator works with a single value. It reverses the result i.e. if the statement is true, the not operator will turn the statement to false and vice-versa.

Example:

a=50
print(not(a % 4 or a > 0))  

Output: False

In the above example, the condition a % 4 or a > 0 evaluates to True. But, as we have used a not statement, it reverses the result i.e. it provided the result as False.