🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Logical Operators: How to Use and, or, not

Python conditions can combine checks when a decision depends on more than one result. Logical operators combine conditions or reverse a condition’s truth value.
This article shows how to combine conditions and why an and/or expression may return an operand instead of True or False.
TL;DR
Python tests logical operands by truth value, but and/or may return an operand instead of True or False.
- Use and when every condition must pass.
- Use or when one condition is enough, and not to reverse a truth value.
- Use parentheses to show grouping, and write and rather than &&.
What Are Python Logical Operators?
Python logical operators combine conditions or reverse one condition. The table gives their effect when both inputs are Boolean values.
| Operator | Role | Result |
|---|---|---|
| and | Requires every condition to pass | Returns its first falsey operand, or its last operand if earlier ones are truthy |
| or | Accepts any true condition | Returns its first truthy operand, or its last operand if none is truthy |
| not | Reverses a condition | Always returns True or False |
| Falsey category | Examples |
|---|---|
| Constants | False, None |
| Numeric zero | 0, 0.0 |
| Empty containers | “”, [], {}, set() |
| Custom objects | __bool__() returns False, or __len__() returns 0 |
Boolean inputs produce these pairwise outcomes.
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
Use bool(expression) when a stored value must be exactly True or False. An if statement can test a condition directly, so convert only when another part of the program needs a normalized flag.
How to Use Python Logical Operators Step by Step
Build a condition from checks that describe the decision, then test how Python evaluates it. These steps use a small entry check and show where short-circuiting and precedence affect the result.
Step 1: Join conditions with and, or, and not
Each comparison produces a Boolean value that can be combined with a logical operator. I ran python3.14 logical_operators_demo.py for an eligible account and got the results below.
has_account = True
has_ticket = True
is_suspended = False
can_enter = has_account and has_ticket and not is_suspended
is_weekend = False or True
print(f"Can enter: {can_enter}")
print(f"Is weekend: {is_weekend}")
print(f"Not suspended: {not is_suspended}")
Can enter: True
Is weekend: True
Not suspended: True

The standalone or line returns True because its right operand is true, while not reverses the suspension value.
Step 2: Put a guard before work that could fail
I ran python3.14 short_circuit_demo.py with value set to zero, and it printed 0 without raising ZeroDivisionError.
value = 0
print("0 and 10 / 0:", value and 10 / value)
print("0 or 42:", value or 42)
print("not []:", not [])
0 and 10 / 0: 0
0 or 42: 42
not []: True

The fallback line returns 42 because or treats zero as missing. Test whether a value is None instead when zero is valid data.
Step 3: Use parentheses to make precedence visible
Comparison operators bind above logical ones. Within Boolean logic, Python applies negation before conjunction and conjunction before disjunction.
result = True or False and False
grouped = (True or False) and False
print(f"Without parentheses: {result}")
print(f"With parentheses: {grouped}")
Without parentheses: True
With parentheses: False

Comparisons can be chained, as in 18 <= age < 65. Python evaluates the middle value once, which avoids repeating the variable.
Python Logical Operator Edge Cases
Python condition errors often come from an unsupported token or an incomplete comparison.
Python rejects &&
I ran python3.14 wrong_logical_operator.py and got a syntax error at the ampersands. Python’s keyword is and.
print(True && False)
File "/home/ubuntu/.cache/journaldev-article-runs/askpython-python-logical-operators-20261115T202026Z-2a20bdce1212/wrong_logical_operator.py", line 1
print(True && False)
^
SyntaxError: invalid syntax

Repeat the comparison on both sides of or. The table also separates a falsey fallback from a bitwise operation.
| Expression | Risk | Use instead |
|---|---|---|
| unit == “L” or “l” | The non-empty string l is true by itself, so the condition passes regardless of unit. | unit == “L” or unit == “l” |
| value or default | Zero is treated as missing and replaced. | value if value is not None else default |
| a & b / a | b | These are bitwise operators, not general condition joiners. | Use and to require both, or to accept either. |
Conclusion
Name each comparison for the fact it tests. Small examples make each expression’s result straightforward to verify before using a condition in application code.
For language details, see the Python Boolean operations documentation and the operator precedence reference. AskPython also explains Python operators and comparison operators.
FAQ
These short answers cover the results and evaluation order that tend to surprise new Python readers.
Do and and or always return True or False?
No. They return the operand that determines the result. Use bool(expression) when the result itself must be a Boolean value.
Which logical operator has the highest precedence?
Among the logical operators, not binds most tightly, then and, then or. Parentheses can make a different grouping explicit.
Does Python evaluate both sides of and and or?
No. and skips its right side when the left operand is falsey, while or skips its right side when the left operand is truthy.


