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

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.

Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 211
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy