Web Analytics

Logical Operators

Beginner ~20 min read

Logical operators let you combine multiple conditions to create complex decision logic. Python uses the keywords and, or, and not - making your code read almost like English!

The Three Logical Operators

Python has three logical operators for combining boolean expressions.

Operator Description Example Result
andTrue if BOTH are trueTrue and TrueTrue
orTrue if AT LEAST ONE is trueTrue or FalseTrue
notInverts the valuenot FalseTrue
Output
Click Run to execute your code
Key Point: Python uses words (and, or, not) instead of symbols (&&, ||, !) like other languages. This makes code more readable!

Truth Tables

Understanding truth tables helps you predict the outcome of logical operations.

A B A and B A or B not A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
Output
Click Run to execute your code
Memory Tip: Think of and as "both must be true" and or as "at least one must be true". For not, just flip the value!

Short-Circuit Evaluation

Python uses "short-circuit" evaluation - it stops evaluating as soon as it knows the result. This can improve performance and prevent errors!

Output
Click Run to execute your code
Caution: Because of short-circuit evaluation, side effects in the second operand may not happen! Don't rely on the second expression always being evaluated.

Practical Examples

Logical operators are essential for real-world conditions like access control, validation, and filtering.

Output
Click Run to execute your code
Best Practice: Use parentheses to make complex conditions clearer, even if they're not strictly required: (a and b) or (c and d)

Common Mistakes

1. Using && and || instead of and/or

# Wrong - these are not valid Python!
if x > 0 && x < 10:  # SyntaxError!
if a || b:           # SyntaxError!

# Correct - use words
if x > 0 and x < 10:
if a or b:

2. Confusing 'and' with 'or' in conditions

# Wrong - impossible condition! (nothing is both < 0 AND > 100)
if score < 0 and score > 100:
    print("Invalid")  # Never executes!

# Correct - use 'or' for "either/or" conditions
if score < 0 or score > 100:
    print("Invalid")  # Catches both cases

3. Forgetting operator precedence

# Confusing - what does this mean?
if a or b and c:
    pass
# This is actually: a or (b and c)
# 'and' has higher precedence than 'or'!

# Clear - use parentheses
if (a or b) and c:
    pass

4. Double negatives

# Confusing - hard to read!
if not is_invalid:
    print("Valid")

# Clearer - use positive naming
if is_valid:
    print("Valid")

Exercise: Access Control System

Task: Build access control logic using logical operators.

Requirements:

  • Admin access: Must be logged in AND be an admin
  • Content access: Logged in AND (is subscriber OR has free trial)
  • Blocked user: NOT banned
  • Premium feature: (Admin OR premium user) AND not in maintenance mode
Output
Click Run to execute your code
Show Solution
# Given values
is_logged_in = True
is_admin = False
is_subscriber = True
has_free_trial = False
is_banned = False
is_premium = True
maintenance_mode = False

# 1. Admin access: logged in AND admin
can_access_admin = is_logged_in and is_admin
print("Admin access:", can_access_admin)  # False

# 2. Content access: logged in AND (subscriber OR free trial)
can_access_content = is_logged_in and (is_subscriber or has_free_trial)
print("Content access:", can_access_content)  # True

# 3. Not blocked: NOT banned
is_allowed = not is_banned
print("Is allowed:", is_allowed)  # True

# 4. Premium feature: (admin OR premium) AND not maintenance
can_use_premium = (is_admin or is_premium) and not maintenance_mode
print("Premium access:", can_use_premium)  # True

Summary

  • and: True only if BOTH operands are True
  • or: True if AT LEAST ONE operand is True
  • not: Inverts True to False and vice versa
  • Short-circuit: Python stops evaluating when result is known
  • Precedence: not > and > or
  • Readability: Use parentheses for complex conditions

What's Next?

Now that you can combine conditions, let's learn about assignment operators - shortcuts for updating variable values with operations like +=, -=, and more.