Python Basics — Logical Operators

3 minute read

Logical Operators

Logical operators combine multiple conditions and return a boolean result. Python has three logical operators: and, or, and not.

Operator Reference

Operator Meaning Example Result
and Both must be True True and False False
or At least one must be True True or False True
not Reverses the value not True False

The and Operator

Returns True only if both conditions are True:

age = 25
has_license = True

print(age >= 18 and has_license)    # True
print(age >= 18 and not has_license) # False (when has_license is True)
# All combinations
print(True and True)    # True
print(True and False)   # False
print(False and True)   # False
print(False and False)  # False

The or Operator

Returns True if at least one condition is True:

is_weekend = True
is_holiday = False

print(is_weekend or is_holiday)  # True
# All combinations
print(True or True)    # True
print(True or False)   # True
print(False or True)   # True
print(False or False)  # False

The not Operator

Reverses a boolean value:

is_raining = False
print(not is_raining)  # True

logged_in = True
print(not logged_in)   # False

Combining Logical Operators

age = 25
income = 50000
has_good_credit = True

# Complex condition
eligible = age >= 21 and income >= 30000 and has_good_credit
print(eligible)  # True

# Using or
is_student = False
is_senior = True
gets_discount = is_student or is_senior
print(gets_discount)  # True

Operator Precedence

When combining operators, Python evaluates in this order:

  1. not (highest priority)
  2. and
  3. or (lowest priority)
# not is evaluated first
print(not True or False)       # False (same as: (not True) or False)
print(not (True or False))     # False (parentheses change meaning)

# and is evaluated before or
print(True or False and False)  # True (same as: True or (False and False))
print((True or False) and False) # False (parentheses change meaning)

Tip: Use parentheses to make your intent clear, even when not strictly necessary.

Short-Circuit Evaluation

Python uses short-circuit evaluation — it stops evaluating as soon as the result is determined.

With and:

If the first condition is False, Python doesn’t check the second (because the result is already False):

x = 0
# Python never evaluates 10/x because x == 0 is False for the "and"
# Actually: x != 0 is False, so short-circuits
print(x != 0 and 10 / x > 2)  # False (10/x is never evaluated)

With or:

If the first condition is True, Python doesn’t check the second (because the result is already True):

name = "Alice"
print(name or "Unknown")  # "Alice" (second value never checked)

name = ""
print(name or "Unknown")  # "Unknown" (name is falsy, so checks second)

Truthiness in Logical Expressions

and and or don’t always return True/False — they return the actual value that determined the result:

and returns:

  • The first falsy value, or the last value if all are truthy
print(1 and 2 and 3)      # 3 (all truthy, returns last)
print(1 and 0 and 3)      # 0 (first falsy value)
print("" and "hello")     # "" (first falsy value)
print("hello" and "world") # "world" (all truthy, returns last)

or returns:

  • The first truthy value, or the last value if all are falsy
print(0 or "" or "hello")  # "hello" (first truthy value)
print(0 or "" or [])       # [] (all falsy, returns last)
print("hi" or "world")     # "hi" (first truthy value)

Common Patterns

# Default value pattern (using or)
user_name = ""
display_name = user_name or "Anonymous"
print(display_name)  # "Anonymous"

# Guard pattern (using and)
data = {"name": "Alice"}
name = "name" in data and data["name"]
print(name)  # "Alice"

# Multiple condition check
def is_valid_age(age):
    return isinstance(age, int) and 0 < age < 150

Common Mistakes

Mistake Example Fix
Using & instead of and True & False Use and for logical ops
Precedence confusion not x or y Use not (x or y) if intended
Expecting True/False "" or "default" → "default" Logical ops return actual values

Summary

  • and requires all conditions to be True
  • or requires at least one condition to be True
  • not reverses a boolean value
  • Precedence: not > and > or — use parentheses for clarity
  • Short-circuit evaluation skips unnecessary checks
  • and/or return actual values, not always True/False
  • Use or for default values: name = user_input or "Anonymous"

Next up: Assignment & Bitwise Operators →

Updated: