01 / 23
and or not

Logical Operators
& Truthiness

Master Python's and, or, and not operators, and discover how Python decides what counts as True or False.

Python

What Are Logical Operators?

Logical operators let you combine or invert boolean expressions. They are the glue that holds complex conditions together: deciding whether a user is logged in and has permission, whether a number is negative or zero, or whether something is not true.

and
True only when both sides are true
or
True when at least one side is true
not
Flips True to False and vice versa

Why Logical Operators Matter

Almost every real program needs to check multiple conditions at once. Without logical operators, you'd need deeply nested if-statements for even simple checks.

# Without logical operators if age >= 13: if has_permission: print("Access granted") # With logical operators if age >= 13 and has_permission: print("Access granted")

Quick Boolean Refresher

Python has exactly two boolean values: True and False (capitalized). Comparison operators produce booleans:

>>> 5 > 3 True >>> 10 == 20 False >>> type(True) <class 'bool'>

Truth Table: and

and returns True only when both operands are True. If the first operand is False, Python never evaluates the second (short-circuit evaluation).

\(A\) \(B\) \(A \text{ and } B\)
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Truth Table: or

or returns True when at least one operand is True. If the first operand is True, Python skips evaluating the second.

\(A\) \(B\) \(A \text{ or } B\)
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Truth Table: not

not is a unary operator: it takes a single value and flips it.

\(A\) \(\text{not } A\)
TrueFalse
FalseTrue
>>> not True False >>> not (5 > 10) True

What Is Truthiness?

In Python, every value has an inherent boolean meaning: it is either truthy (acts like True) or falsy (acts like False). This isn't limited to actual booleans.

Falsy Values False None
\(0, 0.0, 0j\)
"" (empty string)
\[], (), \{\}, \text{set}()\)
Truthy Values True Any nonzero number "hello" (non-empty)
\([1, 2], (3,), \{"a": 1\}\)
Any object (usually)

Truthy vs. True: The Key Distinction

True is a specific Python value. Truthy means any value that Python treats as true in a boolean context. They overlap but are not identical.

True (the value) Truthy (the concept)
TypeboolAny type
ExamplesOnly True\(1, "hi", [1], True\)
Test\(x \text{ is } True\)\(\text{bool}(x) == True\)
UsageIdentity checkif-statement condition

Short-Circuit Evaluation

Python stops evaluating a logical expression as soon as the result is determined. This is called short-circuit evaluation and it has important consequences.

\(A \text{ and } B\)
Eval A A falsy? Return A Return B No Yes
\(A \text{ or } B\)
Eval A A truthy? Yes Return A Return B No

and / or Return Values, Not Bools

A critical Python detail: and and or don't always return True or False. They return the actual operand that determined the result.

>>> "hello" and "world" 'world' # both truthy, returns last >>> "" and "world" '' # first is falsy, returns it >>> "" or "default" 'default' # first is falsy, returns second >>> 42 or 0 42 # first is truthy, returns it

Operator Precedence

When multiple logical operators appear in one expression, Python evaluates them in a specific order. Parentheses always override precedence.

1
not: evaluated first (highest priority)
2
and: evaluated second
3
or: evaluated last (lowest priority)
# Without parentheses: not False and True or False # Step 1: (not False) and True or False → True and True or False # Step 2: (True and True) or False → True or False # Step 3: True or False → True

How to Evaluate a Logical Expression

1
Identify each operand. Determine its truthiness: is it truthy or falsy?
2
Apply precedence. Evaluate not first, then and, then or. Respect parentheses.
3
Short-circuit. For and: if the left side is falsy, return it immediately. For or: if the left side is truthy, return it immediately.
4
Return the deciding value. Remember: and/or return the actual value, not returns a bool.

Example 1: Simple Conditions

x = 15 y = 0 # What does each expression return? x > 10 and y == 0 x < 5 or y == 0 not (x == y)
1
\(x > 10 \text{ and } y == 0\): True and True → True
2
\(x < 5 \text{ or } y == 0\): False or True → True
3
\(\text{not } (x == y)\): not False → True

Example 2: Truthiness in Action

name = "" scores = [85, 92] backup = None name or "Guest" # → ? scores and scores[0] # → ? backup or scores # → ?
1
name or "Guest": "" is falsy → returns "Guest"
2
scores and scores[0]: [85, 92] is truthy → evaluates right side → returns \(85\)
3
backup or scores: None is falsy → returns [85, 92]

Example 3: Complex Chains

# What does this evaluate to? 0 or "" or [] or "found!" or 42
1
\(0\) is falsy → move to next
2
"" is falsy → move to next
3
[] is falsy → move to next
4
"found!" is truthy → short-circuit! Return "found!"

Common Mistakes

✕ Using == True
Writing \(\text{if } x == True\) instead of simply if x. This fails for truthy non-boolean values like \([1]\) or "hello".
✕ Confusing and / or
Using or when you need and. "\(x\) must be positive and even" requires and, not or.
✕ Forgetting Precedence
Writing \(\text{not } x \text{ or } y\) thinking it means \(\text{not } (x \text{ or } y)\). Without parentheses, not binds only to \(x\).

Summary Table

Operator Returns True when Actual return value Short-circuits if
and Both truthy First falsy, or last value Left is falsy
or At least one truthy First truthy, or last value Left is truthy
not Operand is falsy Always a bool N/A (unary)

Question 1 of 5

What does the following expression evaluate to?
"" or 0 or "Python"

Question 2 of 5

Which of these values is falsy in Python?

Question 3 of 5

What does not [] and "hello" evaluate to?

Question 4 of 5

In \(A \text{ and } B\), when does Python skip evaluating \(B\)?

Question 5 of 5

What is the value of \(5 \text{ and } 0 \text{ and } 3\)?