Logical Operators
& Truthiness
Master Python's and, or, and not operators, and discover how Python decides what counts as True or False.
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.
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.
Quick Boolean Refresher
Python has exactly two boolean values: True and False (capitalized). Comparison operators produce booleans:
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\) |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
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\) |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Truth Table: not
not is a unary operator: it takes a single value and flips it.
| \(A\) | \(\text{not } A\) |
|---|---|
| True | False |
| False | 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.
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) | |
|---|---|---|
| Type | bool | Any type |
| Examples | Only True | \(1, "hi", [1], True\) |
| Test | \(x \text{ is } True\) | \(\text{bool}(x) == True\) |
| Usage | Identity check | if-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.
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.
Operator Precedence
When multiple logical operators appear in one expression, Python evaluates them in a specific order. Parentheses always override precedence.
How to Evaluate a Logical Expression
Example 1: Simple Conditions
Example 2: Truthiness in Action
Example 3: Complex Chains
Common Mistakes
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\)?