Logical operators in Python are used to combine conditional statements and control the flow of decision-making. They evaluate Boolean expressions (expressions that return True or False) and return a single True or False value based on the logic applied.

List of Logical Operators

Operator Description Example (a = True, b = False)
and Returns True if both conditions are True a and b → False
or Returns True if at least one condition is True a or b → True
not Reverses the result: True → False, False → True not a → False

Real-Life Analogy

Logical operators can be compared to everyday decision-making:

1. and: "I will go for a walk if it is sunny AND I am free."
Both conditions must be True to go for a walk.

2. or: "I will buy coffee if I am tired OR I like the café’s coffee."
Either condition being True is enough to buy coffee.

3. not: "I will NOT go to the party if it is raining."
Reverses the meaning of the condition.

Operator Details with Examples

1. AND Operator
The and operator returns True only if both conditions are True.

a = True
b = False
print(a and b)  # Output: False

Real-Life Example:
Checking if someone is eligible to apply for a driver's license.

age = 20
has_permit = True

if age >= 18 and has_permit:
    print("Eligible for a driver's license.")
else:
    print("Not eligible.")

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

a = True
b = False
print(a or b)  # Output: True
Real-Life Example: Deciding to watch a movie.
weekend = False
finished_work = True

if weekend or finished_work:
    print("Let's watch a movie.")
else:
    print("Not now.")

3. NOT Operator
The not operator reverses the result of a Boolean expression.

a = True
print(not a)  # Output: False
Real-Life Example: Deciding whether to go outside.
is_raining = True

if not is_raining:
    print("Go outside.")
else:
    print("Stay indoors.")

Combining Logical Operators

Logical operators can be combined to evaluate complex conditions.

Example 1: Loan Approval

A loan is approved if:
1. The applicant has a good credit score (credit_score >= 700).
2. Their income is high enough (income > 50000).
3. They are not blacklisted (not blacklisted).

credit_score = 750
income = 60000
blacklisted = False

if credit_score >= 700 and income > 50000 and not blacklisted:
    print("Loan Approved.")
else:
    print("Loan Denied.")

Example 2: Fitness Goals

A fitness tracker suggests exercise if:
• The user didn’t meet their step goal (steps < goal) or
• They ate more calories than recommended.

steps = 8000
goal = 10000
calories_consumed = 2500
calories_limit = 2000

if steps < goal or calories_consumed > calories_limit:
    print("Exercise more!")
else:
    print("You're on track!")

Short-Circuit Evaluation

Logical operators in Python use short-circuit evaluation:
1. and: Stops evaluating as soon as it encounters False (because the entire expression can’t be True).
2. or: Stops evaluating as soon as it encounters True (because the entire expression is already True).

Example 3:

a = False
b = True

# `a` is False, so `a and b` stops evaluating after `a`
print(a and b)  # Output: False

# `a` is False, so `a or b` checks `b`
print(a or b)  # Output: True

Real-Life Application

Example 4: E-commerce Discount Eligibility

A customer gets a discount if they:
1. Are a member (is_member) or
2. Their cart value exceeds $100 (cart_value > 100).

is_member = True
cart_value = 80

if is_member or cart_value > 100:
    print("Discount Applied!")
else:
    print("No Discount.")

Example 5: Alarm System

An alarm should trigger if:
• Motion is detected (motion_detected) and
• It’s not during working hours (not working_hours).

motion_detected = True
working_hours = False

if motion_detected and not working_hours:
    print("Alarm Triggered!")
else:
    print("All is safe.")

Important Tips

1. Operator Precedence: Logical operators have a precedence order:
o not > and > or.
o Use parentheses to make complex expressions clear and avoid confusion.

a = True
b = False
c = True
result = a or b and c  # `b and c` is evaluated first
print(result)  # Output: True

2. Readable Conditions: Avoid overly complex conditions. Use variables or functions for clarity.

# Instead of:
if age >= 18 and has_permit and not blacklisted:
    ...

# Use:
is_eligible = age >= 18 and has_permit and not blacklisted
if is_eligible:

Key Takeaways

Logical operators (and, or, not) combine or modify conditions to form powerful decision-making logic.
Boolean values (True, False) are the building blocks for these operators.
They are heavily used in decision-making (if statements), loops, and filtering data.