In Python, and returns a true result only when every operand is truthy, while or returns a true result when at least one operand is truthy. Both operators short-circuit, and both return one of their operands rather than always returning True or False. and returns the first falsy operand (or the last one), and or returns the first truthy operand (or the last one).
Knowing that return rule explains idioms such as name = user_input or "guest" and guards such as items and items[0].
and and or combine conditions. They are keywords, not symbols, and they are different from the bitwise operators & and |, which work on integers bit by bit.
Truth table:
| a | b | a and b | a or b |
|---|---|---|---|
True | True | True | True |
True | False | False | True |
False | True | False | True |
False | False | False | False |
Python evaluates operands left to right and stops as soon as the result is known.
a and b evaluates b only if a is truthy.a or b evaluates b only if a is falsy.This is useful for safe guards. The second operand is never touched when the first one already decides the outcome.
Unlike many languages, Python's and and or return the deciding operand itself.
The values treated as falsy are False, None, zero of any numeric type, and empty containers or strings ("", [], {}, set(), (), range(0)). Everything else is truthy, including custom objects unless they define __bool__ or __len__.
A common idiom is value = config.get("port") or 8080. Be careful: it also replaces a legitimate 0 or empty string. When those are valid values, test explicitly:
not binds tighter than and, and and binds tighter than or. Use parentheses to make intent obvious.
Prefer a short helper variable or parentheses over a long mixed expression. Python also supports chained comparisons, so 5 < x < 15 is clearer than x > 5 and x < 15.
For many conditions, the built-ins all() and any() act like and and or across an iterable and also short-circuit.
Note that the list above computes every check first. Use a generator expression, such as all(f(i) for i in items), when you want the evaluation to stop early.
A few idioms use these operators daily.
In valid_age, the isinstance check runs first, so a string such as "abc" never reaches the numeric comparison and no TypeError is raised. Order your operands from cheapest and safest to most expensive or risky.
No. and is the logical operator with short-circuiting and operand return values. & is the bitwise operator and always evaluates both sides, so it cannot guard against errors.
Not necessarily. It returns the first truthy operand, or the last operand if none is truthy. Wrap with bool() if you need a strict boolean.
and has higher precedence than or, so a or b and c means a or (b and c).
No. Python uses the words and, or and not. The symbols && and || are a syntax error.
and needs all operands to be truthy and or needs at least one. Both short-circuit and return an operand instead of a plain boolean, which enables guards and default values but can surprise you with 0 or empty strings. Use parentheses for mixed expressions and all() or any() for longer lists of conditions.