Logical Operators

Logical operators in Python are used to combine, modify, or reverse Boolean expressions. They play a crucial role in decision-making, condition checks, loops, validations, and control flow.
Python has three logical operators:
  • and
  • or
  • not
This topic goes from basics to deep, tricky behavior that many learners miss.

What Are Logical Operators?

Logical operators work with Boolean values (True / False) or expressions that evaluate to Boolean values.
python
x = True
y = False

Logical AND (and)

The and operator returns True only if both conditions are True.

Truth Table

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Basic Examples

python
print(True and True)
python
print(True and False)
python
print(10 > 5 and 5 > 2)

Logical OR (or)

The or operator returns True if at least one condition is True.

Truth Table

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Basic Examples

python
print(True or False)
python
print(False or False)
python
print(5 > 10 or 10 > 5)

Logical NOT (not)

The not operator reverses the Boolean value.
python
print(not True)
python
print(not False)
python
print(not (10 > 5))

Logical Operators with Variables

python
age = 25
print(age > 18 and age < 60)
python
is_admin = False
is_logged_in = True
print(is_admin or is_logged_in)
python
is_active = True
print(not is_active)

Truthy and Falsy Values (Very Important)

Logical operators do not work only with True/False. They work with any value that can be evaluated as Boolean.

Falsy Values in Python

  • False
  • 0
  • 0.0
  • ""
  • []
  • {}
  • ()
  • None
Everything else is Truthy.
python
print(bool(""))
python
print(bool(" "))
python
print(bool([]))

Logical Operators with Non-Boolean Values (Deep Concept)

Logical operators return actual values, not always True or False.

and Operator Behavior

python
print(10 and 20)
python
print(0 and 20)
python
print("Python" and "Java")
Rule: and returns the first falsy value, otherwise the last value.

or Operator Behavior

python
print(10 or 20)
python
print(0 or 20)
python
print("" or "Python")
Rule: or returns the first truthy value.

Short-Circuit Evaluation (Advanced but Critical)

Python stops evaluating as soon as the result is known.

AND Short-Circuit

python
print(False and (10 / 0))
The division is never executed.

OR Short-Circuit

python
print(True or (10 / 0))
Again, division is skipped.

Logical Operators in if Conditions

python
age = 30
salary = 50000

if age > 25 and salary > 30000:
    print("Eligible")
python
role = "user"

if role == "admin" or role == "moderator":
    print("Access granted")

Chained Logical Expressions

python
x = 10

if x > 5 and x < 20 and x != 15:
    print("Condition met")

Common Logical Mistakes

Mistake 1: Incorrect OR Usage

python
if role == "admin" or "user":
    print("Allowed")
This is always True
Correct version:
python
if role == "admin" or role == "user":
    print("Allowed")

Mistake 2: Overusing not

python
if not x == 10:
    print("Not equal")
Better:
python
if x != 10:
    print("Not equal")

Logical Operators vs Bitwise Operators (Often Confused)

python
print(True and False)
python
print(True & False)
Logical operators work on truth values, bitwise operators work on binary representation.

Logical Operators with Functions

python
def is_valid(x):
    return x > 0

print(is_valid(5) and is_valid(10))
python
print(is_valid(-1) or is_valid(5))

Tricky Logical Expressions (Practice)

python
print(10 and 0 or 5)
python
print(not 0 and 1)
python
print("" or [] or "Python")
python
print(5 > 3 and 3 > 10 or 2 > 1)

Summary

  • Logical operators combine Boolean expressions
  • Python supports and, or, not
  • They work with truthy and falsy values
  • Operators return actual values, not always True/False
  • Short-circuit evaluation improves performance
  • Common mistakes come from incorrect OR usage

Practice (Deep Thinking)

  • Write a condition that checks a number is between 10 and 50
  • Predict output of mixed and / or expressions
  • Explain short-circuit behavior with an example
  • Fix a buggy logical condition using correct operators