Python Operators
Estimated reading: 3 minutes 36 views

🧠 Python Logical Operators Explained – AND, OR, NOT with Examples

Logical operators in Python are used to combine conditional statements. They are essential in decision making, controlling the flow of programs using if, while, and other control structures.


πŸ”‘ What Are Logical Operators?

Python provides three main logical operators:

OperatorDescriptionSyntax ExampleResult Condition
andLogical ANDx > 5 and x < 10βœ… True only if both are True
orLogical ORx > 5 or x < 3βœ… True if at least one is True
notLogical NOT (Negation)not (x > 10)βœ… True if expression is False

πŸ“˜ Truth Tables

πŸ”Ή and Operator

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

βœ… Only returns True if both conditions are True.

πŸ”Ή or Operator

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

βœ… Returns True if any one condition is True.

πŸ”Ή not Operator

Anot A
TrueFalse
FalseTrue

βœ… Negates the condition (inverse of truth value).


πŸ§ͺ Examples

βœ… and Usage:

x = 8
y = 21

if (x < 10) and (y > 20):
    print("Condition met")

πŸ“˜ Explanation:

  • x < 10 β†’ True
  • y > 20 β†’ True
  • Both are True β†’ Executes the print statement.

βœ… or Usage:

age = 70

if age < 12 or age > 65:
    print("Eligible for discount")

πŸ“˜ One condition age > 65 is True β†’ Statement is executed.


βœ… not Usage:

is_logged_in = False

if not is_logged_in:
    print("Please log in")

πŸ“˜ not False β†’ True β†’ Displays message.


βš™οΈ Short-Circuit Behavior

Python evaluates expressions from left to right:

  • A or B β†’ if A is True, B is not evaluated.
  • A and B β†’ if A is False, B is not evaluated.

πŸ’‘ Useful for optimizing performance and preventing unnecessary function calls:

def check():
    print("Check called")
    return True

if False and check():
    print("This won’t print")

Output: Nothing is printed because False and stops evaluation.


⚠️ Operator Precedence

From highest to lowest:

  1. not
  2. and
  3. or

πŸ“˜ Example:

True or False and False

βœ… Evaluates as: True or (False and False) β†’ True


πŸ’‘ Pythonic Tips

  • Prefer if x: over if x != 0: when checking for truthiness.
  • Use logical chaining like 10 < x < 20 instead of x > 10 and x < 20 (Python supports this!).
  • Use not to simplify negative logic: if not x == y β†’ if x != y.

πŸ“ Summary

OperatorDescriptionTruth Behavior
andTrue if both conditions are trueStops at first False
orTrue if any condition is trueStops at first True
notInverts truth valueHighest precedence

Logical operators are powerful tools for building decision-making logic in Python. Mastering them is key to writing clear and efficient code.


❓FAQs – Logical Operators in Python

❓ What does and return in Python?

It returns the first falsy value or the last value if all are truthy. Ex: 3 and 5 β†’ 5.

❓ How is or used for setting defaults?

You can use or to set fallback values:

name = input("Name: ") or "Guest"

❓ Does not work on numbers?

Yes. not 0 β†’ True, not 5 β†’ False. Python treats 0 as False and any non-zero number as True.

❓ Can I use multiple and/or together?

Yes, use parentheses for clarity:

if (x > 5 and y < 10) or z == 0:

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Logical Operators

Or Copy Link

CONTENTS
Scroll to Top