Python does have and and or operators — they are keywords, not symbols like && or || used in C, Java, and JavaScript. The reason is that Python's designer, Guido van Rossum, chose readability and explicitness over brevity, and and/or are plain English words that make code easier to read aloud. This article explains the history, how Python's logical operators actually work, and how they differ from bitwise operators and from other languages.
Background: Why Keywords Instead of Symbols?
Python was first released in 1991, and its design philosophy is captured in the Zen of Python (PEP 20), which includes “Readability counts.” Using and and or instead of && and || is a deliberate choice. Symbols like && are terse and can be confusing for beginners; words are self-documenting. This also avoids the ambiguity that arises in languages where & and | are used for both bitwise and logical operations depending on context.
How Python's and/or Work: Short-Circuit Evaluation
Python's and and or are short-circuit operators. They evaluate the left operand first, and only evaluate the right operand if needed. This is a common feature in many languages, but Python adds a twist: they return one of the operands, not necessarily True or False.
a and b: ifais falsy, returna; otherwise returnb.a or b: ifais truthy, returna; otherwise returnb.
This behavior is useful for setting defaults: name = user_input or "default". It also means that and/or do not always produce a boolean; they produce the last evaluated operand. If you need a strict boolean, wrap with bool().
Truthiness: What Counts as True or False?
Python treats certain values as falsy when used in a boolean context: False, None, zero (0, 0.0, 0j), empty sequences/collections ('', [], {}, set()), and objects whose __bool__ or __len__ returns False or 0. Everything else is truthy. This is why 0 or "hello" returns "hello" and [] and "x" returns [].
Difference Between and/or and Bitwise &, |
Python also has bitwise operators & and | that operate on integers (and sets). They do not short-circuit and they return integers, not operands. For example, 2 & 3 returns 2 (binary 10 & 11 = 10). Beginners sometimes mistakenly use & for logical AND, which can cause subtle bugs because it always evaluates both sides and returns a number, not a truthy operand. The same applies to | vs or.
Comparison with Other Languages
Most C-family languages (C, C++, Java, JavaScript, PHP) use && and || for logical operations. They also short-circuit, but they return a boolean (in C, an integer 0/1). Ruby and Perl use and/or as keywords but with lower precedence than &&/||. Python's choice of words is unique among mainstream languages, but it aligns with its philosophy of readability. The lack of && and || is not a missing feature; it's a different syntax for the same concept.
How to Choose Between and/or and Other Constructs
Use and/or for logical conditions. For conditional assignment, consider a ternary expression x if condition else y which is often clearer than a or b when you want to return a specific value. For multiple conditions, use any() and all() which are more readable for lists. For example, if all(x > 0 for x in values): is clearer than chaining and.
Common Pitfalls and Misconceptions
- Misconception:
and/oralways return boolean – false; they return operands. - Pitfall: Using
&instead ofand– leads to wrong results and no short-circuit. - Pitfall: Forgetting that
orreturns the first truthy value, so0 or FalsereturnsFalse, not0. - Pitfall: Chaining
and/orcan be hard to read; use parentheses for clarity.
Frequently Asked Questions
Why doesn't Python use && and ||?
Python uses the words and and or to improve readability. The symbols && and || are not part of the language because the design philosophy prioritizes explicit, readable code over terse symbols.
Can I use & and | for logical operations?
Technically you can on booleans (True & False works), but it's not recommended because it doesn't short-circuit and can behave unexpectedly with non-boolean values. Always use and/or for logic.
What does and return if both operands are truthy?
It returns the second operand. For example, "a" and "b" returns "b". This is because the first is truthy, so it evaluates the second and returns it.
Why does 0 or "default" return "default"?
Because 0 is falsy, so or evaluates the right side and returns it. This is a common idiom for default values.
Is there any performance difference between and/or and bitwise operators?
Bitwise operators are slightly faster because they don't short-circuit, but the difference is negligible in most applications. Readability should guide your choice, not micro-optimization.